DeviceIOCtrl取得的分区类型总是PARTITION_ENTRY_UNUSED

DeviceIOCtrl获得的分区类型总是PARTITION_ENTRY_UNUSED
获得自己电脑硬盘的分区信息,代码如下:


int DiskIndex=m_ComboUsbDrive.GetCurSel();
CString DiskFileName;
DiskFileName.Format(L"\\\\.\\PhysicalDrive%d",DiskIndex);
//AfxMessageBox(DiskFileName);

HANDLE hDisk=::CreateFile(DiskFileName,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
if (hDisk==INVALID_HANDLE_VALUE)
{   
AfxMessageBox(L"打开磁盘驱动器失败:"+GetLastError());
return;
}
    

//获取分区信息
DWORD DiskBytesRead=0;
DWORD DiskBufferSize=sizeof(DRIVE_LAYOUT_INFORMATION_EX)+sizeof(PARTITION_INFORMATION_EX)*104;
PDRIVE_LAYOUT_INFORMATION pDiskPartInfo=(PDRIVE_LAYOUT_INFORMATION_EX)malloc(DiskBufferSize);
//DRIVE_LAYOUT_INFORMATION_EX DiskPartInfo[20];
//DWORD DiskBufferSize=sizeof(DRIVE_LAYOUT_INFORMATION_EX)*20;
memset(pDiskPartInfo,0,DiskBufferSize);//清空缓冲区
if (pDiskPartInfo==NULL)
{
AfxMessageBox(L"缓冲区分配出错:"+GetLastError());
return;
}
 



BOOL ret=::DeviceIoControl(hDisk,IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
                       NULL,NULL,
   (LPVOID)pDiskPartInfo,
   DiskBufferSize,
   &DiskBytesRead,
   NULL);
DWORD temp;
if (!ret)
{
temp=GetLastError();
}


最后这个pDiskPartInfo这个变量里面应该保存了硬盘的分区信息,
debug的情况如下:
DeviceIOCtrl取得的分区类型总是PARTITION_ENTRY_UNUSED
这个PartitionStyle的值为0,按照MSDN的说法,为0的话是磁盘分区不可用,这个是我笔记本的硬盘,分了3个区,怎么回事,下面的PartitionCount的值也不对,有了解的么?谢了
磁盘 disk layout

------解决方案--------------------
PDRIVE_LAYOUT_INFORMATION pDiskPartInfo=(PDRIVE_LAYOUT_INFORMATION_EX)malloc(DiskBufferSize);

这句代码有错哦!
而且,PartitionStyle为0也不是不可用哦,原话:
引用
PARTITION_STYLE Enumeration

Represents the format of a partition.


Syntax
typedef enum _PARTITION_STYLE {
  PARTITION_STYLE_MBRPARTITION_STYLE_MBRMaster Boot Record (MBR) format. This corresponds to standard AT-style MBR partitions.
   = 0,
  PARTITION_STYLE_GPTPARTITION_STYLE_GPTGUID Partition Table (GPT) format.
   = 1,
  PARTITION_STYLE_RAWPARTITION_STYLE_RAWPartition not formatted in either of the recognized formats—MBR or GPT.
   = 2 
} PARTITION_STYLE;

Constants
PARTITION_STYLE_MBR 
Master Boot Record (MBR) format. This corresponds to standard AT-style MBR partitions.

PARTITION_STYLE_GPT 
GUID Partition Table (GPT) format.

PARTITION_STYLE_RAW 
Partition not formatted in either of the recognized formats—MBR or GPT.

PartitionCount值原文:
引用
PartitionCount 
The number of partitions on the drive. On disks with the MBR layout, this value will always be a multiple of 4. Any partitions that are actually unused will have a partition type of PARTITION_ENTRY_UNUSED.