2

我目前正在尝试编写一个 C++ 程序来自动检索有关示例硬盘驱动器映像的分区的信息,这些信息是磁盘上的分区数以及每个分区的起始扇区、大小和文件系统类型.

在这一点上,我很确定实现这一点的最佳方法是通过 MSDN 函数,微软的内置命令。我正在尝试使用“IOCTL_DISK_GET_DRIVE_LAYOUT_EX”函数,但根据我的获取错误调用,我的函数不正确。当我调试程序时,在“IOCTL_DISK_GET_DRIVE_LAYOUT_EX”调用之后,布尔值似乎也没有改变,这意味着它没有返回 bResult 值。

我正在使用 Microsoft Visual C++ Express Edition。如果人们可以看看我的代码并告诉我他们认为我做错了什么,那将不胜感激。

#define UNICODE 1
#define _UNICODE 1

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

#define wszDrive L"\\\\.\\PhysicalDrive6"

BOOL GetDriveParition(LPWSTR wszPath, DRIVE_LAYOUT_INFORMATION_EX *pdg)
{

  HANDLE hDevice = INVALID_HANDLE_VALUE;  // handle to the drive to be examined 
  BOOL bResult   = FALSE;                 // results flag
  DWORD junk     = 0;                     // discard results


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

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

bResult =  DeviceIoControl( 
                  hDevice,                        // handle to device
                  IOCTL_DISK_GET_DRIVE_LAYOUT_EX, // dwIoControlCode
                  NULL,                           // lpInBuffer
                  0,                              // nInBufferSize
                  pdg,                            // lpOutBuffer
                  sizeof(*pdg),                   // nOutBufferSize
                  &junk,                          // lpBytesReturned
                  NULL);                          // lpOverlapped

CloseHandle(hDevice);

return (bResult);


}

int wmain(int argc, wchar_t *argv[])
{
DRIVE_LAYOUT_INFORMATION_EX pdg; // disk drive partition structure
  BOOL bResult = FALSE;      // generic results flag

  bResult = GetDriveParition (wszDrive, &pdg);

  if (bResult) 
  {
    wprintf(L"Drive path            = %ws\n",   wszDrive);
    wprintf(L"Partition Style       = %I64d\n", pdg.PartitionStyle);
    wprintf(L"Partition Count       = %ld\n",   pdg.PartitionCount);

    system("Pause");
  } 
  else 
  {
    wprintf (L"GetDrivePartition failed. Error %ld.\n", GetLastError ());
    system("Pause");
  }

  return ((int)bResult);
}
4

2 回答 2

4

DRIVE_LAYOUT_INFORMATION_EX是一个奇怪的结构。它被定义为

struct {
  DWORD                    PartitionStyle;
  DWORD                    PartitionCount;
  union {
    DRIVE_LAYOUT_INFORMATION_MBR Mbr;
    DRIVE_LAYOUT_INFORMATION_GPT Gpt;
  };
  PARTITION_INFORMATION_EX PartitionEntry[ 1 ];
}

但通常PartitionEntry被视为一个更大的数组,带有PartitionCount条目。这类似于 C99 VLA 机制。由于您只分配了sizeof(*pdg)字节,因此即使是第二个 PartitionEntry 也没有空间。

C++ 破解:

struct ExtraEntries : DRIVE_LAYOUT_INFORMATION_EX
{
   PARTITION_INFORMATION_EX PartitionEntry[ 9 ]; // Or some other reasonable value
};
于 2013-03-15T14:37:54.837 回答
0

即使这篇文章有点老了,我还是找到了另一种方法来获得一个完全填充的 PartitionEntry 而不创建一个棘手的结构:

根据这篇文章:如何调用 DeviceIoControl 来检索它需要的内存量?

DRIVE_LAYOUT_INFORMATION_EX dli;
DWORD bytesReturned = 0;

if (!DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, NULL, 0, (void*)&dli, sizeof(dli), &bytesReturned, NULL))
{
    // Check last error if not ERROR_INSUFFICIENT_BUFFER then return
    int nError = GetLastError();
    if (nError != ERROR_INSUFFICIENT_BUFFER)
    {
        // std::cout << "DeviceIoControl() Failed: " << nError << std::endl;
        CloseHandle(hDevice);
        return false;
    }

    // Allocate enough buffer space based of the value of Partition Count:
    size_t size = offsetof(DRIVE_LAYOUT_INFORMATION_EX, PartitionEntry[dli.PartitionCount]);
    std::vector<BYTE> buffer(size);

    if (!DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, NULL, 0, (void*)buffer.data(), size, &bytesReturned, NULL))
    {
        nError = GetLastError();
        // std::cout << "DeviceIoControl() Failed: " << nError << std::endl;
        CloseHandle(hDevice);
        return false;
    }

    const DRIVE_LAYOUT_INFORMATION_EX& result = *reinterpret_cast<const DRIVE_LAYOUT_INFORMATION_EX*>(buffer.data());
    // Here all parition entry are populated ...
    // TO DO... Do your stuff with result
    
}
else
{
    // Call succeeded; dli is populated with a signle partition entry
    // TO DO... Do your stuff with dli
}
于 2021-07-16T14:45:06.617 回答