I have a C# application/windows service (and the source code) that is trying to an external USB device over serial and I want to replace the physical USB device with a virtual one made by me (another C# service/app). That would normally be straightforward as I have done that multiple times before using com0com to connect the COM port of my program with the COM port of another.
My problem is that the service uses WMI (Windows Management Instrumentation) to check for both the insertion of an USB device with a specific VID and PID (in the "PNPDeviceID"/"DeviceID") ...
this.USBInsertWatcher = new ManagementEventWatcher();
WqlEventQuery wqlEventQuery1 = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBHub'");
this.USBInsertWatcher.EventArrived += new EventArrivedEventHandler(this.OnUSBDeviceInserted);
this.USBInsertWatcher.Query = (EventQuery) wqlEventQuery1;
this.USBInsertWatcher.Start();
... and for the associated COM port ("DeviceID") to connect to.
private string GetComPort()
{
using (ManagementObjectCollection instances = new ManagementClass("Win32_SerialPort").GetInstances())
{
foreach (ManagementBaseObject managementBaseObject in instances)
{
string str = managementBaseObject["PNPDeviceID"].ToString();
if (str.Contains(this.vendorIdStr) && str.Contains(this.productIdStr))
return managementBaseObject["DeviceID"].ToString();
}
return (string) null;
}
}
This means that I
- can't choose the COM port the service connects to and
- can't get the service to connect to anything in the first place :/
What I believe I need is some kind of driver that simulates a COM to USB adapter. I already have a virtual com port (opened by my other C# program)
I'm open to any kind of solution, but I should be usable on a non-virtualized Windows machine, also I can't recompile the binary because of (license/building) issues and the solution should be free.
Thank you!