We’ve been pretending that the ZAPI functions are written in Basic to be called by Basic, but in reality they are written as a C DLL to be called from C, Pascal, Logo, Scheme, and the current language of the month. The ZAPI library couldn’t be a Visual Basic DLL because it needs to support all languages directly, not through COM Automation. Also, many callers won’t have the Visual Basic run-time DLL on their disks. The last thing the author of ZAPI had in mind was making it easy to call API functions from Basic. Internally, ZAPI uses whatever conventions are most efficient (usually taken from C, but sometimes from Pascal).
In order to call a procedure in a DLL, you need either a type library entry or a Declare statement for it. Since Declare statements are written in Basic, I’ll concentrate on them. The Zapem API function, shown in Figure 2-5, serves as a preliminary model for writing and using Declare statements.
Figure 2-5. Declaring and using a DLL function.
A lot is packed into this short bit of code. Take a look at the labeled sections in Figure 2-5, which correspond to the numbered sections here:
Services | DLL |
Common controls | COMCTL32 |
Common dialogs | COMDLG32 |
Drag and drop, icon extraction, Windows 95 shell | SHELL32 |
Graphics Device Interface | GDI32 |
Graphics (3-D lines and surfaces) | OPENGL32 (NT only) |
Graphics (games and animation) | WING32 |
Memory, disks, processes, resources, tasks, modules | KERNEL32 |
Multimedia, sound, MIDI, joysticks, timing | WINMM |
Networks (WNet) | MPR |
Networks (LanMan) | NETAPI32 |
NT Security, Registry, and other advanced services | ADVAPI32 |
Component Object Model (COM) | OLE32 |
Automation and type conversion | OLEAUT32 |
Version checking | VERSION |
Windows, menus, strings, messages | USER32 |
Table 2-1. Windows system DLLs.
That’s the quick introduction to API calls. When you get down to actual coding, though, things get complicated. Every type of argument has its own quirks and patterns, and you must look at each type specifically. Fortunately, the Windows API never uses the Variant, Currency, Single, or Double type. The remaining types fall into patterns that I can discuss one by one.
The COM Automation API uses Variant and other Visual Basic data types. Theoretically, you could use them in declarations for the COM Automation system DLLs or for COM Automation–compatible DLLs that you write. You can even use the Optional and ParamArray attributes in Declare statements. This chapter, however, concentrates on the integer and string types used by the Windows API.