使用GUID获取设备始终失败

时间:2013-01-06 08:07:40

标签: c++ winapi guid ports

我正在尝试获取有关当前在C ++ Win32中连接到计算机的设备的信息(位置信息,位置路径等)。我知道如何使用函数SetupDiGetDeviceRegistryProperty()

获取此信息

在我使用函数SetupDiGetDeviceRegistryProperty()之前,我必须先调用SetupDiGetSelectedDevice(),因为我需要在SP_DEVINFO_DATA内传递SetupDiGetDeviceRegistryProperty()作为参数。这是对的吗?

我的问题:我永远无法使用SetupDiGetSelectedDevice()功能获取设备。当我调用该函数时,它总是失败,即返回FALSE。 GetLastError()返回代码e0000211,我不确定这意味着什么。

我的以下代码出了什么问题?如果我使用错误的功能获取设备,那么我将使用什么功能来获取设备?

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

            switch (wParam)
            {
                case DBT_DEVICEARRIVAL:
                {
                    DWORD dataT = 0;
                SP_DEVINFO_DATA deviceInfoData = {0};
                deviceInfoData.cbSize          = sizeof(SP_DEVINFO_DATA);
                deviceInfoData.ClassGuid       = h->dbcc_classguid;

               // The following function always works and is successful
                HDEVINFO hDevInfo = SetupDiGetClassDevs(&h->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;

0 个答案:

没有答案