Visual Basic For Windows (VB/Win)

Frequently asked Questions & Answers
Section IX - B
Part 5

Last-modified: 22-Aug-95


[Preface] [General VB] [Common VB Questions] [Advanced VB Questions] [Calling the Win. API & DLL's] [VB/Win & Databases] [Distributing Apps] [General Tips] [VB for Apps (VBA)]

The following symbols indicates new & updated topics:

Hope this makes it easier for Our Regular Readers ;-)


TABLE OF CONTENTS:

E. CALLING THE WINDOWS API AND DLLs IN GENERAL

E. CALLING THE WINDOWS API AND DLLs IN GENERAL


1. What is the Windows API?

The Windows API (Application Program Interface) is a collection of Dynamic-Link Libraries (DLLs) that do most of the common things in Windows. Calls to the Windows API gives you access to routines that do things like drawing menu bars, manipulating bitmaps, playing sound files, and pretty much every other function of Windows.

[Top of Page][Table of Contents][Top of FAQ]


2. How do I call a DLL?

Basically, you declare a DLL procedure which you can call in your VB program which in turn passes data to and/or retrieves data from the DLL. You should read the section of the VB manual that talks about calling DLLs ("Chapter 24 Calling Procedures in DLLs" in the VB 3.0 Programmer's Guide). This chapter covers the basics of using the Windows API DLLs and calling DLLs in general. Beyond that you may want to find a good book on this subject since it is too large to cover here (see the Book Listing in the Appendix of the General FAQ - Part 1).

Don't be too intimidated! Using DLLs (especially many of the Windows API functions) is quite easy, once you learn how to call them. In fact, many of the newer DLLs include VB-compatible modules!

[Top of Page][Table of Contents][Top of FAQ]


3. What about DLL calls that require callbacks?

VB does not support callbacks, but various extensions can help. Dan Appleman's "Visual Basic Programmer's Guide to the Windows API" comes with a floppy disk which code samples and tools. It also includes a VBX which supports the callbacks which many API calls require. Dan is also founder and president of Desaware which sells more extensive tools, including SpyWorks, for VB developers. [Walter Hill (whill@netcom.com)]

[Top of Page][Table of Contents][Top of FAQ]


4. Tips for calling DLLs (such as the Windows API)

Using the BYVAL keyword is critical.
Using it when you're supposed to call by reference and (more common) not using it when you are to give a value to the external function are the single most common mistakes. Wrong calling convention can often result in a General Protection Fault (GPF) or, even worse, corruption of another applications' data.

Check return and parameter types.
For return types, a C function declared as "void" means you use a Sub not a Function. Initialize strings by padding it to the necessary length! If you pass a string that is too short to the API it will happily write past the end of the string and possibly corrupt data.

Use Option Explicit.
A typing error that results in a bug in the VB source will occasionally cause a GPF when you call external code. e. It's a jungle out there! Check parameter values as there is no type checking outside VB. If you make a mistake, you'll often get a GPF.

Save before you run!
You may even need to restart Windows after a GPF, since DLL's often aren't unloaded properly. As a second option you can check out WPS (Windows Process Status) which is distributed with VB/Pro and placed in the CDK directory. This utility allows you to kick out any module (EXE, DLL) from memory (shooting yourself in the foot if you want to. WPS is a nice way to find out what DLLs are actually used, but save your work first!).

[Top of Page][Table of Contents][Top of FAQ]



[Next Section][Previous Section]