我想在我的机器中列出USB驱动器。 如何在VC ++中完成。你可以提供一个示例代码。
答案 0 :(得分:3)
根据GetDriveType的文档,它说我们应该使用SetupDiGetDeviceRegistryProperty,我引用:
要确定驱动器是否为USB类型驱动器,请调用SetupDiGetDeviceRegistryProperty并指定SPDRP_REMOVAL_POLICY属性。
我运行了一些测试,无法找到某个设备是USB驱动器的任何迹象。
SPDRP_REMOVAL_POLICY
为许多设备(以及我的USB驱动器)返回2,所以我无法真正使用它。使用SetupDiGetDeviceRegistryProperty
调用SPDRP_CAPABILITIES
并仅过滤CM_DEVCAP_REMOVABLE
也会为许多设备提供服务(即使与移除策略结合使用时,也无法提供有关如何查找USB驱动器的任何良好指示。
此外,使用SetupDiGetDeviceRegistryProperty
调用SPDRP_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, ®DataType,
(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, ®DataType,
(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, ®DataType,
(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, ®DataType,
(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;
}
}
答案 1 :(得分:0)
我认为你不会让任何人为你编写代码:你是程序员,(大概)你的工作。
但是,您可以从GetLogicalDriveStrings和GetDriveType开始。