Declare Function GetDiskFreeSpaceEx Lib "kernel32.dll" Alias "GetDiskFreeSpaceExA" (ByVal lpDirectoryName As String, lpFreeBytesAvailableToCaller As ULARGE_INTEGER, lpTotalNumberOfBytes As ULARGE_INTEGER, lpTotalNumberOfFreeBytes As ULARGE_INTEGER) As Long
GetDiskFreeSpaceEx determines information about the size of a disk. It finds the free space available to the current user, the total disk space, and the amount of free space (all in bytes). Each value is placed into a ULARGE_INTEGER structure which can hold the unsigned 64-bit integer values. (If your programming language supplies an intrinsic unsigned 64-bit integer data type, that can be used instead.)
If an error occured, the function returns 0 (use GetLastError to retrieve the error code). If successful, the function returns a non-zero value.
Until Visual Basic implements an intrinsic unsigned 64-bit integer data type, the ULARGE_INTEGER data structure must be used.
' Display the amount of free space on drive C:. If possible, display the free space
' as a "regular" number. If it is larger than a 32-bit value, display the number in
' hexadecimal becaue Visual Basic does not yet support unsigned 64-bit integers.
Dim userbytes As ULARGE_INTEGER ' receives space available to user
Dim totalbytes As ULARGE_INTEGER ' receives total space
Dim freebytes As ULARGE_INTEGER ' receives free space
Dim buffer As String ' used as buffer to build hexadecimal display string
Dim retval As Long ' return value
' Determine the amount of free space available -- ignore the other values.
retval = GetDiskFreeSpaceEx("C:\", userbytes, totalbytes, freebytes) ' information for C:
' If the value fits into a 31-bit integer (because VB doesn't support unsigned 32-bit integers),
' display its value in base 10. If not, convert it to base 16 (hexadecimal) to display.
If freebytes.HighPart = 0 And (freebytes.LowPart &H80000000) = 0 Then ' can display regularly
Debug.Print "Free Space on C:"; freebytes.LowPart ' no conversion is necessary
Else ' convert the two halves into hexadecimal
buffer = Hex(freebytes.LowPart) ' convert low-order half into hex
' Add leading 0's if it isn't 8 digits long.
If Len(buffer) < 8 Then buffer = String(8 - Len(buffer), "0") & buffer
buffer = Hex(freebytes.HighPart) & buffer ' add high-order word
Debug.Print "Free Space on C: (hex): "; buffer ' display hexadecimal equivalent
End If
Last Modified: July 24, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information