Visual Basic 6 Implementation of the Windows API
To include a Windows API function in your VB6 programs, use the Declare statement to "declare" the function to be part of your program. The Declare statement is added to the General Declarations section of either a standard module or a form. If the Declare statement is added to a standard module, the function is considered Public and can be called from anywhere in your application. If the Declare statement is added to the General Declarations section of a form, the function is local to that form and can be called only from within that form. In the latter case, you need to include the Private keyword.
Syntax
The syntax of the Declare statement depends on whether or not the procedure you call returns a value. If the procedure does return a value, you use the Function form of the Declare statement:
[Public | Private] Declare Function publicname Lib "libname" _
[Alias "alias"] [([[ByVal | ByRef] argument [As Type] _
[, [Byval | ByRef] argument [As Type]] ...])] [As Type]
Example
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
ByVal bFailIfExists As Long) As Long
More information on the CopyFile API
If the procedure does not return a value, you use the Sub form of the Declare statement:
[Public | Private] Declare Sub publicname Lib "libname" _
[Alias "alias"] [([[ByVal | ByRef] argument [As Type] _
[, [ByVal | ByRef] argument [As Type]] ...])]
Example
Public Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
More information on the CopyMemory API
Public or Private
Define the scope of the function, and determines whether or not it can be used outside of a module in which it is declared.
publicname
The name of the function is defined by the publicname parameter.
Lib "libname"
Defines the DLL library in which it is located.
Alias "alias"
Some Windows API functions are named using characters that are illegal in Visual Basic 6; for example, the underscore( _ ). Trying to reference a name that starts with an underscore in Visual Basic 6 generates an error. A way to avoid the error is to "alias" the name in the declare statement.
Example
To use the _lopen API function, the following Declare statement will work:
Declare Function lopen Lib "kernel32" Alias "_lopen" _
(ByVal lpPathname As String, ByVal ireadWrite As Long) As Long
The Windows API function _lopen is renamed lopen so that it is recognised as a legal name in Visual Baisc 6. The Alias keyword lets Visual Basic 6 know that the function it is really working with is _lopen.
Another use of the Alias keyword is to change the name of a function, usually for readability reasons. For example, the _lopen function might be renamed to OpenExistingFile as in:
Declare Function OpenExistingFile Lib
"kernel32" Alias "_lopen" (ByVal lpPathname As String, ByVal
ireadWrite As Long) As Long
More info on the _lopen API function
Sometimes, a Windows API function can be named with its ordinal number rather than a more descriptive text name. Using an ordinal number requires fewer system resources, so it is slightly more efficient than using a text name.
If you want to refer to a function by its ordinal number, use the Alias keyword to refer to the number, as in:
Declare Function GetWindowsDirectory Lib "kernel32" Alias "#432" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
To find the ordinal number of a Windows API function, you must use a utility program such as Dumpbin.exe, which is included with Microsoft Visual C++.
ByVal or ByRef
Passing an argument by value means that a copy of the argument is sent to the function. Passing arguments by value means that the function cannot change the value of the actual argument because it is only working with a copy of the argument.
Passing an argument by reference means that the function is actually passing a 32-bit pointer to the memory address where the value of the argument is stored. When an argument is passed by reference, it actually is possible for the function to change the value of the argument because the function is working with the actual memory address where the argument's value is stored, and not just a copy of the argument's value.
Data Types (As Type)
The functions that make up the Windows API are written in C. Here are some of the most common data types you will encounter when using the API.
- Integer: Used for 16-bit numeric arguments.
Equivalent to the short, unsigned short and WORD data types in C
- Long: Used for 32-bit arguments.
Corresponds to the C data types: int, unsigned int, unsigned long, DWORD, and LONG.
- String: Equivalent C Data type is LPSTR
- Structure: A Structure is the C++ equivalent to a Visual Basic UDT (User Defined Type)
- Any: Some functions accept more than one data type for the same argument
Here's a short table that helps you translate the C++ variable type declaration to its equivalent in Visual Basic:
| C++ Variable |
Visual Basic Equivalent |
| ATOM |
ByVal variable as Integer |
| BOOL |
ByVal variable as Long |
| BYTE |
ByVal variable as Byte |
| CHAR |
ByVal variable as Byte |
| COLORREF |
ByVal variable as Long |
| DWORD |
ByVal variable as Long |
| HWND |
ByVal variable as Long |
| HDC |
ByVal variable as Long |
| HMENU |
ByVal variable as Long |
| INT |
ByVal variable as Long |
| UINT |
ByVal variable as Long |
| LONG |
ByVal variable as Long |
| LPARAM |
ByVal variable as Long |
| LPDWORD |
variable as Long |
| LPINT |
variable as Long |
| LPUINT |
variable as Long |
| LPRECT |
variable as Type any variable of that User Type |
| LPSTR |
ByVal variable as String |
| LPCSTR |
ByVal variable as String |
| LPVOID |
variable As Any use ByVal when passing a string |
| LPWORD |
variable as Integer |
| LPRESULT |
ByVal variable as Long |
| NULL |
ByVal Nothing or ByVal 0& or vbNullString |
| SHORT |
ByVal variable as Integer |
| VOID |
Sub Procecure not applicable |
| WORD |
ByVal variable as Integer |
| WPARAM |
ByVal variable as Long |
User-defined types can be defined in a Visual Basic application as shown in this code snippet:
Type Customer
Customer_ID As Integer
Customer_Name As String
End Type
API Viewer
The WinAPI 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.
To view an API file
- From the Add-Ins menu, open the Add-In Manager, and load WinAPI Viewer.
- Click WinAPI Viewer from the Add-Ins menu.
- 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.
Note: If the text file you are opening is large, you may be prompted to convert the file to a database. Doing so will make the WinAPI Viewer much faster.
To optimize speed, you can convert the Win32api.txt file, or any large text file containing the information you want, into a Microsoft Access database file. Converting the text files to database files has numerous advantages primarily because it is much faster to display the list when opening a database than when opening a text file.
If the file you are loading is very large, WinAPI Viewer may automatically prompt you to convert the file to a database to optimize speed. To do so, follow the directions in the message box that appears. You can choose to convert any text file you are loading, however.
To convert a text file to a database file
- Start the WinAPI Viewer application.
- From the File menu, select Load Text File, and open the .txt file you want to convert.
- From the File menu, select Convert Text to Database.
- Choose a file name and location for your database file; then click OK.
To load a database file, click File | Load Database File
Select the type of item you want to view from the API Type list: Constants, Declares, or Types.
Now, take a look at how Visual Basic .NET implements the API.
Comments
There are no comments yet. Be the first to comment!