Sometimes you need to put the address of a callback procedure into a variable. No problem. You should be able to say this:
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: GetProc = proc
Then call it like this:
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:
I had to search to find this example. It’s not a problem you’ll run into every day.Putting Procedure Pointers in Variables
Dim procWindow As Long
procWindow = AddressOf EnumWndProcFunction GetProc(proc As Long) As Long
End Function
procWindow = GetProc(AddressOf EnumWndProc)Dim msgpars As MSGBOXPARAMS
msgpars.lpfnMsgBoxCallback = GetProc(AddressOf MyMsgBoxProc)