Introducing the Win API

API stands for application programming interface. It is simply a group of standard functions that are packaged together and made available to application programmers.

There are quite a few APIs, but the one that you've probably heard most about is the Windows API. The Windows API consists of many DLLs that make up the Windows operating system and ensure that every application that runs under Windows behaves in a consistent manner.

What this actually means is that standard Windows operations, such as saving files, opening forms, managing dialog boxes, and so on, are all handled by the Windows APIs. For example, the standard File Open dialog box is an API function called GetOpenFileName found in comdlg32.dll. Similarly, the GetTempPath function in Kernl3 2.dll returns the name of the folder where temporary files are stored.

All Windows-based applications interact with the Windows APIs in some way, whether they are opening a file, displaying time, putting text on the screen, or managing computer memory while you play Flight Simulator.

When you program in Microsoft Access, you use the built-in VBA functions, which you could loosely refer to as an API. Similarly, when you use the Access Add-in Manager or References dialog box to link to an external DLL, OCX, MDB, and so on, you are linking to something that is essentially an API.

There's no need to feel intimidated by the API, despite the fact that it has the reputation of being highly complex. Because the Windows APIs were written in C++, there are rules that VB programmers must be aware of, but other than that, they can pretty much be used in the same way as any other function.

For example, enter the following example into a standard module and run it.

Private Declare Function GetUserName _

Lib "advapi3 2.dll" Alias "GetUserNameA" _ (ByVal lpBuffer As String, _ nSize As Long) As Long

Private Const MAXLEN = 255

Function GetLoginName() As String Dim strUserName As String Dim lngSize As Long Dim lngReturn As Long lngSize = 256

strUserName = Space(MAXLEN) & Chr(0)

If GetUserName(strUserName, lngSize) <> 0 Then

GetLoginName = left(strUserName, lngSize - 1)

Else

GetLoginName = "" End If End Function

The above code returns the domain log-in name of the current Windows user (that'd be you). At this point, the Declare function part may be a bit of a mystery, but the rest is a standard VBA function. In Visual Basic, using most APIs is just that simple.

The following table lists just a few of the DLLs that contain APIs to use in VBA applications. For more information about the Windows API, refer to Appendix F.

API

Basic Description

KERNEL32.DLL

Low-level operating system functions, such as memory and task management, resource handling, and so on.

USER32.DLL

Window management functions, including messages, menus, cursors, carets, timers, communications, and most of the nondisplay functions.

GDI32.DLL

The Graphics Device Interface Library. Device output, including most drawing functions, display context, metafile, coordinate, and font functions.

COMDLG32.DLL

Common dialog boxes.

LZ32.DLL

File compression.

VERSION.DLL

Version control.

MAPI32.DLL

Electronic mail.

COMCTL32.DLL

Implements a new set of Windows controls and dialog boxes, including the tree view and rich text edit controls.

NETAPI32.DLL

Access and control of networks.

ODBC32.DLL

Implements ODBC (Open Database Connectivity), providing functions to work with databases.

WINMM.DLL

Multimedia.

32-bit Windows packs its APIs into function libraries called DLLs, but before we get too far into the details of how to use them, here is some historical background to help understand how they work.

0 0

Responses

  • robert
    How to use lz32.dll in ms access?
    7 years ago

Post a comment