0

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

  1. can't choose the COM port the service connects to and
  2. 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!

Bastian
  • 26
  • 6
  • Hello Bastian. I don't get why you can't choose the port you want. Have you tried listing all available serial ports? See [here](https://codereview.stackexchange.com/questions/111178/find-a-serial-port-device-through-wmi-windows-management-instrumentation) or [here](https://stackoverflow.com/questions/11458835/finding-information-about-all-serial-devices-connected-through-usb-in-c-sharp/23651956#23651956). – Marcos G. Apr 07 '21 at 07:15
  • Hello @MarcosG. . Maybe I should clarify that the code in the op is part of the service I can’t modify. I already have another program with an associated virtual COM port. My problem is that the unmodifiable program chooses the COM port it connects to by waiting for a serial to USB adapter to connect to the pc (which doesn’t exist when the COM port is virtual). What I need is a virtual COM to USB adapter that can simulate being plugged in otherwise the unmodifiable service just won`t do anything. – Bastian Apr 07 '21 at 08:52
  • I see what you mean now, it was not clear to me after reading your question. As far as I know, you won't be able to emulate a specific UID or the plugging-unplugging with any existing virtual/fake serial ports. For Win10 you can do it with [UDE](https://learn.microsoft.com/en-us/windows-hardware/drivers/usbcon/developing-windows-drivers-for-emulated-usb-host-controllers-and-devices) but if you have to start from scratch I'm afraid it might be a huge effort. – Marcos G. Apr 07 '21 at 09:40
  • @MarcosG. Thank you for your suggestion, I will look into UDE, while it isn't ideal it seems like my only solution. – Bastian Apr 08 '21 at 20:26

0 Answers0