使用IOCTL_DISK_GET_LENGTH_INFO获取硬盘大小
2021-09-07
79
0
使用IOCTL_DISK_GET_LENGTH_INFO获取硬盘大小
char buff[512];
sprintf(buff,"\\\\.\\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) // cannot open the drive
{
MessageBox("打开磁盘失败!","提示",MB_OK|MB_ICONSTOP);
return ;
}
#define IOCTL_DISK_GET_LENGTH_INFO CTL_CODE(IOCTL_DISK_BASE, 0x0017, METHOD_BUFFERED, FILE_READ_ACCESS)
typedef struct _GET_LENGTH_INFORMATION {
LARGE_INTEGER Length;
} GET_LENGTH_INFORMATION, *PGET_LENGTH_INFORMATION;
GET_LENGTH_INFORMATION disklen;
ULONG d;
BOOL ok = DeviceIoControl(hDevice,IOCTL_DISK_GET_LENGTH_INFO,NULL,0,&disklen,sizeof(disklen),&d,NULL);
CloseHandle(hDevice);
CString str;
if(ok)
{
// str.Format("0x%08X%08X B(%d GB)\n",disklen.Length.u.HighPart,disklen.Length.u.LowPart,disklen.Length.QuadPart/1024/1024/1024);
str.Format("%d GB\n",disklen.Length.QuadPart/1024/1024/1024);
}
else
{
MessageBox("获取磁盘大小失败!","提示",MB_OK|MB_ICONSTOP);
}