Programmatically Add Fake Hardware in Windows - windows

I have created a fake usb flash driver driver in Windows that will fake Windows into thinking that a new hardware device is attached. I have also found the APIs that allow me to enumerate the hardware attached (so I know which device is my fake driver).
The problem is I only want to attach the hardware when my program is running, and I don't want it to be accessible when my program is not running.
How can I programmatically add this hardware or enable/disable this driver? Someone suggested the right nomenclature is "load/unload".
Using VC++ with Windows APIs on Windows 7 and higher.

Try the C++ class wrapper to load/unload device drivers from code project.

Related

Can I use WinUSB alongside the built-in HID drivers?

I am prototyping a keyboard using a Pi Zero, and I plan to set the Pi Zero up so that it emulates an HID-compliant USB keyboard (for Windows). There are many guides on how to do this, so I do not think this will be an issue. The additional functionality of this keyboard is going to require the ability to configure certain aspects keyboard on the host machine and send those configurations back up to the device.
My understanding is that once I have the pi zero emulating an HID keyboard, I will not have to do any extra work with Windows to get the host to accept the new keyboard device - it will automatically recognize the HID device and use the correct built-in driver. The configuration bit, however, I will have to work on myself. I was planning on writing a Win32 application that calls WinUSB as the other driver that handles transfer for the configurations.
On the MSDN page for selecting a USB driver model, it states that WinUSB is a good option if:
Your device is accessed by a single application.
The question stands thusly:
Does having my device configured as an HID keyboard prohibit me from being able to use WinUSB as a configuration driver? More specifically, does having my keyboard constantly open in an HID filter driver (I believe the HID host is a filter driver) count as the device being used in one application already, where the configuration application would be a second?
I believe this answers my question.
https://social.msdn.microsoft.com/Forums/en-US/9687e8ba-9eb0-4d41-a8ac-973a029e05b2/winusb-sample-to-read-inputs-from-keyboard
only one driver can be installed on the device, either HID or winusb. you could force winusb onto the keyboard but then it would stop looking like a keyboard to the OS.
Ignoring the irking lack of capitalization in this post, this seems to state pretty conclusively that a separate driver needs to be written. How unfortunate.

Virtual USB device for Windows?

I'm investigating options available for creating a virtual USB device (say, a keyboard or a mass storage device), so to emulate its function as needed and to allow a userspace app to emulate its insertion/removal at will.
What I am not clear about is how to go about the emulation of insertion/removal. It seems that one option is to emulate a (virtual) USB hub and have it fake the device arrival/departure events (and I would also supply the device driver for my virtual USB device and that's where my device logic will reside).
I know my way around Windows kernel (having written NDIS miniport drivers), not afraid of SoftICE, but USB is not my domain, just starting out with it.
Am I on the right track with the virtual hub approach? If so, is developing virtual hub drivers supported by WDK (it doesn't seem to be)?
Any other options?
--
(Edit) Forgot to mention - I am aware of DSF, but it is not supported on W8.
Am I on the right track with the virtual hub approach?
In short - yes, I was right.
That's how USBIP does it and it's a relatively simple way to go. Also, see this comment by Eugen.
I don't know if you are aware, but Microsoft released to Win10 the UDE (USB device emulation).
In the section Write a UDE client driver they describe exactly what you want.

Faking the presence of a USB device in order to test driver installation on Windows

I've been asked to help with some problems that a company are having with an Windows installer they have that includes some custom driver installation for some hardware they make.
I've got access to the source code to build the software and installer (which is an WIX/MSI one), but don't have access to the hardware, so can't actually test it properly.
Is it possible (either with a toolkit, or without) to trick windows into thinking that a specific device USB device has been attached to the computer in order to trigger Windows into trying to install the drivers? . I've got access to all the Vendoer Id, DeviceID, etc information.
Thanks
Tom
That should be possible. Take a look at WDK USBSamp and NDIS Virtual miniport (or virtual serial driver) samples. The first one is a USB driver sample and second one demonstrates how to build virtual driver. You should be able to combine the two to create a virtual USB driver.

How can I make a custom USB device show up in Windows as a COM Port?

I have developed a USB device that communicates with linux over a simple but proprietary interface and some custom Linux drivers. My goal is to port this to Windows without writing windows drivers. What I would like to do is find an open source or inbuilt class driver for windows that would look like a COM port in Windows. Then I would tailor the embedded software to match what ever protocol and descriptors the virtual COM port expects to see.
The idea would be that I could plug my device in to a Windows machine and a relatively high speed COM port would appear with out me having to develop Windows drivers for it.
I have been looking at the USB CDC (Communications Device Class) documentation and it looks promising, but I don't know which sub interface would be best to use so that it would show up as a COM port.
Has anyone here done any work like this before or could provide some insight?
Specifically:
Are there virtual COM drivers "built in" to windows or would I need a 3rd party driver.
Which CDC sub class should I use for simple RS232 emulation (No need for modem AT commands, etc)
Is there a better option to do what I am trying to do.
Thanks
There is a USB-to-serial driver built in to Windows that will do what you want. It is called usbser.sys:
http://support.microsoft.com/kb/837637
You will have to write an INF file and distribute that to your users, but that will not be too hard because it is only a few kilobytes of text and you can find examples online.
I'm not aware of any great documentation for this driver by Microsoft, so my advice would be to find some other device that uses it, such as Pololu Wixel, and copy what they did.
Here are the device descriptors we used and the special control tranfers we had to implement:
https://github.com/pololu/wixel-sdk/blob/master/libraries/src/usb_cdc_acm/usb_cdc_acm.c
You can see our INF file, wixel_serial.inf, by downloading the software and looking in the drivers folder:
http://www.pololu.com/docs/0J46/3.a
(There are other files in there that are not necessary for you.)
You can also look at the Arduino Uno because they use the same driver.
Whatever you do, please don't use our USB Vendor ID in your product! You need to get your own.
Update: In Windows 10, you don't need an INF file anymore because of the new usbser.inf driver that comes with Windows.
If you are using a UART you can easily interface it to a FTDI USB chip like http://www.ftdichip.com/Products/ICs/FT232R.htm or a Prolific like http://www.prolific.com.tw/eng/products.asp?id=59
For development, prototype and testing I have half dozen of these laying around http://www.pololu.com/catalog/product/391
I connect it directly to the UART pins on AVR and 8051 micros.

Resetting a device in device manager

I am looking for a programmically API on how to restart any device that allows enable/disable in the device manager,
such as Audio devices and Network adapter
You will have to use SetupApi / ConfigManager API. But be aware that under x64 you app. must be also x64 to enable/disable device (so you cannot do it in Delphi directly right now - first I thought that it's a problem with file/registry redirection for x86-app under x64, but it didn't help). Device enumeration works fine. There was something about it in one article on MSDN but I cannot find it right now. I've made FP/Lazarus x64 application for enable/disable devices under x64 OS.
You can download WDK and look for source code of DevCon (C:\WinDDK\7600.16385.1\src\setup\devcon). In cmds.cpp there is function ControlCallback which enables/disables device using SetupApi). But first you need to enumerate device classes (by GUID or ClassName), and then enumerate device instances or open device by DeviceInstanceId string. It's in C but it should be easy to learn how to use that API.
Not sure what you are doing, but maybe it would be easier to use that devcon.exe (don't know if license permits it) and enable/disable devices by it?
I have no experience with it but I think you can use the DeviceIoControl API.

Resources