VISUAL BASIC

 PC Hardware  |  PC Software  |  Networking  |  Web Designs  |  News Letter   |  Home

Visual Basic  |  C++  |  Java

CALLING THE WINDOWS API AND DLLs IN GENERAL

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.

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!

What about DLL calls that require callbacks?

VB 5.0 supports real callback functions, and full (?) access to Windows API functions. See example under AddressOf operator in help/docs.

VB 4.0 and up support callbacks with OLE automation only.VB 4.0 can also create OLE DLLs with VB.

VB 3 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. Dan's update for Windows 95 is called The Visual Basic Programmer's Guide To the Win32 API.
[Walter Hill <whill@netcom.com>]

Tips for calling DLLs (such as the Windows API)

  1. 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.
  2. 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.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.
  3. 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!).

DISTRIBUTING VISUAL BASIC APPLICATIONS

What are some tips for using Setup Wizard?

Setup Wizard for VB 4 and 5 is finally a fully capable product.

Applies to VB 3.0: There were loads of bugs in the setup utilities supplied with VB3. Be sure to get the newest version of SETUPKIT (usually called SETUPK.EXE or -.ZIP). It is available from the sources listed in the beginning of this document, and in the General FAQ.

Alternatively, if you have the older versions, you may have to manually remove the line referring to OLE2UI.DLL in the file SETUPWIZ.INI. See later in this document for dates of newest files on ftp.microsoft.com.

 

 

For more details contact the webmaster