【发布时间】:2020-07-30 01:42:30
【问题描述】:
我在使用 DeviceIoControl 查询 PartitionCount 时遇到问题。
#include <tchar.h>
#include <iostream>
#include <windows.h>
int wmain(int argc, wchar_t *argv[])
{
HANDLE hDevice = CreateFileA("\\\\.\\PhysicalDrive0", 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
return 0;
}
else {
DWORD dwInfoSize = sizeof(DRIVE_LAYOUT_INFORMATION_EX)+sizeof(PARTITION_INFORMATION_EX)* 3;
DRIVE_LAYOUT_INFORMATION_EX* tpDriveLayoutInformationEx = (DRIVE_LAYOUT_INFORMATION_EX*)malloc(dwInfoSize);
if (NULL == tpDriveLayoutInformationEx) {
CloseHandle(hDevice);
return(-1);
}
DWORD dwResult = 0;
BOOL bResult = DeviceIoControl(hDevice, // handle to device
IOCTL_DISK_GET_DRIVE_LAYOUT_EX, // dwIoControlCode
NULL, // lpInBuffer
0, // nInBufferSize
tpDriveLayoutInformationEx, // output buffer
dwInfoSize, // size of output buffer
&dwResult, // number of bytes returned
NULL // OVERLAPPED structure
);
if (0 == bResult) {
printf("DeviceIoControl IOCTL_DISK_GET_DRIVE_LAYOUT_EX FAILED", "");
CloseHandle(hDevice);
return 0;
}
printf("PartitionCount: %s", tpDriveLayoutInformationEx->PartitionCount);
}
system("pause");
return 0;
}
为什么 DeviceIoControl IOCTL_DISK_GET_DRIVE_LAYOUT_EX 失败了? 请帮我。 谢谢。
【问题讨论】:
-
如果操作失败或挂起,返回值为零。要获取扩展错误信息,请调用 GetLastError。
-
我可以正常运行代码,但是将
%s更改为%d,因为PartitionCount是DWORD。希望您可以使用GetLastError查看错误码,以便我们为您提供更多帮助。 -
GetLastError() = 122.