直升机,
我使用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"));
答案 0 :(得分:2)
由于您需要使用驱动器号码来呼叫GetDriveType()
和GetVolumeInformation()
,因此使用GetLogicalDriveStrings()
代替GetLogicalDrives()
会更容易,例如:
WCHAR myDrives[105];
WCHAR volumeName[MAX_PATH];
WCHAR fileSystemName[MAX_PATH];
DWORD serialNumber, maxComponentLen, fileSystemFlags;
UINT driveType;
if (!GetLogicalDriveStringsW(ARRAYSIZE(myDrives)-1, myDrives))
{
wprintf(L"GetLogicalDrives() failed with error code: %lu\n", GetLastError());
}
else
{
wprintf(L"This machine has the following logical drives:\n");
for (LPWSTR drive = myDrives; *drive != 0; drive += 4)
{
driveType = GetDriveTypeW(drive);
wprintf(L"Drive %s is type %d - ", drive, driveType);
switch (driveType)
{
case DRIVE_UNKNOWN:
wprintf(L"Cannot be determined!");
break;
case DRIVE_NO_ROOT_DIR:
wprintf(L"Invalid root path/Not available.");
break;
case DRIVE_REMOVABLE:
wprintf(L"Removable.");
break;
case DRIVE_FIXED:
wprintf(L"Fixed.");
break;
case DRIVE_REMOTE:
wprintf(L"Network.");
break;
case DRIVE_CDROM:
wprintf(L"CD-ROM.");
break;
case DRIVE_RAMDISK:
wprintf(L"RAMDISK.");
break;
default:
wprintf(L"Unknown value!");
}
wprintf(L"\n");
if (driveType == DRIVE_CDROM)
{
if (GetVolumeInformationW(drive, volumeName, ARRAYSIZE(volumeName), &serialNumber, &maxComponentLen, &fileSystemFlags, fileSystemName, ARRAYSIZE(fileSystemName)))
{
wprintf(L" There is a CD/DVD in the drive:\n");
wprintf(L" Volume Name: %s\n", volumeName);
wprintf(L" Serial Number: %08X\n", serialNumber);
wprintf(L" File System Name: %s\n", fileSystemName);
wprintf(L" Max Component Length: %lu\n", maxComponentLen);
}
else
{
wprintf(L" There is NO CD/DVD in the drive");
}
}
}
}
答案 1 :(得分:1)
我会为GetLogicalDrives()创建一个包装器函数,以便更容易处理驱动器路径的向量:
Bid.where('supplier.name' => 'Ford')
可以像这样使用:
std::vector< std::wstring > GetLogicalDrivePathes()
{
std::vector< std::wstring > result;
DWORD mask = GetLogicalDrives();
for( wchar_t drive = 'A'; drive <= 'Z'; ++drive )
{
if( mask & 1 )
{
std::wstring rootPath;
rootPath += drive;
rootPath += L":\\";
result.push_back( rootPath );
}
mask >>= 1;
}
return result;
}
答案 2 :(得分:0)
如果您希望使用Drive字符串来解决问题,我也讨厌双NULL术语字符串。
甚至无法在调试器中查看它们,或者我不知道如何...
此函数采用该双NULL终止的字符串,并将其拆分为CStringArray,可以随意使用您喜欢的字符串。就是这样。
size_t ExtractNullTermStringsFromBuffer(CStringArray &dest, const WCHAR* SourceBuffer, const size_t BufLen)
{
if (SourceBuffer[0] == '\0')
return 0;
WCHAR* strtok = new WCHAR[BufLen];
size_t tokPos = 0;
for (size_t curPos = 0; curPos < BufLen; ) {
if (SourceBuffer[curPos] == '\0') { // first null byte
strtok[tokPos] = '\0';
tokPos = 0; // set to start new string
dest.Add(strtok); //add it
curPos++; // increment pos
if (SourceBuffer[curPos] == '\0') { // check for the end
curPos = BufLen; // break the loop
}
} else {
strtok[tokPos] = SourceBuffer[curPos]; // otherwise continue
curPos++;
tokPos++;
}
}
delete strtok;
return dest.GetCount();
}
GetLogicalDriveStrings部分位于此处,然后调用上述函数
DWORD dwSize = MAX_PATH;
WCHAR szLogicalDrives[MAX_PATH] = { 0 };
DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
CStringArray dest;
size_t szSize = ExtractNullTermStringsFromBuffer(dest, szLogicalDrives, MAX_PATH);
for (size_t curPos = 0; curPos < szSize; curPos++) {
// dest[curPos]; do something with it here
}