How to get device instance id for COM port

Radim Langer 101 Reputation points
2020-12-14T14:07:43.293+00:00

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 Performance
Windows Hardware Performance
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.Hardware Performance: Delivering / providing hardware or hardware systems or adjusting / adapting hardware or hardware systems.
1,574 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 83,101 Reputation points
    2020-12-14T17:10:37.13+00:00

    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;					  
    			}  
    		}  
    	}  
    

0 additional answers

Sort by: Most helpful