1

我有一个硬盘,上面有 3 个分区。当我使用 IOCTL_DISK_GET_DRIVE_LAYOUT_EX 时,该对象(在我的代码中是“pdg”对象)仅返回数组 1 分区信息,即使它说找到了 4 个分区。我缺少什么以便 partitionEntry(必须使用调试器来调试对象 pdg 的 partitionentry)显示所有 3 个分区。我已经到处寻找一些信息,但无法让它发挥作用。不同的论坛,msdn ...

下面是我的代码

#define UNICODE 1
#define _UNICODE 1

#include <windows.h>
#include <winioctl.h>
#include <stdio.h>

#define wszDrive L"\\\\.\\PhysicalDrive3"

BOOL GetDrive(LPWSTR wszPath)
{
  HANDLE hDevice = INVALID_HANDLE_VALUE;  // handle to the drive to be examined 
  BOOL bResult   = FALSE;                 // results flag
  DWORD junk     = 0;                     // discard results
  DWORD hr;

  DWORD szNewLayout = sizeof(DRIVE_LAYOUT_INFORMATION_EX) + sizeof(PARTITION_INFORMATION_EX) * 4 * 25 ;
  DRIVE_LAYOUT_INFORMATION_EX *pdg = (DRIVE_LAYOUT_INFORMATION_EX*) new BYTE[szNewLayout];

  hDevice = CreateFileW(wszPath,          // drive to open
                        GENERIC_READ|GENERIC_WRITE,                // no access to the drive
                        FILE_SHARE_READ | // share mode
                        FILE_SHARE_WRITE, 
                        NULL,             // default security attributes
                        OPEN_EXISTING,    // disposition
                        0,                // file attributes
                        0);            // do not copy file attributes

  if (hDevice == INVALID_HANDLE_VALUE)    // cannot open the drive
  {
    hr = GetLastError();
      return (FALSE);
  }

  bResult = DeviceIoControl(hDevice,                       // device to be queried
                            IOCTL_DISK_GET_DRIVE_LAYOUT_EX, // operation to perform
                            NULL, 0,                       // no input buffer
                            pdg, szNewLayout,// sizeof(*pdg)*2,            // output buffer
                            &junk,                         // # bytes returned
                            (LPOVERLAPPED) NULL);          // synchronous I/O
  if(!bResult)
  {    
     hr = GetLastError();

     LPTSTR errorText = NULL;
     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errorText, 0, NULL);
     wprintf(L"Error",   errorText);
  }
  CloseHandle(hDevice);

  return (bResult);
}


int wmain(int argc, wchar_t *argv[])
{

  BOOL bResult = FALSE;      // generic results flag

  bResult = GetDrive(wszDrive);

  system ("pause");

  return ((int)bResult);
}

谢谢

4

1 回答 1

2

其他分区数据在那里,但调试器不显示。

DRIVE_LAYOUT_INFORMATION_EX.PartitionEntry 被声明为长度为 1 的数组,但实际上是动态分配的以匹配 PartitionCount。

在 之后设置断点DeviceIoControl,右键单击pdg并选择 QuickWatch...,然后将表达式更新为pdg->PartitionEntry[1],然后 [2] 等以检查其他分区。

或者,添加一个循环来遍历PartitionEntry数组并打印结果:

for( int i = 0; i < pdg->PartitionCount; i++ ) {
    printf( "partition %d: %d\n", i, pdg->PartitionEntry[i].PartitionStyle);
}
于 2013-08-21T17:38:07.253 回答