Link to home
Start Free TrialLog in
Avatar of BigOne
BigOne

asked on

Simple Drivespace Question

What can I do to get the current available space on a specific drive?  What are some pitfalls and tricks I should be aware of?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

standard C++ provides no facilities for obtaining this.  Many OS's however, have OS-specific functions that you can call to obtain this information, for exmple in windows you can use the GetDiskFreeSpace() function.

What OS are you using?
Opps, a little late.
Windows 95:
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.

Windows 95 OSR2 and Windows 98:
The GetDiskFreeSpaceEx function is available on beginning with Windows 95 OEM Service Release 2 (OSR2). The GetDiskFreeSpaceEx function returns correct values for all volumes, including those that are greater than 2 gigabytes.

To determine whether GetDiskFreeSpaceEx is available, call GetModuleHandle to get the handle to Kernel32.dll. Then you can call GetProcAddress.

The following code fragment shows one way to do this:

pGetDiskFreeSpaceEx = GetProcAddress( GetModuleHandle("kernel32.dll"),
                         "GetDiskFreeSpaceExA");

if (pGetDiskFreeSpaceEx)
{
   fResult = pGetDiskFreeSpaceEx (pszDrive,
                (PULARGE_INTEGER)&i64FreeBytesToCaller,
                (PULARGE_INTEGER)&i64TotalBytes,
                (PULARGE_INTEGER)&i64FreeBytes);

// Process GetDiskFreeSpaceEx results.
}

else
{
   fResult = GetDiskFreeSpace (pszDrive,
                &dwSectPerClust,
                &dwBytesPerSect,
                &dwFreeClusters,
                &dwTotalClusters)

// Process GetDiskFreeSpace results.

}

It is not necessary to call LoadLibrary on Kernel32.dll because it is already loaded into every Win32 process's address space.


On Windows NT 4.0 or later, use GetDiskFreeSpaceEx.
Avatar of BigOne

ASKER

Thanks for your help, sorry about the OS, it is NT4.0, my bad.  Good answer.