How to get com ports of usb device using setupapi?

I need to determine number of modems com ports. Modem is usb device.

To accomplish this I need

  1. Detect attached modem

    GUID usbGuid = { 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };
    HDEVINFO hDevInfo = SetupDiGetClassDevs(&usbGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    
    SP_DEVINFO_DATA DeviceInfoData;
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    DWORD i;
    
    for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
    {
        //detect by vendorId
    }
    

2.For attached modem list all com ports that it emulates. But how to gain access to info about child devices and associated com ports number. Below log from USB Tree View with information about com ports that I need.

    +++++++++++++++++ Device Information ++++++++++++++++++
Device Description       : Composite USB device
Device Path              : ?usb#vid_1199&pid_68c0#6&24e4eadd&0&4#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
...
Power State              : D0 (supported: D0, D1, D2, D3, wake from D0, wake from D1, wake from D2)
 Child Device 1          : Sierra Wireless NMEA Port
  Device ID              : USBVID_1199&PID_68C0&MI_02&2BC75400&0&0002
  Class                  : Ports
  COM-Port               : COM19 (DeviceQCUSB_COM19_2)
 Child Device 2          : Sierra Wireless Mobile Broadband Network Adapter
  Device ID              : USBVID_1199&PID_68C0&MI_0A&2BC75400&0&000A
  Class                  : Net
 Child Device 3          : Sierra Wireless WWAN Modem
  Device ID              : USBVID_1199&PID_68C0&MI_03&2BC75400&0&0003
  Class                  : Modem
  COM-Port               : COM18 (DeviceQCUSB_COM18_3)
 Child Device 4          : Sierra Wireless Mobile Broadband Network Adapter
  Device ID              : USBVID_1199&PID_68C0&MI_08&2BC75400&0&0008
  Class                  : Net
 Child Device 5          : Sierra Wireless Mobile Broadband Network Adapter
  Device ID              : USBVID_1199&PID_68C0&MI_0B&2BC75400&0&000B
  Class                  : Net
 Child Device 6          : Sierra Wireless DM Port
  Device ID              : USBVID_1199&PID_68C0&MI_00&2BC75400&0&0000
  Class                  : Ports
  COM-Port               : COM17 (DeviceQCUSB_COM17_1)


Source: c++

Leave a Reply