列出安装在我的计算机上的物理驱动器

3

可能是重复问题:
如何列出物理硬盘?

有没有最快的C++方法来列出计算机上已安装的物理驱动器?是否有使用boost库的方法可以实现?


1
那么,只用boost吗?你可以使用哪些库?操作系统是什么? - Tamer Shlash
@Mr.TAMER 我正在使用的操作系统是Windows,首选方式是使用boost库,但如果boost无法实现,其他方式也可以被接受。 - smallB
3
“驱动程序”不是一个跨平台的概念,因此我怀疑你会找到一个跨平台的解决方案。如果你的目标是Windows系统,那么就使用Windows API吧。 - ildjarn
3
你可以使用 GetLogicalDrives() 函数,查看这篇文章:https://dev59.com/A3VC5IYBdhLWcg3wdw65。 - Cyclonecode
1
哦,抱歉,没有注意到。我可以提供两个可能的解决方案:您可以使用WMI类或参考https://dev59.com/-HRC5IYBdhLWcg3wVvjL并使用QueryDosDevice和GetLogicalDrives,正如之前提到的那样。 - Volodymyr Rudyi
显示剩余3条评论
2个回答

12

使用 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;
}

0

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