【问题标题】:Listing physical drives installed on my computer [duplicate]列出我的计算机上安装的物理驱动器 [重复]
【发布时间】:2012-02-08 01:32:33
【问题描述】:

可能重复:
How to list physical disks?

列出计算机上安装的物理驱动器的“最佳方式”(最快)C++ 方式是什么?有没有一个 boost 库可以做到这一点?

【问题讨论】:

  • 所以,只有提升?您允许使用哪些库?什么操作系统?
  • @Mr.TAMER Windows 是我正在开发的系统,boost 是首选方式,但如果 boost 不这样做,它也会被接受。
  • “驱动器”不是一个跨平台的概念,所以我怀疑你会找到一个跨平台的解决方案。如果您的目标是 Windows,只需使用 Windows API。
  • @smallB - 你可以使用GetLogicalDrives(),看看这个帖子stackoverflow.com/questions/286534/…
  • 哦,抱歉,没注意到。所以我可以向你推荐两种可能的解决方案:你可以使用 WMI 类或参考stackoverflow.com/questions/327718/how-to-list-physical-disks 并使用前面提到的 QueryDosDevice 和 GetLogicalDrives。

标签: c++ windows boost hard-drive


【解决方案1】:

使用GetLogicalDriveStrings() 检索所有可用的逻辑驱动器。

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


DWORD mydrives = 100;// buffer length
char lpBuffer[100];// buffer for drive string storage

int main()
{
      DWORD test = GetLogicalDriveStrings( mydrives, lpBuffer);

      printf("The logical drives of this machine are:\n\n");

      for(int i = 0; i<100; i++)    printf("%c", lpBuffer[i]);


      printf("\n");
      return 0;
}

或使用GetLogicalDrives()

#include <windows.h>
#include <direct.h>
#include <stdio.h>
#include <tchar.h>

// initial value
TCHAR szDrive[ ] = _T(" A:");

int main()
{
  DWORD uDriveMask = GetLogicalDrives();
  printf("The bitmask of the logical drives in hex: %0X\n", uDriveMask);
  printf("The bitmask of the logical drives in decimal: %d\n", uDriveMask);
  if(uDriveMask == 0)
      printf("\nGetLogicalDrives() failed with failure code: %d\n", GetLastError());
  else
  {
      printf("\nThis machine has the following logical drives:\n");
  while(uDriveMask)
    {// use the bitwise AND, 1–available, 0-not available
     if(uDriveMask & 1)
        printf("%s\n",szDrive);
     // increment... 
     ++szDrive[1];
      // shift the bitmask binary right
      uDriveMask >>= 1;
     }
    printf("\n ");
   }
   return 0;
}

【讨论】:

    【解决方案2】:

    一种可能性是使用 WMI 枚举Win32_DiskDrive 的实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      相关资源
      最近更新 更多