List USB drive letters in VC++ [closed]

泪湿孤枕 提交于 2020-05-18 01:47:45

问题


I want to list USB drives in my machine. How to do it in VC++. Can u give a sample code.


回答1:


I don't think you're going to get anybody to write the code for you: you're a programmer, that's (presumably) your job.

However, you can start with GetLogicalDriveStrings and GetDriveType.




回答2:


According to the documentation of GetDriveType it says we should use SetupDiGetDeviceRegistryProperty for, and I quote:

To determine whether a drive is a USB-type drive, call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property.

I ran some tests and couldn't find any indication of a certain device being a USB drive. SPDRP_REMOVAL_POLICY returns 2 for many devices (as well as my USB drives) so I can't really use that. Calling SetupDiGetDeviceRegistryProperty with SPDRP_CAPABILITIES and filter only CM_DEVCAP_REMOVABLE also gives many devices (even when combining with removal policy doesn't give any good indication how to find my USB drives. Also, calling SetupDiGetDeviceRegistryProperty with SPDRP_DEVTYPE always returns an error 13 ("The data is invalid.") and I have no idea why.

Here's some code:

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


来源:https://stackoverflow.com/questions/2688402/list-usb-drive-letters-in-vc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!