2
votes

I'm trying to get the "friendly name" of a plugged-in USB device. I'm using SetupDiGetDeviceRegistryProperty method with SPDRP_FRIENDLYNAME property but the method returns false and sets the error code to ERROR_INVALID_DATA, although everything works fine with other properties, such as SPDRP_DEVICEDESC or SPDRP_MFG.

I checked the registry and the Device Manager and the friendly name exists.

Does anyone have any idea?

UPDATE: What i tried so far:

GUID hidGuid;
HidD_GetHidGuid(&hidGuid);

HDEVINFO hDevInfo = SetupDiGetClassDevs(&hidGuid, 0, 0, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);

if (INVALID_HANDLE_VALUE == hDevInfo)
{
    AfxMessageBox(CString("SetupDiGetClassDevs(): ")
        + _com_error(GetLastError()).ErrorMessage(), MB_ICONEXCLAMATION);
    return;
}
SP_DEVINFO_DATA* pspDevInfoData =
    (SP_DEVINFO_DATA*)HeapAlloc(GetProcessHeap(), 0, sizeof(SP_DEVINFO_DATA));
pspDevInfoData->cbSize = sizeof(SP_DEVINFO_DATA);
for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, pspDevInfoData); i++)
{
    DWORD DataT;
    DWORD nSize = 0;
    TCHAR buf[MAX_PATH];
if (!SetupDiGetDeviceInstanceId(hDevInfo, pspDevInfoData, buf, sizeof(buf), &nSize))
    {
        AfxMessageBox(CString("SetupDiGetDeviceInstanceId(): ")
            + _com_error(GetLastError()).ErrorMessage(), MB_ICONEXCLAMATION);
        break;
    }
if (SetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData,
                    SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
    { 
           //display buf
    }
else
    {
        if (GetLastError() == ERROR_INVALID_DATA)
        {
            //display ERROR_INVALID_DATA
        }
        if (SetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData, SPDRP_MFG, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
        {
            //display buf
        }
        if (SetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData,
                SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
        {
            // display buf
        }
   }
}

Something like this. As i said, i get the device description and the device manufacturer, but not the friendly name.

3
"Does anyone have any idea?" - There's a problem with your code.Captain Obvlious
Please provide some code to show what you have tried so far.avojak

3 Answers

2
votes

Not all devices have SPDRP_FRIENDLYNAME attribute set. When that's the case, ERROR_INVALID_DATA is expected, it tells you just that.

When they don’t have it, device manager GUI uses another one for display name, SPDRP_DEVICEDESC

0
votes

Maybe useful information:

  1. In my case I had two network adapters, but the function succeeded only for the adapter that is shown as "... #2" in device manager when SPDRP_FRIENDLYNAME is used.

  2. I could also verify that the other adapter (without the "... #2") does not have a value "FriendlyName" in it's registry data.

  3. This behaviour seems to depend on the O/S. In may case the funtion succeeded always when SPDRP_FRIENDLYNAME is used Windows 10, but only worked for the device that was shown as "...#2" in device manager.

0
votes

This issue mainly goes down to Windows 10 stopping unsigned drivers from installing, even when the 'driver' is just a .inf file that simply references a (presumably signed) windows DLL, but is there to change the "USB Serial Device" into something meaningful to humans, and recognisable by the application software. I've had to re-write 10 different projects because of this issue.

I now have to check for specific VID/PID, however it is not future proof.