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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS