What Is a Callback Function?

To call most DLL functions from Microsoft® Visual Basic® for Applications (VBA), you write a VBA function that calls the DLL function, and you are done. However, to call a function that requires a callback function, you must write a VBA function that calls the DLL function. Then, the DLL function calls another VBA function, the callback function, that you have defined in your project. You never call the VBA callback function directly; the DLL function calls it for you. The following diagram provides a conceptual overview of how a callback function works.

How a Callback Function Works

The DLL functions that require callback functions are often functions that must execute some action repeatedly. For example, the SetTimer API function requires a callback function each time a specified interval elapses. You might use it to update a clock on a form every second, or to display a form every hour. This is a simple task in Microsoft® Access, which provides a Timer control for use on Access forms. The other Microsoft® Office XP applications, however, do not have Timer controls. You can use the SetTimer function to create a timer.

Other DLL functions that require callback functions are the enumeration functions, which enumerate through a group of Microsoft® Windows® objects: EnumWindows, EnumPrinters, EnumFontFamilies, and so on. For example, the EnumWindows function enumerates through all of the existing windows and calls the callback function for each window. You can use the EnumWindows callback function to perform an operation on each available window, such as getting the window's caption text or determining its location on the screen.

See Also

Wrapping DLL Functions | Encapsulating the DLL | Creating a Callback Function | Calling a Callback Function