获取有关分区的信息

我从msdn中得到一个c ++代码的例子,我尝试获取有关分区的信息,但是在这段代码中,我的DeviceIoControl方法返回0,结束了错误代码3.我该如何解决这个错误?

代码在这里:

#define WINVER 0x0500 #include <windows.h> #include <winioctl.h> #include <iostream> #include <stdio.h> void DisplayVolumeInfo(CHAR *volume) { HANDLE hDevice = CreateFileA( volume, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, NULL); if (hDevice != INVALID_HANDLE_VALUE) { PARTITION_INFORMATION partInfo; DWORD retcount = 0; BOOL res = DeviceIoControl( hDevice, IOCTL_DISK_GET_PARTITION_INFO, (LPVOID)NULL, (DWORD)0, (LPVOID)&partInfo, sizeof(partInfo), &retcount, (LPOVERLAPPED)NULL); if (res) std::cout << "Volume size = " << partInfo.PartitionLength.QuadPart << " bytes (" << (unsigned long long)(partInfo.PartitionLength.QuadPart / 1024 / 1024) << "Mb)" << std::endl; else std::cout << "Can't do IOCTL_DISK_GET_PARTITION_INFO (error code=" << GetLastError() << ")" << std::endl; DISK_GEOMETRY diskGeometry; retcount = 0; res = DeviceIoControl( hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &diskGeometry, sizeof(diskGeometry), &retcount, (LPOVERLAPPED)NULL); if (res) { std::cout << "Cylinders = " << diskGeometry.Cylinders.QuadPart << std::endl; std::cout << "Tracks/cylinder = " << diskGeometry.TracksPerCylinder << std::endl; std::cout << "Sectors/track = " << diskGeometry.SectorsPerTrack << std::endl; std::cout << "Bytes/sector = " << diskGeometry.BytesPerSector << std::endl; } else std::cout << "Can't do IOCTL_DISK_GET_DRIVE_GEOMETRY (error code=" << GetLastError() << ")" << std::endl; CloseHandle(hDevice); } else std::cout << "Error opening volume " << volume << " (error code=" << GetLastError() << ")" << std::endl; } int main() { DWORD CharCount = 0; char DeviceName[MAX_PATH] = ""; DWORD Error = ERROR_SUCCESS; HANDLE FindHandle = INVALID_HANDLE_VALUE; BOOL Found = FALSE; size_t Index = 0; BOOL Success = FALSE; char VolumeName[MAX_PATH] = ""; // // Enumerate all volumes in the system. FindHandle = FindFirstVolumeA(VolumeName, MAX_PATH); if (FindHandle == INVALID_HANDLE_VALUE) { Error = GetLastError(); printf("FindFirstVolume failed with error code %d\n", Error); return 0; } for (;;) { // // Skip the \\?\ prefix and remove the trailing backslash. Index = strlen(VolumeName) - 1; if (VolumeName[0] != '\\' || VolumeName[1] != '\\' || VolumeName[2] != '?' || VolumeName[3] != '\\' || VolumeName[Index] != '\\') { Error = ERROR_BAD_PATHNAME; printf("FindFirstVolume/FindNextVolume returned a bad path: %s\n", VolumeName); break; } // // QueryDosDeviceW does not allow a trailing backslash, // so temporarily remove it. VolumeName[Index] = '\0'; CharCount = QueryDosDeviceA(&VolumeName[4], DeviceName, MAX_PATH); VolumeName[Index] = '\\'; if ( CharCount == 0 ) { Error = GetLastError(); printf("QueryDosDevice failed with error code %d\n", Error); break; } printf("\nFound a device:\n %s", DeviceName); printf("\nVolume name: %s", VolumeName); printf("\n"); DisplayVolumeInfo(VolumeName); // // Move on to the next volume. Success = FindNextVolumeA(FindHandle, VolumeName, MAX_PATH); if ( !Success ) { Error = GetLastError(); if (Error != ERROR_NO_MORE_FILES) { printf("FindNextVolumeW failed with error code %d\n", Error); break; } // // Finished iterating // through all the volumes. Error = ERROR_SUCCESS; break; } } FindVolumeClose(FindHandle); FindHandle = INVALID_HANDLE_VALUE; return 0; } 

有没有人遇到这个问题?

PS我在Windows 7 x32上工作

Solutions Collecting From Web of "获取有关分区的信息"