Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: Advanced
Aug 21, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

    lowpart As Long     ' lower 32-bit value
    highpart As Long    ' higher 32-bit value
End 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 _
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 Currency
Dim lpTotalNumberOfBytes As Currency
Dim lpTotalNumberOfFreeBytes As Currency

GetDiskFreeBytesEx2 "C:\", lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, _
Debug.Print "Free Bytes Available to Caller = " & lpFreeBytesAvailableToCaller
Debug.Print "Total Number of Bytes = " & lpTotalNumberOfBytes
Debug.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.
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date