Why does GetDiskFreeSpaceEx return the wrong amount of free space?


One customer discovered that viewing the properties of a drive from Explorer shows the amount of disk free space significantly lower than the actual amount of free disk space as resported by the disk management snap-in. The command prompt agrees with Explorer and shows the incorrect result. What’s going on?

Not surprisingly, both Explorer and the command prompt use the GetDiskFreeSpaceEx function to get the amount of free disk space. (It’s not surprising because the only two convenient options are GetDiskFreeSpace and GetDiskFreeSpaceEx, and the former exists only for compatibility with older programs who don’t support drives bigger than 2GB.) If you go to the documentation for the GetDiskFreeSpaceEx function, you’ll see that the amount of free disk space reported is not the actual amount of physical free disk space but rather the amount of available physical free disk space as constrained by the user’s quota.

Just saying the word “quotas” to the customer was enough to satisfy them. I guess that was enough of a nudge that they could figure out the rest for themselves.

Comments (17)
  1. ac says:

    so, how would one get the "real" amout of free space?

  2. Mike Dimmick says:

    @ac: GetDiskFreeSpaceEx is basically telling you the size of the largest file you could create, which for most programs is indeed the real amount of free space.

    The documentation for GetDiskFreeSpaceEx tells you exactly how to get the total number of bytes on a volume: call DeviceIoControl with the IOCTL_DISK_GET_LENGTH_INFO parameter.

  3. Grant says:

    [so, how would one get the "real" amout of free space?]

    Raymond already answered that in his article:

    "One customer discovered that viewing the properties of a drive from Explorer shows the amount of disk free space significantly lower than the actual amount of free disk space as resported by the disk management snap-in."

    Oh, you meant programmatically? Well, I’m guessing you use GetDiskFreeSpaceEx all the time because the amount of disk space available to a process is going to be the amount of disk space quota allotted to the user the process is running as.

    In other words, even if you could determine the amount of "real" free disk space, what’s the point in knowing it is 100GB if the user has a quota of 10GB?

    It would sort of like going to your bank and asking how much money they have then trying to buy something for that much when you only have $100 in your own bank account.

  4. Mark Sowul says:

    "It would sort of like going to your bank and asking how much money they have, then trying to buy something for that much when you only have $100 in your own bank account."

    That was beautiful!

  5. Karellen says:

    "If you go to the documentation for the GetDiskFreeSpaceEx function, you’ll see that the amount of free disk space reported is not the actual amount of physical free disk space…"

    Well, going to MSDN[0], it looks like both are reported, the bytes left in the user’s quota in *lpFreeBytesAvailable and the amount of physical free disk space in *lpTotalNumberOfFreeBytes.

    So GetDiskFreeSpaceEx returns the right amount of free space, according to two different metrics. Explorer and the command prompt just happen to display the quota-based one, instead of the one that the user expected to be shown.

    Or do I have the wrong bit of documentation for GetDiskFreeSpaceEx()?

    [0] http://msdn2.microsoft.com/en-us/library/aa364937.aspx

  6. . says:

    Total unused storage capacity, is GetDiskFreeSpaceEx() + GetDiskExpensiveSpaceEx().  "If you have to ask, you can’t afford it."

  7. Karellen says:

    ac > You’d also get the amount of physical free space with GetDiskFreeSpaceEx() – it returns the available space to the user, *and* the amount of physical free space on the disk.

    Each application picks one of those values to display. Looks like explorer and the command prompt (should that be the "dir" command – or does the command prompt show free space by itself as well?) are picking one, and "the disk management snap-in" (?) is picking another.

    For more details, see http://msdn2.microsoft.com/en-us/library/aa364937.aspx

  8. Karellen says:

    Sorry, didn’t mean to post the almost exactly the same thing twice. I thought the aether had swallowed my first one.

    *shakes head in order to clear it*

    Maybe I should have had more than 7 hours combined sleep in the last couple of nights. Things might make more sense then…

  9. Yuhong Bao says:

    BTW, that reminded me of one of my last comments on the article http://blogs.msdn.com/oldnewthing/archive/2007/07/17/3903614.aspx. You may have missed this one:

    "BTW, I remember reading that Windows NT 3.51 and older automatically filled out the uniquifier part of a window handle (the high 16-bit) if it was zeroed. e.g. casting a window handle to a short, like in the Excel XLL function xlGetHwnd before Excel 2007. NT 4.0 stopped doing that."

    I now have a reference for this:

    http://www.microsoft.com/technet/archive/ntwrkstn/support/trblshoot/apint95.mspx

  10. Name required says:

    This is one of the few occasions where I would have thought an enumeration would be enough as a return value: "Loads, don’t worry about it", "Enough", "Stop downloading porn/mp3s", "Very little", and "Not enough".

  11. Yuhong Bao says:

    BTW, how about a “Why does GetDiskFreeSpace return the wrong amount of free space?” that describes how programs truncate the result of NumClusters * BytePerSector * SectorsPerCluster to 32-bit.

    [There’s nothing to write beyond what you already did. -Raymond]
  12. Joe Butler says:

    Grant: “It would sort of like going to your bank and asking how much money they have then trying to buy something for that much when you only have $100 in your own bank account.”

    No one said the process was going to check actural hard disc space and then allocate more than the quota allowed.  So, the analogy, in my opionion, is not helpful.  It would be more like – “oh, that much?  I don’t have enough to buy that thing I wanted.  I will need to negotiate with the bank manager for some more of your money?”

    I.e. if you have an application that needs lots of hard-disc space, say a pro video editing application, it might look at the quota of hard drive space and also see that the real hard drive has lots of space available.  So, it might pop up a message to the user explaining that things would be better if the user could ask the network admin to increase their disc quota.

    [I think your program is trying too hard. Following this logic, Web browsers should also check the NIC vs. bandwidth vs. pipe and say “Your network card and DSL cable are capable of higher speeds. Things would be better if you asked your DSL provider to increase your bandwidth.” -Raymond]
  13. Medinoc says:

    The thing I’m wondering, is:

    IF GetDiskFreeSpace() is obsolete, what is the correct manner of getting the number of clusters in a partition, and the size of each cluster ?

    A defragmentation program would need such information…

  14. Joe Butler says:

    Raymond: "I think your program is trying too hard."

    Possibly.  It was just an example.  I do think that your counter-example is not quite the same, though.  I think most people understand, or at least accept, that Internet connections are slow or fast or are just used to whatever they are given.

    I think if I had authored a disc-consumming application of the sort in my example, I think I would try to probe the user’s system to ensure they were aware of the best configuration – this may give my app an advantage over the competition (I don’t know that though – but I don’t imagine it would hurt it).  Maybe not in version 1.0, but it would be something worth doing, I think.  Isn’t this the same as saying, "your video card is set to 256 colours [and my app is going to look rubbish] – let me increase that for you [so that it looks good]."

  15. Joe Butler says:

    Medonic, XP and, I presume, higher exposes its own defrag API so, presumably you can do all you want via that.  

  16. Yuhong Bao says:

    "Total unused storage capacity, is GetDiskFreeSpaceEx() + GetDiskExpensiveSpaceEx().  "If you have to ask, you can’t afford it.""

    That is just funny.

  17. Darren Winsper says:

    I could imagine a situation where you need an application to monitor the physical disk space.  For example, say you give 100 users a quota of 10GB.  You don’t expect everybody to use the full 10GB, so you don’t have a terrabyte drive set up.  You might have an application monitor the total free disk space and e-mail you when it starts to get low.

Comments are closed.