0

我正在尝试获取有关当前在 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;
4

0 回答 0