Microsoft Office 2000/Visual Basic Programmer's Guide   

What Is an API?

API stands for application programming interface. An API is simply a set of functions that you can use to work with a component, application, or operating system. Typically an API consists of one or more DLLs that provide some specific functionality.

DLLs are files that contain functions that can be called from any application running in Windows. At run time, a function in a DLL is dynamically linked into an application that calls it. No matter how many applications call a function in a DLL, that function exists in only a single file on the disk, and the DLL is created only once in memory.

The API you've probably heard about most frequently is the Windows API, which includes the DLLs that make up the Windows operating system. Every Windows application interacts with the Windows API directly or indirectly. The Windows API ensures that all applications running under Windows will behave in a consistent manner.

Note   As the Windows operating system has evolved, several versions of the Windows API have been published. Windows 3.1 uses the Win16 API. The Windows NT, Windows 95, and Windows 98 platforms use the Win32 API. This chapter focuses on functions included in the Win32 API that are available to the Windows NT, Windows 95, and Windows 98 platforms.

There are other published APIs available in addition to the Windows API. For example, the Mail Application Programming Interface (MAPI) is a set of DLLs that can be used to write e-mail applications.

APIs are traditionally written for C and C++ programmers who are building Windows applications, but the functions in a DLL can be called by other programming languages, including VBA. Because most DLLs are written and documented primarily for C/C++ programmers, calling a DLL function may differ somewhat from calling a VBA function. In order to work with an API, you need to understand how to pass arguments to a DLL function.

Warning   Calling the Windows API and other DLL functions can be hazardous to the health of your application. When you call a DLL function directly from your code, you're bypassing some of the safety mechanisms that VBA normally provides for you. If you make a mistake in defining or calling a DLL function (as all programmers eventually do), you may cause an application error (also referred to as a general protection fault, or GPF) in your application. The best strategy is to save your project before you run your code, and to make sure that you understand the principles behind calls to DLL functions.