Getting Disc Free Space on Windows 95 | CodeGuru

Getting Disc Free Space on Windows 95

Environment: VC6 , win9x When you want to determine disc free space, what function do you use? GetDiskFreeSpace() or GetDiskFreeSpaceEx()? Well if you read the documentation for GetDiskFreeSpace(), the following comment will make you want to use GetDiskFreeSpaceEx(): The GetDiskFreeSpace function returns incorrect values for volumes that are larger than 2 gigabytes. The function caps […]

Written By
CodeGuru Staff
CodeGuru Staff
Jun 6, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: VC6 , win9x

When you want to determine disc free space, what function do you use?

GetDiskFreeSpace() or GetDiskFreeSpaceEx()?

Well if you read the documentation for GetDiskFreeSpace(), the following comment will make you want to use GetDiskFreeSpaceEx():

The GetDiskFreeSpace function returns incorrect values for volumes that are larger than 2 gigabytes. The function caps the values stored into *lpNumberOfFreeClusters and *lpTotalNumberOfClusters so as to never report volume sizes that are greater than 2 gigabytes.
Even on volumes that are smaller than 2 gigabytes, the values stored into *lpSectorsPerCluster, *lpNumberOfFreeClusters, and *lpTotalNumberOfClusters values may be incorrect. That is because the operating system manipulates the values so that computations with them yield the correct volume size.

However, if you read the documentation for GetDiskFreeSpaceEx(), you will notice there is a small problem when targeting Windows95: You cannot guarantee that GetDiskFreeSpaceEx() is available. (Prior to OSR2, you only get GetDiskFreeSpace().)

This means if you need to target Windows95, you need to determine on the fly whether GetDiskFreeSpaceEx() is available or not. If it is, great…if not, you have to use GetDiskFreeSpace().

One way of doing this on the fly, is to load the Kernel32 DLL and determine whether GetDiskFreeSpaceEx() exists or not. If we get a pointer to the function, we can call it…if not, we know we have to use GetDiskFreeSpace():

BOOL GetDiscFreeSpace(LPCTSTR lpszPath, DWORDLONG* pnFree)
{
	BOOL bRet = FALSE;

	// We need to determine whether GetDiskFreeSpaceEx is available by calling LoadLibrary
	// or LoadLibraryEx, to load Kernel32.DLL, and then calling the GetProcAddress to
	// obtain an address for GetDiskFreeSpaceEx.  If GetProcAddress fails, or if
	// GetDiskFreeSpaceEx fails with the ERROR_CALL_NOT_IMPLEMENTED code, we use the
	// GetDiskFreeSpace function instead.
	HINSTANCE hInstance = LoadLibrary("KERNEL32.DLL");

	if(hInstance)	// If we got the library
	{
		FARPROC lpfnDLLProc = NULL;

		lpfnDLLProc = GetProcAddress(hInstance, "GetDiskFreeSpaceExA");

		if(lpfnDLLProc)	// If we got an address to the function
		{
			ULARGE_INTEGER nTotalBytes, nTotalFreeBytes, nTotalAvailable;

			if((*lpfnDLLProc)(lpszPath, &nTotalAvailable, &nTotalBytes, &nTotalFreeBytes))
			{
				*pnFree = nTotalAvailable.QuadPart;
				bRet = TRUE;
			}
		}

		FreeLibrary(hInstance);
	}

	if(!bRet)	// We have to try and use GetDiskFreeSpace()
	{
		ULONG secpercluster, bytespersec, nooffreeclusters, totalnoofclusters;

		if(GetDiskFreeSpace(lpszPath, &secpercluster, &bytespersec,
			&nooffreeclusters, &totalnoofclusters))
		{
			*pnFree = (nooffreeclusters * secpercluster * bytespersec);
			bRet = TRUE;
		}
	}

	return bRet;
}

History

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.