Fixing the Windows API


At some point, you’re bound to run into limitations that have no solution in Basic. If you need low-level information that Basic doesn’t provide, you must go to the operating system.

Win32 has a ReadFile and a WriteFile, but these functions are normally worthless for Visual Basic programmers because the Basic Input and Print statements handle the same tasks in a friendlier manner. But Win32 also provides functions that Basic doesn’t provide.

It’s a common belief that you can speed up your Visual Basic I/O by using API calls such as ReadFile and WriteFile rather than Basic features such as binary I/O with Get and Put. I don’t be­-lieve it. Any slight advantage you might get from the API will be washed out by the cost of disk access. Do you doubt it? Prove me wrong. I won’t argue with facts. If anybody can show me code demonstrating that API calls are significantly faster than Basic I/O for comparable operations, I’ll publicly grovel in ab­ject humiliation. I’ve posted this challenge in several online forums, but so
far nobody has made me bow. There are reasons to use API I/O calls (compatibility with other API calls), but I maintain that speed isn’t one of them.


For example, the Windows API has GetDiskFreeSpace, which tells you the size of a disk and how much of it is used. Basic won’t give you this information. If you want the disk size under 32-bit Visual Basic, you simply write a Declare statement for GetDiskFreeSpace and call it.

Unfortunately, some of the most useful API functions aren’t Basic-friendly. They return pointers to string positions that do you no good in Visual Basic because of Unicode conversion problems. Others return usable data, but in a style unbe­fitting what Visual Basic programmers are accustomed to. The previous edition of this book provided a C DLL to supply a few key functions that Visual Basic allegedly couldn’t handle. But we don’t need no stinkin’ DLL for this edition. If it’s worth doing, it’s worth doing in Basic.

Under Visual Basic version 3, many programmers discovered
a secret tool for getting the free space on a disk. The following
Declare statement gave you a function that returned the free bytes on the current drive:

Declare Function DiskSpaceFree _
Lib “SETUPKIT.DLL” () As Long

This didn’t work in version 4 because the function moved to STKIT432.DLL, and that didn’t work in version 5 because the function moved toVB5STKIT.DLL. You could change the Declare statement for every version, but I suggest you pass. This func­-
tion reports only the free disk space, not the total disk space. Furthermore, there’s no guarantee that the DLL will remain on every user’s disk after setup.