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 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



Comments

  • Much Simpler Version!

    Posted by Legacy on 03/19/2002 12:00am

    Originally posted by: Jeremy Davis

    A guy called Mike Dunn pointed me to a much easier way...
    
    

    (1) Download latest Platform SDK

    (2) To your stdafx.h add...
    #define COMPILE_NEWAPIS_STUBS
    #define WANT_GETDISKFREESPACEEX_WRAPPER

    (3) #include <newapis.h> where you need to use GetDiskFreeSpaceEx.

    (4) Use GetDiskFreeSpaceEX as you need - eg...
    ULARGE_INTEGER fba, tnob, tnofb;
    GetDiskFreeSpaceEx("c:\\", &fba, &tnob, &tnofb);
    CString str;
    str.Format("%ld", fba.QuadPart / (DWORD)1024 / (DWORD)1024);
    MessageBox(str);

    THIS WORKS ON ALL VERSIONS OF OPERATING SYSTEMS FROM WIN95 TO WINXP!!!!

    Reply
  • Diskspace problem

    Posted by Legacy on 01/20/2002 12:00am

    Originally posted by: Sekhar

    I have the same issue as Sujeect has. I tried the code given by Sung-ug Hong and made the correction as per Olaf Oelsner.
    
    

    I still end up with the VC.exe - Application error which says "Instruction at '0x004010a6' referenced memory ...., memoery could not be written"

    Please help.

    Reply
  • how to get the total hard disk space

    Posted by Legacy on 10/10/2001 12:00am

    Originally posted by: sujeet

    hi there,
    i need some sample code for how to get the total hard disk space on an windows NT 4.0.
    i tried getfreediskspaceEx() but got some error in kernel32.lib that was access violation error
    can u please send me a sample code how to get total hard disk space
    thanx
    sujeet

    Reply
  • Re-make this function (if you have error C2197 code ...)

    Posted by Legacy on 01/15/2001 12:00am

    Originally posted by: Sung-ug Hong

    VC++ 6, Win98
    
    

    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.

    typedef BOOL (__stdcall *_FPREGISTERSERVICEPROCESS)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER

    _FPREGISTERSERVICEPROCESS m_pRegisterServiceProcess;
    HINSTANCE m_hDllInst;

    m_hDllInst = LoadLibrary("Kernel32.DLL");
    if (m_hDllInst) {
    ULARGE_INTEGER nTotalBytes, nTotalFreeBytes, nTotalAvailable;
    m_pRegisterServiceProcess = reinterpret_cast<_FPREGISTERSERVICEPROCESS>(::GetProcAddress(m_hDllInst, "GetDiskFreeSpaceExA"));

    if (m_pRegisterServiceProcess)
    m_pRegisterServiceProcess(lpszPath, &nTotalAvailable, &nTotalBytes, &nTotalFreeBytes);

    *pnFree = nTotalAvailable.QuadPart;
    bRet = TRUE;

    FreeLibrary(m_hDllInst);
    }

    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;
    }

    Reply
  • PB with V5.0 ??

    Posted by Legacy on 01/06/2000 12:00am

    Originally posted by: patrick.schermesser

    Hello
    I use GetDiscFreeSpace in my own Project
    Visual C++ 5.0 Wint NT 4.0 and i have some errors :
    error C2197: 'int (__stdcall *)(void)' : too many actual parameters
    a the line if((*lpfnDLLProc)(lpszPath, &nTotalAvailable, &nTotalBytes, &nTotalFreeBytes))

    ANy ideas ????

    Reply
  • Thanks Geeza

    Posted by Legacy on 10/27/1999 12:00am

    Originally posted by: Jason Hattingh

    Cheers mate - very useful.

    Jason

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds