【发布时间】:2017-07-08 10:09:42
【问题描述】:
直升机,
我正在使用 GetLogicalDrives 获取所有驱动器,我想进一步使用它来检测驱动器类型,然后使用 GetVolumeInformation 检查特定驱动器的状态。但是,我无法在 GetVolumeInformation 和 GetDriveTypes 中使用 GetLogicalDrives(DWORD) 的结果,因为它们不支持 LPCWSTR。如何转换 GetLogicalDrives 的结果并将其传递给 GetVolumeInformation 和 GetDriveTypes?
TCHAR myDrives[] = L" A";
DWORD myDrivesBitMask = GetLogicalDrives();
WCHAR szTest[10];
if (myDrivesBitMask == 0)
wprintf(L"GetLogicalDrives() failed with error code: %d\n", GetLastError());
else {
wprintf(L"This machine has the following logical drives:\n");
while (myDrivesBitMask) {
// Use the bitwise AND with 1 to identify
// whether there is a drive present or not.
if (myDrivesBitMask & 1) {
// Printing out the available drives
wprintf(L"drive %s\n", myDrives);
}
// increment counter for the next available drive.
myDrives[1]++;
// shift the bitmask binary right
myDrivesBitMask >>= 1;
}
wprintf(L"\n");
}
UINT test;
for (i = 0; i<12; i++)
{
test = GetDriveType(myDrives[i]);
switch (test)
{
case 0: printf("Drive %S is type %d - Cannot be determined.\n", myDrives[i], test);
break;
case 1: printf("Drive %S is type %d - Invalid root path/Not available.\n", myDrives[i], test);
break;
case 2: printf("Drive %S is type %d - Removable.\n", myDrives[i], test);
break;
case 3: printf("Drive %S is type %d - Fixed.\n", myDrives[i], test);
break;
case 4: printf("Drive %S is type %d - Network.\n", myDrives[i], test);
break;
case 5: printf("Drive %S is type %d - CD-ROM.\n", myDrives[i], test);
break;
case 6: printf("Drive %S is type %d - RAMDISK.\n", myDrives[i], test);
break;
default: "Unknown value!\n";
}
}
(GetVolumeInformation(myDrives, volumeName, ARRAYSIZE(volumeName), &serialNumber, &maxComponentLen, &fileSystemFlags, fileSystemName, ARRAYSIZE(fileSystemName)))
{
_tprintf(_T("There is a CD/DVD in the CD/DVD rom"));
_tprintf(_T("Volume Name: %s\n"), volumeName);
_tprintf(_T("Serial Number: %lu\n"), serialNumber);
_tprintf(_T("File System Name: %s\n"), fileSystemName);
_tprintf(_T("Max Component Length: %lu\n"), maxComponentLen);
}
else
_tprintf(_T("There is NO CD/DVD in the CD/DVD rom"));
【问题讨论】:
-
使用
GetLogicalDriveStrings而不是GetLogicalDrives。 -
或者,只需将驱动器号
0映射到 `A:`,依此类推(按字母顺序)。 -
对于现代 Windows,只需使用 Unicode(
wchar_t类型)就可以大大简化事情,而不是像TCHAR这样的 Windows 9x 兼容性宏。毕竟,您的工具链可能甚至无法生成 Windows 9x 可执行文件。因此,除了使事情复杂化之外,这些东西都是徒劳的。