To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98
Chapter 27 Using The Windows API
Your Visual Basic programs can access all the power of Windows by calling procedures in the Windows API.
In working with Visual Basic, youve seen how it has a number of built-in procedures that perform such commonly needed tasks as string manipulation, graphics, and mathematical calculations. In a similar fashion, the Windows operating system has a large collection of built-in procedures. These procedures are part of the Windows Applications Programming Interface, or API. By calling these API procedures directly from your Visual Basic program, you can provide additional functionality with relatively little programming effort. In this chapter, Ill show you how to use API calls in your Visual Basic programs as well as a sophisticated API technique called callbacks.
What Is The Windows API?
The Windows API is a huge collection of procedures that can be called by any program running under Windows. In fact, the API is behind a great deal of what every Windows program offers. Displaying screen windows, using the printer, displaying text, using menusthese are all handled by the API. You cannot overestimate the importance of the API. From a programmers perspective, Windows is the API.
When you write a Visual Basic program, you are accessing the API indirectly. You create your program by placing controls, setting properties, executing methods, and so on. When the program runs, Visual Basic translates your instructions into calls to the appropriate API procedures, which perform much of the actual job of the program. You can also call API procedures directly, a task similar to calling a regular Basic procedure.
Why call the API directly? As powerful as Visual Basic is, it does not provide access to all of the capabilities of the Windows API. If you know whats available in the API and how to reach ityoull have that many more tools available when creating a Visual Basic program.
The API procedures are located in files called dynamic link libraries, or DLLs. DLL files are installed on your disk when you install Windows. They are loaded into memory whenever a Windows application calls them. Many applications install their own DLL files, but we are interested only in those that are part of the Windows 95 operating system. There is only a single copy of each API procedure, which is shared by all programs that need it. This is called dynamic linkinghence, the name dynamic link library.
The Windows API contains a huge number of procedures capable of performing just about any task you can imagineand probably a lot that you cant. Youll find procedures for window management, file manipulation, printer control, menus and dialog boxes, memory management, graphics drawing, multimedia, string manipulation...well, you get the idea. Reference material on the API runs to multivolume sets with more than 1,000 pages. Within the context of this book, providing information on even a small fraction of the available API procedures would be impossible. Instead, my goal in this chapter is to show you the general methods for calling API procedures from your Visual Basic programs. For information on specific procedures, I suggest you turn to one of the numerous API reference books.
Accessing The Windows API
As mentioned earlier, calling an API procedure from a Visual Basic program is similar to calling a general Basic function or procedure. If the API procedure is a functionthat is, if it returns a value to the calling programyou use the function name on the right side of an assignment statement, assigning its return value to a variable of the appropriate type. For an API procedure that does not return a value, you use the Call statement. As with any procedure, you must pass it the correct number and types of arguments.
Lets look at a simple example. If MyAPISub is an API procedure, and MyAPIFunc is an API function that returns an integer, you would call them as shown here (ignoring arguments for now):
Call MyAPISub()
RetVal = MyAPIFunc()
Before you can call an API procedure, it must be declared in the program using the Declare statement. The Declare statement informs Visual Basic about the name of the procedure, as well as the number and types of arguments it takes. The Declare statement takes one of the following forms, depending on whether the API procedure returns a value to the calling program or not:
Declare Sub APIProcName Lib DLLname [([argumentlist])]
Declare Function APIProcName Lib DLLname [([argumentlist])] [As type]
APIProcName is the name of the API procedure, and DLLname is a string literal specifying the name of the DLL file that contains the procedure. The type at the end of the function declaration declares the data type of the value returned by a function; argumentlist is a list of variables representing arguments passed to the function or procedure when it is called. Most arguments to API procedures must be passed with the ByVal keyword. The argument list has the following syntax:
ByVal argname1 [As type] [,ByVal argname2 [As type]]...
For the statement to work, the components of the Declare statement must be specified exactly. If the Declare statement contains an error, the call to the API procedure generates a Reference to undefined function or array error message when you run the program. The Declare statements required by many API procedures are rather long and involved. Heres an example:
Declare Function SendDlgItemMessage Lib user32 Alias _
SendDlgItemMessageA (ByVal hDlg As Long, ByVal nIDDlgItem As Long, _
ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
For now, dont worry about the Alias keywordwhich Ill explain lateror what the API procedure does. Im just using this as an example of a real API procedure declaration. Im glad to report that you will rarely, if ever, need to type them into your programs. Visual Basic includes a program called the API Text Viewer that provides a complete listing of Declare statements for all of the API functions. You can simply copy one or more Declare statements to the Clipboard and paste them into your program. The API Text Viewer also provides information on defined Windows constants and types. You can start the API Text Viewer by clicking on its icon in the Visual Basic program group that was created when Visual Basic was installed. Please refer to your Visual Basic documentation for information on how to use the viewer.
Declare statements should usually be placed in the general declarations section of the module that calls the procedures. See the following section on the Private and Public keywords for more information on the location of Declare statements.
TIP: DLL Procedure Names Are Case-Sensitive
In Windows 95, DLL procedure names should carry a warning label that reads, Case-sensitive areaenter with care. All it takes to get a runtime error is one letter typed in the wrong case. Theres no alternative but to be extremely careful when typing procedure names.
|