The Windows API

The Windows API is a collection of routines available to you, the Visual Basic programmer. In a way, these API routines work just like Visual Basic's own internal functions. When you need to use the code in an API routine, your Visual Basic program calls that routine. When the Windows API finishes, control returns to your program so that it can continue.

So many Windows API routines exist that just about anything you can do from Windows, you can do from a Visual Basic application by calling the appropriate Windows API routine. For example, you can even force a system reboot by calling the appropriate Windows API routine.

note

The Windows API, or Application Programming Interface, is a set of internal Windows routines you can call from Visual Basic.

All the Windows API routines are stored in special files called DLLs. Several thousand API routines are available for use. These API routines appear in files stored in your Windows and Windows\System folders. When you install Windows, the DLL files install as well; therefore, you have access to these libraries automatically.

note

A DLL, or dynamic link library, is a set of API-based routines available for any Visual Basic application to use.

Most DLL files have the .DLL filename extension or the .EXE filename extension. Any program you write has access to the Windows DLLs. These same DLLs were part of older Windows versions as well (before Windows 95), except that these files didn't have "32" in their names, designating them as 32-bit compatible. Pre[nd]Windows 95 versions were 16-bit compatible, meaning that data traveled through the system 16 bits (two bytes) at a time. Programming in a 32-bit environment adds much more flexibility, speed, and efficiency over the older 16-bit environment.

Here are the three most common DLL libraries:

  • USER32.DLL - Contains functions that control the Windows environment and the user's interface, such as cursors, menus, and windows.
  • GDI32.DLL - Contains functions that control output to the screen and other devices.
  • KERNEL32.DLL - Contains functions that control the internal Windows hardware and software interface. Most of the memory, file, and directory service routines are located in KERNEL32.DLL.

note

The GDI32.DLL library gets its name from the graphics device interface.

tip

Windows is an operating system of several layers, starting with the layer the user sees (the graphical user interface) and ending with the layer closest to the hardware that controls the data flow between programs and hardware. This lowest level of the operating system is called the kernel. Hence, the name KERNEL32.DLL for the dynamic link library that contains the kernel-based routines.

These three files hold most of the API routines, or functions, that you'll call from your Visual Basic applications. As you glance through your Windows and Windows\System folders, you'll see other dynamic link libraries, as well, with names such as COMDLG.DLL, MAPI32.DLL, NETAPI32.DLL, and WINMM.DLL. As Microsoft adds features to the operating system, new DLL files appear.

DLLs are not just part of Windows. When you add a new application to your system, that application often supplies its own DLL. Therefore, over time, many DLL files begin to appear in your system.

caution

DLLs give you much more power over your system than Visual Basic normally provides. When using a Windows API function, you're working with the internals of the operating system. As always, with power comes responsibility. Visual Basic's environment and debugger recognize normal internal Visual Basic functions. However, API functions are far outside Visual Basic's scope. Therefore, you can cause a system to crash, losing all your work, just by specifying improper arguments when running a Visual Basic application that calls a Windows API function.

tip

Save your project often when calling API functions. This way, if you inadvertently call an API function that causes a system crash, you'll not lose all your work.

Top Home