如何从Windows 7中的驱动器号获取可移动设备的物理驱动器号?

时间:2011-03-31 14:49:00

标签: winapi visual-c++ windows-7

我正在尝试从Windows上的CDROM驱动器的驱动器号中查找物理驱动器号(例如,我需要N中的\\.\PhysicalDriveN以打开块设备以进行读取) 7. This page表示IOCTL_STORAGE_GET_DEVICE_NUMBER应该有效,但是对于C:和D:(其中D:是可移动驱动器)的驱动器号,它返回0,因此不能正确。还建议使用IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,但在D:上的ERROR_INVALID_FUNCTION失败。

我不禁觉得我错过了某个关键概念。

这是我的代码:

#include "stdafx.h"
#include "Windows.h"


void printLastError(){
  DWORD lastError;
  DWORD bytesReturned;
  WCHAR outbuf[2048];

  lastError = GetLastError();

  bytesReturned = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
    NULL, lastError, LANG_USER_DEFAULT, outbuf, 2048, NULL);

  if (bytesReturned > 0){
    wprintf(outbuf);
  } else {
    printf("Error %d while formatting error %d\n", GetLastError(),     lastError);
  }
}

void readDeviceNumberByExtents(HANDLE hFile){
  BOOL ioctlSuccess;
  DWORD bytesReturned;

  VOLUME_DISK_EXTENTS vde;

  ioctlSuccess = DeviceIoControl(hFile,
    IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
    NULL, 0, &vde, sizeof(vde), &bytesReturned, NULL);

    if (ioctlSuccess != 0){
      printf("%d\n", vde.Extents->DiskNumber );
    } else {
      printLastError();
  }
}

void readDeviceNumberByStorage(HANDLE hFile){
  BOOL ioctlSuccess;
  DWORD bytesReturned;

  STORAGE_DEVICE_NUMBER sdn;

  ioctlSuccess = DeviceIoControl(hFile,
    IOCTL_STORAGE_GET_DEVICE_NUMBER,
    NULL, 0, &sdn, sizeof(sdn), &bytesReturned, NULL);

  if (ioctlSuccess != 0){
    printf("%d\n", sdn.DeviceNumber );
  } else {
    printLastError();
  }
}

void runTest(WCHAR* driveName){
  HANDLE driveHandle;
  DWORD diskNumber;

  driveHandle = CreateFile(driveName,
    0,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

  if (driveHandle != INVALID_HANDLE_VALUE){
    wprintf(L"Opened %s\n", driveName);

    printf("Device number by extents: ");
    readDeviceNumberByExtents(driveHandle);
    printf("Device number by storage: ");
    readDeviceNumberByStorage(driveHandle);

    CloseHandle(driveHandle);
  } else {
    printf("Failure!\n");   
  }
}

int _tmain(int argc, _TCHAR* argv[])
{
  runTest(L"\\\\.\\C:");
  printf("\n");
  runTest(L"\\\\.\\D:");

  getc(stdin);
  return 0;
}

...以及我以管理员身份运行它时的输出:

Opened \\.\C:
Device number by extents: 0
Device number by storage: 0

Opened \\.\D:
Device number by extents: Incorrect function.
Device number by storage: 0

2 个答案:

答案 0 :(得分:5)

"\\.\PhysicalDriveN"仅适用于(类似行为)硬盘驱动器,而不是可移动磁盘。如果某些东西像可移动磁盘(或软盘,CD-ROM等),"\\.\X:"打开原始驱动器(其他驱动器不支持分区,因此区分"\\.\x:"和{{1他们不存在)。您通常希望使用"\\.\PhysicalDiskN"来确定您拥有的磁盘类型,并且只有当它表明它是GetDriveType时才会尝试查找驱动器号并使用DRIVE_FIXED

答案 1 :(得分:2)

这是C#.Net代码,但这是我写的工作:

using System.Management; //Add in a reference to this as well in the project settings
public static string GetPhysicalDevicePath(char DriveLetter)
{
    ManagementClass devs = new ManagementClass( @"Win32_Diskdrive");
    {
        ManagementObjectCollection moc = devs.GetInstances();
        foreach(ManagementObject mo in moc)
        {
            foreach (ManagementObject b in mo.GetRelated("Win32_DiskPartition"))
            {
                foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
                {
                    string DevName = string.Format("{0}", c["Name"]);
                    if (DevName[0] == DriveLetter)
                        return string.Format("{0}", mo["DeviceId"]); 
                }
            }
        }
    }
    return "";
}