views:

455

answers:

3

I'm trying to figure out the available disk space programmatically in windows. For this, I need to first get a list of the available drives, then check which of those are local drives and then query the available bytes on each local drive.

I'm a bit stuck on the first part, where the API presents two functions:

  1. GetLogicalDrives (http://msdn.microsoft.com/en-us/library/aa364972%28VS.85%29.aspx) which gives you a DWORD with the bits set (bit 0 if drive A is present, bit 1 if drive B etc)
  2. GetLogicalDriveStrings (http://msdn.microsoft.com/en-us/library/aa364975%28VS.85%29.aspx) which gives you the actual strings.

Now, although I'll be using strings later on, I'd prefer using the first option for querying. However, on my system a DWORD is typedef-ed to "unsigned long", which is 4 bytes, whereas drive letters only range A-Z (26 - i think - characters). Obviously, one can define more than 26 drives on their system (however unlikely they are to do so) - so I was wondering if there was any convention for those drives. Can someone point me to a resource on this?

Thanks.

+1  A: 

You could use WMI. The following WMI query should list all drives:

SELECT * FROM Win32_DiskDrive
Vinko Vrsalovic
I'm using gcc under mingw, so I don't think I have access to WMI. `GetLogicalDriveStrings` does the same thing, but it is not what I asked.
laura
True, I misread. Sorry
Vinko Vrsalovic
+1  A: 

It it not sufficient to enumerate MS-DOS drives (there can be at most 26 of them, by the way, although each can be bound twice, once globally and once locally in your session), a volume can, for example, be mounted to a directory. What you want is probably to enumerate all volumes in the system, using FindFirstVolume et al. Take a look at the associated MSDN example.

avakar
+1  A: 
  1. DWORD is always 4 bytes, regardless of the system (it's a Win32 type).

  2. The maximum for dirve letters in Windows is 26. Because English alphabet has only 26 letters :). However, Windows allows two ways to mount a volume:

    • to a drive letter
    • to a directory (on an NTFS volume). You can mount one volume to multiple locations (but no more than one drive letter, IIRC). A GUI for this task is presented by Control Panel -> Administrative Tools -> Computer Management -> Disk Management.

You can enumerate all volumes and their mount points as described in this article.

atzz
(1) so it is, i stand corrected.
laura