MSDN:DeviceIoControl
与硬件驱动交互时,DeviceIoControl 是常用的windows API。 它把控制码发给制定的硬件驱动,然后获取设备信息或者对设备进行其他操作。 下面是一个小demo
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
TCHAR szDiskPos[128] = L"\\\\.\\PhysicalDrive0"; //ff
HANDLE hDevice = INVALID_HANDLE_VALUE;
int nDiskBufferSize = sizeof(PARTITION_INFORMATION_EX);
PARTITION_INFORMATION_EX * lpDiskPartinfo_ex = (PARTITION_INFORMATION_EX *)malloc(nDiskBufferSize);
DWORD nDiskBytesRead = 0;
memset(lpDiskPartinfo_ex, 0, sizeof(lpDiskPartinfo_ex));
hDevice = CreateFile(szDiskPos, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
BOOL Ret = FALSE;
Ret = DeviceIoControl(hDevice, IOCTL_DISK_GET_PARTITION_INFO_EX, NULL, 0, lpDiskPartinfo_ex, nDiskBufferSize, &nDiskBytesRead, NULL);
if(!Ret)
{
int e = GetLastError();
cout<<strerror(e)<<endl;
}
if(lpDiskPartinfo_ex->PartitionStyle == PARTITION_STYLE_MBR )
{
cout<<"MBR"<<endl;
}else if(lpDiskPartinfo_ex->PartitionStyle == PARTITION_STYLE_GPT )
{
cout<<"GPT"<<endl;
}else if(lpDiskPartinfo_ex->PartitionStyle == PARTITION_STYLE_RAW )
{
cout<<"RAW"<<endl;
}
free(lpDiskPartinfo_ex);
lpDiskPartinfo_ex = NULL;
CloseHandle(hDevice);
cin.get();
return 0;
}
这写这个小demo的时候,犯了一个错误,使用了一个不再支持的控制码IOCTL_DISK_GET_DRIVE_LAYOUT
而在MSDN上也已经警告过了
Note IOCTL_DISK_GET_DRIVE_LAYOUT has been superseded by IOCTL_DISK_GET_DRIVE_LAYOUT_EX, which retrieves layout information for AT and EFI (Extensible Firmware Interface) partitions.
而我却把这句话理解反了,使用IOCTL_DISK_GET_DRIVE_LAYOUT得不到想要的数据,虽然编译是成功的,运行也没有报错,这导致后面浪费了好多时间。