如何获取驱动器字母和名称

3

我想获取驱动器的盘符和名称。我使用了 "DeviceIoControl" 和 "IOCTL_DISK_GET_DRIVE_LAYOUT_EX"。我正在使用 Microsoft Visual C++ Ultimate 版本。

#define wszDrive L"\\\\.\\PhysicalDrive0"

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,                       // device to be queried
                            IOCTL_DISK_GET_DRIVE_LAYOUT_EX, // operation to perform
                            NULL,
                            0,                       // no input buffer
                            pdg,
                            sizeof(*pdg),            // output buffer
                            &junk,                         // # bytes returned
                            NULL);          // synchronous I/O

  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);
  } 
  else 
  {
    wprintf (L"GetDrivePartition  failed. Error %ld.\n", GetLastError ());
  }

  getch();
}

但是当我在执行时遇到了一个错误,即“错误122”。


1
有些东西告诉我,在花费数千美元购买C++集成开发环境之前,你应该先学习C++的基础知识。 - undefined
1个回答

2
我认为您想说的是错误代码122而不是22。该错误是ERROR_INSUFFICIENT_BUFFER。如文档所述,您需要分配一个更大的缓冲区并重试。
关键在于该结构体是可变大小的。您需要分配足够大的动态内存来保存所有分区的信息。
像这样的东西应该可以让您朝着正确的方向前进:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define wszDrive L"\\\\.\\PhysicalDrive0"

BOOL GetDriveParition(LPWSTR wszPath, DRIVE_LAYOUT_INFORMATION_EX *pdg, size_t size)
{
  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,                       // device to be queried
                            IOCTL_DISK_GET_DRIVE_LAYOUT_EX, // operation to perform
                            NULL,
                            0,                       // no input buffer
                            pdg,
                            size,            // output buffer
                            &junk,                         // # bytes returned
                            NULL);          // synchronous I/O

  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

  size_t size = sizeof(DRIVE_LAYOUT_INFORMATION_EX) + 10*sizeof(PARTITION_INFORMATION_EX);
  pdg = (DRIVE_LAYOUT_INFORMATION_EX*) malloc(size);
  bResult = GetDriveParition (wszDrive, pdg, size);

  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);
  } 
  else 
  {
    wprintf (L"GetDrivePartition  failed. Error %ld.\n", GetLastError ());
  }

  free(pdg);
}

我已经将malloc函数的返回值强制转换了,因为您声明使用C++编译器。


我做了,并且我遇到了这个错误:"无法将 'void *' 转换为 'DRIVE_LAYOUT_INFORMATION_EX *'",发生在以下代码行:pdg = malloc(size); - undefined
好的,你正在使用C++。我的代码是C语言的。将我的代码编译为C语言,它将能够编译通过。或者强制转换malloc的返回值。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接