Listing physical drives installed on my computer [

2019-05-11 01:11发布

Possible Duplicate:
How to list physical disks?

What is the "best way" (fastest) C++ way to List physical drives installed on my computer? Is there a boost library to do that?

2条回答
forever°为你锁心
2楼-- · 2019-05-11 01:53

use GetLogicalDriveStrings() to retrieve all the available logical drives.

#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;
}

or use 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;
}
查看更多
孤傲高冷的网名
3楼-- · 2019-05-11 02:01

One possibility is to use WMI to enumerate the instances of Win32_DiskDrive.

查看更多
登录 后发表回答