Calling a Callback Function

In previous versions of VBA, it is not possible to use callback functions, because there is no way to tell the DLL function which of your own functions you want to call. To call a callback function, a DLL function must have a pointer to the callback function's address in memory. Previous versions of VBA do not support pointers to functions. VBA now supports the AddressOf operator, which makes it possible for you to pass the address of a VBA function to a DLL.

The Class_Initialize procedure in the ParentWindows collection calls the EnumWindows function, passing the address of EnumWindowsProc for the lpEnumFunc argument, and a reference to the ParentWindow object itself (using the Me keyword) for the lParam argument.

Note   The AddressOf operator is followed by the name of the callback function, without any arguments:

Private Sub Class_Initialize()
      
   ' Create new instance of private collection object.
   Set mcolParents = New Collection
   
   ' Add visible parent windows to collection.
   ' Pass Me as reference to ParentWindows collection.
   EnumWindows AddressOf EnumWindowsProc, Me
End Sub

See Also

Wrapping DLL Functions | Encapsulating the DLL | What Is a Callback Function? | Creating a Callback Function