0

我想列出我机器中的 USB 驱动器。如何在 VC++ 中做到这一点。你能给出一个示例代码吗?

4

2 回答 2

4

根据 GetDriveType 的文档,它说我们应该使用SetupDiGetDeviceRegistryProperty,我引用:

要确定驱动器是否为 USB 类型驱动器,请调用 SetupDiGetDeviceRegistryProperty 并指定 SPDRP_REMOVAL_POLICY 属性。

我进行了一些测试,但找不到任何表明某个设备是 USB 驱动器的迹象。 SPDRP_REMOVAL_POLICY为许多设备(以及我的 USB 驱动器)返回 2,所以我不能真正使用它。仅调用SetupDiGetDeviceRegistryPropertywithSPDRP_CAPABILITIES和 filterCM_DEVCAP_REMOVABLE也会提供许多设备(即使与删除策略结合使用也不能很好地指示如何找到我的 USB 驱动器。此外,调用SetupDiGetDeviceRegistryPropertywithSPDRP_DEVTYPE总是返回错误 13(“数据无效。”),我不知道为什么。

这是一些代码:

void SetupDiInformation()
{
    HDEVINFO hDevInfo = SetupDiGetClassDevsW(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
    if (INVALID_HANDLE_VALUE == hDevInfo)
    {
        fwprintf(stderr, L"Error SetupDiCreateDeviceInfoList: %d\n", GetLastError());
        return;
    }

    SP_DEVINFO_DATA devInfoData;
    devInfoData.cbSize = sizeof(devInfoData);
    BOOL success;
    success = SetupDiEnumDeviceInfo(hDevInfo, 0, &devInfoData);
    for (int i=1; success; i++)
    {
        DWORD regDataType = REG_NONE, reqSize = 0;
        WCHAR deviceDesc[MAX_PATH+1] = {0};
        DWORD deviceType = -1, capabilities = -1;
        DWORD removalPolicy = CM_REMOVAL_POLICY_EXPECT_NO_REMOVAL;
        BOOL regPropSuccess = false;

/*
        regDataType = REG_NONE; reqSize = 0;
        regPropSuccess = SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, 
            SPDRP_DEVTYPE, &regDataType, 
            (PBYTE)&deviceType, sizeof(deviceType), &reqSize);
        if (!regPropSuccess)
        {
            fwprintf(stderr, L"Error SetupDiGetDeviceRegistryProperty(SPDRP_DEVTYPE)[%d]: %d\n", i, GetLastError());
        }
*/

        regPropSuccess = SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, 
            SPDRP_DEVICEDESC, &regDataType, 
            (PBYTE)deviceDesc, sizeof(deviceDesc), &reqSize);
        if (!regPropSuccess)
        {
            fwprintf(stderr, L"Error SetupDiGetDeviceRegistryProperty(SPDRP_DEVICEDESC)[%d]: %d\n", i, GetLastError());
        }


        regDataType = REG_NONE; reqSize = 0;
        regPropSuccess = SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, 
            SPDRP_CAPABILITIES, &regDataType, 
            (PBYTE)&capabilities, sizeof(capabilities), &reqSize);
        if (!regPropSuccess)
        {
            fwprintf(stderr, L"Error SetupDiGetDeviceRegistryProperty(SPDRP_CAPABILITIES)[%d]: %d\n", i, GetLastError());
        }  

        regDataType = REG_NONE; reqSize = 0;
        regPropSuccess = SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, 
            SPDRP_REMOVAL_POLICY, &regDataType, 
            (PBYTE)&removalPolicy, sizeof(removalPolicy), &reqSize);
        if (!regPropSuccess)
        {
            fwprintf(stderr, L"Error SetupDiGetDeviceRegistryProperty(SPDRP_REMOVAL_POLICY)[%d]: %d\n", i, GetLastError());
        }  

        if ((CM_DEVCAP_REMOVABLE & capabilities) != 0)
        {
            wprintf(L"% 4d. ", i);
            wprintf(L"%X-%04X-%X-", 
                devInfoData.ClassGuid.Data1, 
                devInfoData.ClassGuid.Data2, 
                devInfoData.ClassGuid.Data3);
            int data4len = sizeof(devInfoData.ClassGuid.Data4)/sizeof(devInfoData.ClassGuid.Data4[0]);
            for (int j=0; j<data4len; j++)
                wprintf(L"%02X", devInfoData.ClassGuid.Data4[j]);

            if (wcslen(deviceDesc) > 30)
                deviceDesc[30]=L'\0';
            //wprintf(L" %-8d%-30s 0x%08X %d [%d] ", devInfoData.DevInst, deviceDesc, deviceType, removalPolicy, capabilities);
            wprintf(L" %-8d%-30s %d [%d] ", devInfoData.DevInst, deviceDesc, removalPolicy, capabilities);
            //DisplayCapabilities(capabilities);
            wprintf(L"\n");
        }

        success = SetupDiEnumDeviceInfo(hDevInfo, i, &devInfoData);
    }

    DWORD lastError = GetLastError();
    if (lastError != ERROR_NO_MORE_ITEMS)
    {
        // error occurred
        fwprintf(stderr, L"Error SetupDiEnumDeviceInfo: %d\n", lastError);
    }

    if (!SetupDiDestroyDeviceInfoList(hDevInfo))
    {
        fwprintf(stderr, L"Error SetupDiDestroyDeviceInfoList: %d\n", GetLastError());
        return;
    }
}
于 2011-02-09T16:45:14.573 回答
0

我认为您不会让任何人为您编写代码:您是一名程序员,那(大概)是您的工作

但是,您可以从GetLogicalDriveStringsGetDriveType开始。

于 2010-04-22T05:28:54.763 回答