0

I think I set up the GetDiskFreeSpace() function correct but my output does not look right.

#define wszDrive L"\\\\.\\PhysicalDrive0"

DWORD dwSectPerClust,
        dwBytesPerSect,
        dwFreeClusters,
        dwTotalClusters;

    BOOL diskClust = 0;
    diskClust = GetDiskFreeSpace(wszDrive,
        &dwSectPerClust,
        &dwBytesPerSect,
        &dwFreeClusters,
        &dwTotalClusters);
    cout << "Sectors Per Cluster: " << dwSectPerClust << endl;
    cout << "Bytes Per Sector: " << dwBytesPerSect << endl;
    cout << "Free Clusters: " << dwFreeClusters << endl;
    cout << "Total Clusters: " << dwTotalClusters << endl;

I don't get any errors or warnings but when I build and run this is how my output looks completely wrong:

Sectors Per Cluster: 3435973836
Bytes Per Sector: 3435973836
Free Clusters: 3435973836
Total Clusters: 3435973836

I am not sure why the variables might already be initialized? I also added an if statement for error handling:

if (diskClust == 0){ cout << "GetDiskFreeSpace failed with error: "<<GetLastError() << endl; }

With an output of GetDiskFreeSpace failed with error: 1

dspaces1
  • 193
  • 1
  • 3
  • 15
  • `GetDiskFreeSpace` was really designed for much smaller drives than those we have today. Use [`GetDiskFreeSpaceEx`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937.aspx) instead. – Jerry Coffin Jun 10 '14 at 17:53
  • Roman R. is bang on. It looks like the call to `GetDiskFreeSpace` is failing and not putting anything in your DWORDs. Check the value of `diskClust`; it will likely indicate a failure. – nneonneo Jun 10 '14 at 17:56
  • Should the drive name really be in a `L"..."`? – laune Jun 10 '14 at 18:03
  • Yea I checked the value of diskClust and got an error. How can I fix this issue? This is the first time I used the variables. I tried different variables and got the same output. – dspaces1 Jun 10 '14 at 18:11
  • What is the error code? – drescherjm Jun 10 '14 at 18:13
  • if (diskClust == 0){ cout << "GetDiskFreeSpace failed with error: "< – dspaces1 Jun 10 '14 at 18:16
  • 2
    I don't think this should be called a duplicate. Yes the output is 0xccccccccc but that is not the real problem. I guess the OP can rephrase the question to ask why GetDiskFreeSpace() is failing and completly eliminate the 0xcccccccc from the discussion by just checking the return code like the comment above. – drescherjm Jun 10 '14 at 18:23
  • 1
    As to the **why** your call is failing, it's simply because you're using `\\.\PhysicalDrive0`, which isn't what's supposed to be used; you're supposed to use the drive mount point e.g. `c:`, or if you have it, the volume name e.g. `\\?\Volume{21d48af6-62f8-4236-b97d-a14eb1801a06}`, or if it's a UNC share `\\server\share` – Anya Shenanigans Jun 10 '14 at 19:41
  • @Petesh I replaced "\\\\.\\PhysicalDrive0" with "C:" and it worked like a charm. Thanks! – dspaces1 Jun 10 '14 at 20:02
  • 1
    It's amazing how many people ignore error checking. Don't do that. – David Heffernan Jun 10 '14 at 23:51

0 Answers0