Get Device using GUID always fails

Started by
0 comments, last by violentrevolution 11 years, 3 months ago

Hello

I am attempting to get information(location info, location path, etc.) about a device that is currently connected to the computer in C++ Win32. I know how to get this information by using the function SetupDiGetDeviceRegistryProperty()

Before I use the function SetupDiGetDeviceRegistryProperty(), I must first call SetupDiGetSelectedDevice() because I need to pass a SP_DEVINFO_DATA as a parameter inside SetupDiGetDeviceRegistryProperty(). Is this correct?

My Problem: I can never get the device using the function SetupDiGetSelectedDevice(). When I call that function it always fails, ie, returns FALSE. GetLastError() returns the code e0000211 which I am not sure what that means.

Whats going wrong with my following code? If I am using the wrong function to *get* a device then what function do I use to get a device? I hope I dont have to enumerate over EVERY device present to find a device with the same GUID that I am looking for, thats a bit inefficient, I am thinking that windows may have a way to get a device by its GUID..?


INT_PTR WINAPI WinProcCallback( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
    {
        switch (message)
        {
            case WM_DEVICECHANGE:
            {
                TCHAR strBuff[256];
    
                PDEV_BROADCAST_HDR h = (PDEV_BROADCAST_HDR) lParam;
                if (h->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE) {
                    printf("h->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE\n");
                    break;
                }



               PDEV_BROADCAST_DEVICEINTERFACE b = (PDEV_BROADCAST_DEVICEINTERFACE) h;
    
                switch (wParam)
                {
                    case DBT_DEVICEARRIVAL:
                    {
                        DWORD dataT = 0;
                    SP_DEVINFO_DATA deviceInfoData = {0};
                    deviceInfoData.cbSize          = sizeof(SP_DEVINFO_DATA);
                    deviceInfoData.ClassGuid       = b->dbcc_classguid;
                
                   // The following function always works and is successful
                    HDEVINFO hDevInfo = SetupDiGetClassDevs(&b->dbcc_classguid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
                    if (hDevInfo == INVALID_HANDLE_VALUE) {
                        printf("hDevInfo == INVALID_HANDLE_VALUE\n");
                        break;
                    }
                
                   // ERROR OCCURS HERE: The following function ALWAYS returns false: whats going wrong?
                    if (SetupDiGetSelectedDevice(hDevInfo, &deviceInfoData) == FALSE) {
                        printf("SetupDiGetSelectedDevice(hDevInfo, &deviceInfoData) == FALSE\n");
                        break;
                    }
    
                   // Get device location information
                   DWORD buffersize = 0;
                     LPTSTR buffer    = NULL;
                     while (!SetupDiGetDeviceRegistryProperty(hDevInfo,  &deviceInfoData, SPDRP_LOCATION_INFORMATION, &dataT,
                                                                        (PBYTE)buffer, buffersize, &buffersize))
                     {
                         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
                             // Change the buffer size.
                             if (buffer)
                                 LocalFree(buffer);
                             buffer = (LPTSTR)LocalAlloc(LPTR, buffersize);
                         }
                     }
             
                     printf("Data: %d: %s\n", i, buffer);
                    }
                    break;

Advertisement

DIGCF_DEVICEINTERFACE won't work with a NULL Enumerator. It's also clearly mentioned on MSDN http://msdn.microsoft.com/en-us/library/windows/hardware/ff551069(v=vs.85).aspx

This topic is closed to new replies.

Advertisement