Use Currency instead of LARGE_INTEGER values

Use Currency instead of LARGE_INTEGER values

A few API calls require that you pass one or more LARGE_INTEGER arguments. These are 64-bit integer values, defined as follows:

Type LARGE_INTEGER    lowpart As Long     ' lower 32-bit value    highpart As Long    ' higher 32-bit valueEnd Type

Unfortunately, working with LARGE_INTEGER values in VB isn’t easy, because you have to perform a lot of multiplications and divisions. A better solution is to pass the API function a Currency variable. Currency values are 64-bit integers, but when you print their values, VB scales them by a factor of 10,000 – to account for the 4 decimal digits that all Currency have.

As an example of this technique, let’s see how you can retrieve the number of free bytes on the Windows versions that support partitions with more than 2G. In this case, you must call the GetDiskFreeSpaceEx API function, whose Declare is:

Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias _    "GetDiskFreeSpaceExA" (ByVal lpDirectoryName As String, _    lpFreeBytesAvailableToCaller As LARGE_INTEGER, _    lpTotalNumberOfBytes As LARGE_INTEGER, lpTotalNumberOfFreeBytes As _    LARGE_INTEGER) As Long

To apply the trick, you must define an aliased Declare, as follows:

Private Declare Function GetDiskFreeSpaceEx2 Lib "kernel32" Alias _    "GetDiskFreeSpaceExA" (ByVal lpDirectoryName As String, _    lpFreeBytesAvailableToCaller As Currency, lpTotalNumberOfBytes As Currency, _    lpTotalNumberOfFreeBytes As Currency) As Long

and then call it using this code:

Dim lpFreeBytesAvailableToCaller As CurrencyDim lpTotalNumberOfBytes As CurrencyDim lpTotalNumberOfFreeBytes As CurrencyGetDiskFreeBytesEx2 "C:", lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, _    lpTotalNumberOfFreeBytesDebug.Print "Free Bytes Available to Caller = " & lpFreeBytesAvailableToCallerDebug.Print "Total Number of Bytes = " & lpTotalNumberOfBytesDebug.Print "Total Number of Free Bytes = " & lpTotalNumberOfFreeBytes

UPDATE: The original example had a few typo, that have been fixed now. Thanks to Kenneth Ives for spotting the problem.

See also  How to Avoid Money Transfer Scams  

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist