GetDiskFreeSpace(), GetDiskFreeSpaceEx()
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Target platform: none, just for learning
Header file: Standard and Windows
Additional library: Windows Platform SDK
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: non-CLR or unmanaged
To do: Finding disk free space
To show: Using GetDiskFreeSpace(), GetDiskFreeSpaceEx() functions
#include <windows.h>
#include <stdio.h>
// You may want to try wmain() version
int main(void)
{
// A pointer to a null-terminated string that specifies a directory on a disk.
// If this parameter is NULL, the function uses the root of the current disk.
// If this parameter is a UNC name, it must include a trailing backslash,
// for example, \\MyServer\MyShare\. This parameter does not have to specify the
// root directory on a disk. The function accepts any directory on a disk. Here we are using NULL, using the root of the current disk
LPCWSTR pszDrive = NULL;
BOOL test, fResult;
// 64 bits integer, low and high bytes
__int64 lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes;
DWORD dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters;
// If the function succeeds, the return value is nonzero. If the function fails, the return value is 0 (zero).
test = GetDiskFreeSpaceEx(
pszDrive,
(PULARGE_INTEGER)&lpFreeBytesAvailable,
(PULARGE_INTEGER)&lpTotalNumberOfBytes,
(PULARGE_INTEGER)&lpTotalNumberOfFreeBytes
);
// printf("Drive to be checked: %s\n", pszDrive);
printf("\nUsing GetDiskFreeSpaceEx()...\n");
// Check the return value
printf("The return value: %d, error code: %d\n", test, GetLastError());
printf("Total number of free bytes available for user-caller: %ul\n", lpFreeBytesAvailable);
printf("Total number of bytes available for user: %ul\n", lpTotalNumberOfBytes);
// Just straight to the free bytes result
printf("Total number of free bytes on disk: %ul\n", lpTotalNumberOfFreeBytes);
// If the function succeeds, the return value is nonzero. If the function fails, the return value is 0 (zero).
fResult = GetDiskFreeSpace(pszDrive,
&dwSectPerClust,
&dwBytesPerSect,
&dwFreeClusters,
&dwTotalClusters);
printf("\nUsing GetDiskFreeSpace()...\n");
printf("The return value: %d, error code: %d\n", fResult, GetLastError());
printf("Sector per cluster = %ul\n", dwSectPerClust);
printf("Bytes per sector = %ul\n", dwBytesPerSect);
printf("Free cluster = %ul\n", dwFreeClusters);
printf("Total cluster = %ul\n", dwTotalClusters);
// Using GetDiskFreeSpace() need some calculation for the free bytes on disk
printf("Total free bytes = %ul\n", (dwFreeClusters*dwSectPerClust*dwBytesPerSect));
return 0;
}
Output example:
Using GetDiskFreeSpaceEx()...
The return value: 1, error code: 0
Total number of free bytes available for user-caller: 49754112l
Total number of bytes available for user: 1897263104l
Total number of free bytes on disk: 49754112l
Using GetDiskFreeSpace()...
The return value: 1, error code: 0
Sector per cluster = 8l
Bytes per sector = 512l
Free cluster = 2109299l
Total cluster = 2560351l
Total free bytes = 49754112l
Press any key to continue . . .