Thread: Retrieve total drive capacity

  1. #1
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53

    Retrieve total drive capacity

    I need to write a program that will crawl through a list of specified drives and return the total and free space of each, I'm using the GetFreeSpaceEx function to do this:

    GetDiskFreeSpaceEx(Dnames,NULL,(PULARGE_INTEGER)&t otal,(PULARGE_INTEGER)&free);

    (info from http://msdn.microsoft.com/library/en...reespaceex.asp)

    That guide and a few forum searches suggest that the thridt argument to getfreediskspaceex should return the total size of the drive, however it is clearly not returning this, n the last trial:

    Total=1701228544 bytes
    Free =1966190592 bytes

    so... I have more free space than total

    I'm not running multiple users on this machine, so I don't see that all the quota stuff could be the problem here (though it';s something I'll have to work around later, so if there's anoter function that can do this, please let me know).

    I've compared the value for total, and it doesn't seem to be anywhere near any values for the drive , I don't know what this is actually returning.

    Any help solving this problem would be greatly appreciated

    I'm running on WinXp System, with the Dev-C++ compiler (4.9.7.0) and the actual drive size is 5Gb.

    thanks in advance
    -Mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    From MSDN about the total:

    Pointer to a variable that receives the total number of bytes on the disk that are available to the user associated with the calling thread. This parameter can be NULL.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    Ok, I'm now on a different machine (though still win XP/dev-Cpp setup), and I've done a few more tests. the code seems to run ok on C: but screws up on E and F (E is the system drive in this case)

    here's the source

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int main(int argc, char *argv[])
    {
      __int64 total,free;
      char Dnames[4];
      int n;
      
      for(n=0;argv[1][n]!='\0';n++)
        {
        sprintf(Dnames,"%c:\\",argv[1][n]);
        printf("Drive %s\n",Dnames);
        if(GetDriveType(Dnames) !=DRIVE_FIXED)
          {
          printf("not a fixed drive, skipping");
          }else{
          GetDiskFreeSpaceEx(Dnames,NULL,(PULARGE_INTEGER)&total,(PULARGE_INTEGER)&free);
          printf("TOTAL:%u bytes\n",total);
          printf("FREE :%u bytes\n",free);
          }
        }
      return 0;
    }
    the code takes one argument, a string representing a list of drives to scan, eg

    drivereporter.exe "CEF"

    and the output when run on this computer, with that command line:

    Drive C:\
    TOTAL:106670080 bytes
    FREE :56360960 bytes
    Drive E:\
    TOTAL:1254318080 bytes
    FREE :1890611200 bytes
    Drive F:\
    TOTAL:3878248448 bytes
    FREE :93036544 bytes

    The free values for each drive are correct to an accuracy of a few mb, howevr something very weird is going on with the totals:

    C is a 100mb FAT32 partition, which has turned out correct
    E is the remaining partition on that drive, 37.1 Gb NTFS, system drive
    F is a 16 Gb partition on a 20 Gb drive (FAT 32) the last 4 Gb of this drive is unaccssible to windows (Linux partition).

    there's some pretty weird stuff going on there, but I don't really know enough about it to draw much of a conclusion.

    I've read the posts from this forum ( and from some google searches) and I've seen nothing that would indicate this kindof behaviour.

    thanks for the responses

    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  4. #4
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    sorry about that, thanks for correcting my idiocy...

    using %I64d seems to work correctly

    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with day of the week program
    By Punkakitty in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 06:55 PM
  2. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  3. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 PM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  5. Replies: 4
    Last Post: 04-22-2003, 12:52 PM