January 28, 2021
Hot Topics:

Visual Basic 6 Win32 API Tutorial

  • By James Limm
  • Send Email »
  • More Articles »

We've covered the majority of issues, problems, and errors that most VB programmers run into when using the API calls. The rest of them are pretty minor, so I've lumped them together into this "Other" category.

Currency Variables

The Currency data type is only valid inside of VB; no API calls that I'm aware of can handle it. If you need to pass in information from a Currency variable to a DLL, convert it to a Double or a Long. You may lose some precision, but that's the best you can do now.

Single and Double Types

The Single data type maps to the float type in C, and the Double type maps to the double type in C. However, none of the Win32 calls use floats or doubles, so unless you're using a custom DLL written in-house, you won't have to worry about it in this book.

Pointers to Functions

Some API calls need a function to call back to inform the calling application of some event. We'll address this (no pun intended) in chapters 7 and 8.

Window Handles

Most of the graphical API calls need a window handle to change something about a particular window. Thankfully, all of the forms and most of the controls (remember, most controls are windows) have a hWnd property, so you can just pass in that value straight into the API (by the way, window handles are declared using the Long data type).

Boolean Return Values

Watch out if a function says it returns zero on error, and nonzero on success. This doesn't translate to VB's True/False Boolean values, where False does equal 0, but True is equal to -1. Use the CBool function on these return values, since CBool will return True for any nonzero value.


Variants are used with any OLE or COM API calls, but none of the Win32 calls use this type. Therefore, I would strongly suggest avoiding using variants with Win32 calls.

Revisiting the Gotcha Function

Remember that evil C programmer that tried to fool us on April 1st? Well, let's take a look at that call again:

Declare Function Gotcha Lib _
"HaHa" (WatchOut as Integer) as String

Two big problems should immediately jump out at you:

  • We're passing in an Integer data type, but the call may try to put information into our variable that will make it overflow.
  • We can't return a String data type; we have to return a Long that points to a String data type, and use CopyMemory to get that information into a string variable that we can use.

Now that you know there are pitfalls waiting for you, change that employee's salary in the corporate database appropriately

Page 8 of 13

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

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