3

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?

Community
  • 1
  • 1
smallB
  • 16,662
  • 33
  • 107
  • 151
  • 1
    So, only boost? what libraries are you allowed to use? what operating system? – Tamer Shlash Jan 11 '12 at 17:16
  • @Mr.TAMER Windows is system I'm working on, boost is the preferred way but other in case boost doesn't do it will get accepted too. – smallB Jan 11 '12 at 17:17
  • 3
    "Drives" are not a cross-platform concept, so I doubt you're going to find a cross-platform solution to this. Just use the Windows API if you're targeting Windows. – ildjarn Jan 11 '12 at 17:19
  • 3
    @smallB - You could use `GetLogicalDrives()`, check out this post http://stackoverflow.com/questions/286534/enumerating-all-available-drive-letters-in-windows – Cyclonecode Jan 11 '12 at 17:19
  • Please, specify target OS and what exactly do you need: logical drivers under Windows, partitions or physical drives. – Volodymyr Rudyi Jan 11 '12 at 17:20
  • @volodymyr as I've answered 7 minutes before you asked me for it (see my reply to Mr. TAMER) the OS is Windows – smallB Jan 11 '12 at 17:43
  • 1
    Oh, sorry, didn't notice that. So I can propose you two possible solutions: You can use WMI classes or refer to http://stackoverflow.com/questions/327718/how-to-list-physical-disks and use QueryDosDevice and GetLogicalDrives as it was mentioned earlier. – Volodymyr Rudyi Jan 11 '12 at 17:49
  • File system is different concept than physical drive, even if it's closely related. As Boost:Filesystem doesn't deal with drives, you can't use it for that purpose. [See this post](http://stackoverflow.com/q/327718/11343) for a solution. – CharlesB Jan 11 '12 at 17:24

2 Answers2

12

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;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
0

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

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111