The magazine of the Melbourne PC User Group

Seeing the Basics: VB and the Windows API
Tony Stevenson

Visual Basic is a powerful development language and its functionality can be extended in two ways. The first way, is by using custom controls. These are software components that are easily incorporated into VB applications. There are custom controls for almost any task imaginable, including numerical analysis, speech recognition, image and document support. Custom controls for VB 3.0 are called VBXs. Those for VB 4.0 are called OCXs.

The second way VB can be extended is through the Windows Application Programming Interface (API).

What is an API?

In general terms, an API is system software for an operating system or environment, which consists of a standardised set of functions and procedures. Programmers can call these functions and procedures from their programs to gain extra functionality. Because programmers do not have to write this code themselves, they save time. The system also provides a standard and well documented way of working.

The Windows API?

The Windows API is the set of functions and procedures available in Windows. They can be used by all sorts of programmers, including those working in VB.

There are hundreds of procedures and functions in the Windows API. They cover all aspects of Windows programming: window and mouse control, drawing capabilities, menus, fonts, printing, memory management, the clipboard, and so on.

The most often required functionality of the Windows API already exists in the Visual Basic development environment. This is why VB is regarded as one of the most powerful ways of creating Windows applications. Most of the API functions and procedures that have not been included in VB itself, can still be called from VB applications. The small percentage that cannot, are rarely required.

Dynamic Link Libraries

In conventional programming languages, functions and procedures external to a program are usually loaded into the executable version of the program at compile and link time. This approach makes programs bigger, because each program's executable version "contains" a copy of the external functions and procedures.

In contrast, the combination of Windows and VB works differently from the way programs work in the non-Windows programming environment. There is only one copy of the Windows functions and procedures, and these are contained in the Windows API as Dynamic Link Libraries (DLLs).

These DLLs are resident in memory whenever Windows is running. When a VB program needs to make use of one of Windows' API functions and procedures, it is "dynamically linked", with the linking process resolved at run time. The particular API function or procedure is still available to other programs which might need it. As a result of this sharing arrangement, programs are smaller than if each VB program had to have their own individual copies of the program elements for function or procedure.

The API Viewer

In the 32-bit Professional edition of VB 4.0, there is a folder named "winapi." This folder contains the text file WIN32API.TXT, along with other files. The WIN32API.TXT file holds the "Constants," "Declares" and "Types" for the 32-bit versions of the Windows API functions. The file APILOD.TXT in the same folder contains more information.

Double-clicking WIN32API.TXT loads it using Windows 95 WordPad - it is too large to open using Notepad. But there's a better and more efficient way of getting API information into your application than using the "Copy" and "Paste" functions on the WIN32API.TXT file.

Double-clicking "APILOD32.EXE" loads the API Viewer. With it you can look at the Constants, Declares, and Types of the API. It works as follows:

Click on the "File" menu, then the menu option "Load Text File ...".

Select the WIN32API.TXT item in the list box and click the "Open" command button.

You'll be asked if you would like to convert the API file to a database. If you answer yes, the information will load more quickly, so click the "Yes" command button. Next you will be asked to save it, with the recommendation to save it as WIN32API.MDB. Accept this recommendation by clicking the "Save" command button. You'll only have to do this the first time.

The next time you use the API Viewer you can select the "File" menu option. Then the "Load Database File ..." option. From the list that appears you select the item "WIN32API.MDB".

Once the database file information has been loaded, use the API Viewer to select one of the categories of Constants, Declares, or Types - and the items available in each category will now be displayed.

You can choose one of these and add it to a list of selected items. Use the "Remove" command button to delete wrongly selected items. Once you are happy with your selections, click the "Copy" command button to place the items you have selected onto the clipboard. From the clipboard, you can paste the copied information into your VB application.

The steps involved in coding a API call

The following sample VB application uses the "GetDeviceCaps" API call to demonstrate using the Windows API. The example shows how to include the code for an API call. It's not not concerned with the functionality of the call.

The sample application only requires one form and one code module. Using the API Viewer as described above, place the code for the "GetDeviceCaps" declaration in the General declarations section of a code module. It should look as follows:
Declare Function GetDeviceCaps Lib "gdi32"
(ByVal hdc As Long, ByVal nIndex As Long) As Long

Then add the following code to the click-event of the form button

Private Sub Form_Click ()
Dim indValue As Long
Dim valReturned as Long
' Set the indValue to the
' required value
indValue =
valReturned = GetDeviceCaps
(Form1.hdc, indValue)

For more information about the GetDeviceCaps call, refer to one of the reference books mentioned below.

API Tip

To ensure API calls function as intended, make sure

  • The spelling is correct
  • All parameters have been designated as the correct type
  • The correct library has been specified.
API Books

If you are considering using the Windows API in your VB projects, the following books deserve a place on your bookshelf.

Daniel Appleman: Visual Basic programmer's guide to the Windows API, ISBN 1 56276 073 4.

Daniel Appleman: Visual Basic Programmer's guide to the Win32 API, ISBN 1 56276 287 7

Reprinted from the May 1996 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]