question

RadimLanger-2441 avatar image
0 Votes"
RadimLanger-2441 asked RadimLanger-2441 commented

How to get device instance id for COM port

How can I programatically find out device instance id for COM port. In my case the COM port is an virtual FTDI COM port. In Device manager / Details / Device instance path I can see
FTDIBUS\VID_0403+PID_6015+DN04P3D2A\0000.

In my application, I open the com port using:
comHandle = CreateFileA("\\\.\\com10", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

Now I wonder if the comHandle could be used for determining the device id. Or maybe the device id could be recognized directly from the com port number?

I need the device id for restarting the device. I use devcon source code functions for it and thus I am able to simulate devcon restart @FTDIBUS\VID_0403+PID_6015+DN04P3D2A\0000 command in my application. But I need to identify the correct device id for the particular COM port.

Thank you for any help



windows-hardware
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered RadimLanger-2441 commented

You can use SetupDiGetDeviceInstanceId

A quick test with GUID_SERENUM_BUS_ENUMERATOR =>

     HDEVINFO DeviceInfoSet = SetupDiGetClassDevs(&GUID_SERENUM_BUS_ENUMERATOR, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
     SP_DEVINFO_DATA DeviceInfoData;
     DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
     for (DWORD dwDeviceIndex = 0; SetupDiEnumDeviceInfo(DeviceInfoSet, dwDeviceIndex, &DeviceInfoData); dwDeviceIndex++)
     {
         SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
         DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
         for (DWORD dwMemberIndex = 0; SetupDiEnumDeviceInterfaces(DeviceInfoSet, &DeviceInfoData, &GUID_SERENUM_BUS_ENUMERATOR, dwMemberIndex, &DeviceInterfaceData); dwMemberIndex++)
         {
             DWORD dwDeviceInterfaceDetailDataSize = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA, DevicePath) + MAX_PATH * sizeof(TCHAR);
             PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)new BYTE[dwDeviceInterfaceDetailDataSize];
             pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
             if (SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, &DeviceInterfaceData, pDeviceInterfaceDetailData, dwDeviceInterfaceDetailDataSize, NULL, NULL))
             {
                 DWORD dwRegType;
                 WCHAR wsPropertyBuffer[256] = L"";
                 BOOL bRet = SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_FRIENDLYNAME, &dwRegType, (BYTE*)wsPropertyBuffer, 256, NULL);
                 ULONG nSize = 0;
                 WCHAR wsInstanceId[255] = L"";
                 SetupDiGetDeviceInstanceId(DeviceInfoSet, &DeviceInfoData, wsInstanceId, nSize, &nSize);
                 bRet = SetupDiGetDeviceInstanceId(DeviceInfoSet, &DeviceInfoData, wsInstanceId, nSize, &nSize);

                 WCHAR wsText[255] = L"";
                 swprintf(wsText, L"Name : %s\r\n", wsPropertyBuffer);
                 OutputDebugString(wsText);
                 swprintf(wsText, L"InstanceId : %s\r\n", wsInstanceId);
                 OutputDebugString(wsText);

                 delete[] pDeviceInterfaceDetailData;                    
             }
         }
     }


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This helped me a lot. Thank you.

0 Votes 0 ·