MKallarakkal
MKallarakkal

Reputation: 315

Unique Identifier for Mouse,Keyboard and other HID devices

I have an MFC application that used to find the details of devices in the windows. The PnPID of USB Storage devices is unique in every system. but in the case of mouse-keyboard, the PnPID is different on every computer for the same device. is there is any way to find the unique identifier of Mouse, Keyboard, and HID using C++. If anyone knows about the unique identifier for HID devices or mouse and keyboard please share.Here is my code.

while(SetupDiEnumDeviceInfo(hDevInfo,lCount++,&DevInfoData))
        {
            ///////////////////////////////////////
            DWORD   dwDataTypeHardwareid;
            LPTSTR  pszBufferHardwareid     = NULL;
            LPTSTR  pszBufferPnPid      = NULL;
            DWORD   dwBuffSizeHardwareid    = 0; 
            int     iRetValHardwareid       = 1;
            ///////////////////////////////////////
            iRetValHardwareid = SetupDiGetDeviceRegistryProperty(hDevInfo, &DevInfoData, SPDRP_HARDWAREID, &dwDataTypeHardwareid, (PBYTE)pszBufferHardwareid, dwBuffSizeHardwareid, &dwBuffSizeHardwareid);
            DWORD dwErr = GetLastError();

            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                if (pszBufferHardwareid) 
                    LocalFree(pszBufferHardwareid);
                //// / //
                pszBufferHardwareid = (char *)LocalAlloc(LPTR,dwBuffSizeHardwareid * 2);
                iRetValHardwareid=SetupDiGetDeviceRegistryProperty(hDevInfo, &DevInfoData, SPDRP_HARDWAREID, &dwDataTypeHardwareid, (PBYTE)pszBufferHardwareid, dwBuffSizeHardwareid, &dwBuffSizeHardwareid);
            }       
            if(StrStrI(pszBufferHardwareid,"HID\\"))
            {
                if(StrStrI(pszBufferClass,"Mouse"))
                {
                    CM_Get_Device_ID(DevInfoData.DevInst, pszBufferPnPid, MAX_PATH, 0); //**Not unique in different machines**
                    CString strDevid = (CString)pszBufferPnPid;
                    strDevid.Trim();
                    strDevid.MakeUpper();
                    SaveDeviceDetails(strDevid); //Method to save device details to DB
                }
            }
        }

Upvotes: 2

Views: 2846

Answers (1)

Sergio Monteleone
Sergio Monteleone

Reputation: 2886

Since the OP clarified he's looking for an unique identifier for HID devices, I suggest to try with the device serial number, although there is no guarantee that it will be unique.

On Win32 you can use the HidD_GetSerialNumberString function to retrieve the serial number of an HID device.

Upvotes: 1

Related Questions