使用IOCTL_DISK_GET_DRIVE_GEOMETRY获取硬盘的布局
2021-09-07
165
0
typedef struct _DISK_GEOMETRY {
LARGE_INTEGER Cylinders;
MEDIA_TYPE MediaType;
ULONG TracksPerCylinder;
ULONG SectorsPerTrack;
ULONG BytesPerSector;
} DISK_GEOMETRY, *PDISK_GEOMETRY;
BOOL CDisk::GetDiskInfoById(int nId, DISK_GEOMETRY* dg)
{
TCHAR buff[512];
_stprintf_s(buff, TEXT("\\\\.\\PhysicalDrive%d"), nId);
HANDLE hDevice = CreateFile(buff, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
return FALSE;
}
ULONG rtn;
BOOL bResult = DeviceIoControl(hDevice,
IOCTL_DISK_GET_DRIVE_GEOMETRY,
NULL, 0,
dg, sizeof(*dg),
&rtn,
(LPOVERLAPPED)NULL);
CloseHandle(hDevice);
return bResult;
}