• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

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?
0
BigOne
Asked:
BigOne
1 Solution
 
jkrCommented:
You didn't mention the OS (thus, a comment). For Win32, the functions of choice are 'GetDiskFreeSpace()' and 'GetDiskFreeSpaceEx()'.

Pitfalls: None, except that NTFS can handle files >4GB and that 'GetDiskFreeSpaceEx()' isn't available on the first release of Win95...
0
 
nietodCommented:
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?
0
 
nietodCommented:
Opps, a little late.
0
 
chensuCommented:
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.
0
 
BigOneAuthor Commented:
Thanks for your help, sorry about the OS, it is NT4.0, my bad.  Good answer.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now