Visual Basic Concepts

Accessing the Microsoft Windows API

You can gain access to the Windows API (or other outside DLLs) by declaring the external procedures within your Visual Basic application. After you declare a procedure, you can use it like any other language feature in the product.

The most commonly used set of external procedures are those that make up Microsoft Windows itself. The Windows API contains thousands of functions, subs, types, and constants that you can declare and use in your projects. These procedures are written in the C language, however, so they must be declared before you can use them with Visual Basic. The declarations for DLL procedures can become fairly complex. While you can translate these yourself, the easiest way to access the Windows API is by using the predefined declares included with Visual Basic.

The file Win32api.txt, located in the \Winapi subdirectory of the main Visual Basic directory, contains declarations for many of the Windows API procedures commonly used in Visual Basic. To use a function, type, or other feature from this file, simply copy it to your Visual Basic module. You can view and copy procedures from Win32api.txt by using the API Viewer application, or by loading the file in any text editor.

Note   The Windows API contains a vast amount of code. To find reference information on the procedures and other details included in this API set, refer to the Win32 SDK, included on the Microsoft Developer Network Library CD.

Using the API Viewer Application

The API Viewer application enables you to browse through the declares, constants, and types included in any text file or Microsoft Jet database. After you find the procedure you want, you can copy the code to the Clipboard and paste it into your Visual Basic application. You can add as many procedures as you want to your application.

The API Viewer application

To view an API file

  1. From the Add-Ins menu, open the Add-In Manager and load API Viewer.

  2. Click API Viewer from the Add-Ins menu.

  3. Open the text or database file you want to view.

    • To load a text file into the viewer, click File \ Load Text File and choose the file you want to view.

    • To load a database file, click File \ Load Database File.

  4. Select the type of item you want to view from the API Types list.

Note   You can have the API Viewer automatically display the last file you viewed in it, when it is opened, by selecting View \ Load Last File.

To add procedures to your Visual Basic code**

  1. Click the procedure you want to copy in the Available Items list.

  2. Click Add. The item appears in the Selected Items list.

  3. Indicate the scope of the item by clicking Public or Private in the Declare Scope group.

  4. To remove an entry from the Selected Items list box, click the item and click Remove.

  5. To remove all entries from the Selected Items list box, click Clear.

To copy the selected items to the clipboard

  1. Click Copy. All of the items in the Selected Items list will be copied.

  2. Open your Visual Basic project and go to the module in which you want to place the API information.

  3. Position the insertion point where you want to paste the declarations, constants, and/or types, and then choose Edit \Paste.

Converting Text Files to Jet Database Files

To optimize speed, you can convert the Win32api.txt file into a Jet database file, because it is much faster to display the list when opening a database than when opening a text file.

To convert a text file to a jet database file

  1. Start the API Viewer application.

  2. Click File \ Load Text File and open the .txt file you want to convert.

  3. Click File \ Convert Text to Database.

  4. Choose the file name and location for your database file, then click OK.

Loading an API File Automatically from the Command Line

You can specify a text or database file on the command line for Apilod32.exe so that the file is automatically loaded when you start API Viewer. Use the following syntax to load the file you choose when you start the API Viewer application:

Apilod32.exe {/T|/D} filename
Argument Description
/T API Viewer will load the file as a text file. /T must be uppercase.
/D API Viewer will load the file as a database file. /D must be uppercase.
Filename The path of the file you want to open.

There must be a space between /T or /D and the filename argument. An error message will be displayed if the file is not found. If you specify a file that is not a database or text file, an error message will be displayed when you try to load the file.

Tip   You can view a prompt that shows the parameters of the command line syntax by using a DOS window to navigate to the directory in which the API viewer application is installed, then typing apiload /?.

Viewing the Win32api.txt file with a Text Editor

You can also load the Win32api.txt file in a text editor, such as Microsoft Word or WordPad, to locate the procedures you want to use. Again, you just copy the procedures from the file to a Visual Basic module to use them in your application.

Tip   Don't load the Win32api.txt file into a module. This is a large file, and it will consume a lot of memory in your application. You will generally use only a handful of declarations in your code, so selectively copying the declarations you need is much more efficient.

Using Procedures from Other Sources

If you are attempting to call a procedure in a DLL that is not part of the operating system, you must determine the proper declaration for it. The topic "Declaring a DLL Procedure" explains the syntax of the Declare statement in detail.

Note   If you use Visual C++ (or a similar tool) to create DLLs that will be called by Visual Basic, use the __stdcall calling convention. Do not use the default calling convention (_cdecl).