Putting Procedure Pointers in Variables
Sometimes you need to put the address of a callback procedure into a variable. No problem. You should be able to say this:
Dim procWindow As Long
procWindow = AddressOf EnumWndProc
That might seem obvious to you, but it isn’t obvious to Visual Basic, which recognizes AddressOf only in procedure arguments. The code above just gives a syntax error. You have to jump through more hoops to get this one to work. First write the following function:
Function GetProc(proc As Long) As Long
GetProc = proc
End Function
Then call it like this:
procWindow = GetProc(AddressOf EnumWndProc)
That’s kind of a roundabout way to do it, but I suppose it’s easier for thousands of users to write a one-line function and put it in UTILITY.BAS than for Visual Basic to make the AddressOf operator work in data assignments. You’ll occasionally have to use the GetProc function in UDTs. For example, the MSGBOXPARAMS UDT used by MessageBoxIndirect has a MSGBOXCALLBACK field called lpfnMsgBoxCallback. You can fill it like this:
Dim msgpars As MSGBOXPARAMS
msgpars.lpfnMsgBoxCallback = GetProc(AddressOf MyMsgBoxProc)
I had to search to find this example. It’s not a problem you’ll run into every day.