[RESOLVED] Is it possible to put AddressOf in a variable
I would like to create a Sub that can transfer an address of a subroutine, I've tried Long and Long_Ptr without success.
Function WndProc_Sounds
'working stuff is in here
End Function
Function SubClass_iDrive(hWnd as Long, lProc as Long, WndProc as Long) as Long
'of course the following line works if I use it outside the Function and insert the real variables
If lProc = False Then lProc = SetWindowLong (hWnd, GWL_WNDPROC, AddressOf WndProc)
End Function
My call:
Dim lProcSounds as Long
Dim hWndSounds as Long
The helper function doesn't need to be public if called from the same class object it exists in. If it will be in a module, then yes, make it a public function
Insomnia is just a byproduct of, "It can't be done"
There is actually no need for a helper function. Just remove the AddressOf operator from the SetWindowLong call and the compilation error will be gone. Note that you cannot use the AddressOf operator with a procedure parameter (WndProc); as stated in the VB6 manual, the AddressOf operator only works with procedures that resides in a standard module (.BAS). Also, as stated again in the documentation, a helper function only becomes necessary when you want to cache a procedure's address in a variable.
Code:
Public Function WndProc_Sounds() As Variant
'working stuff is in here
End Function
Public Function Subclass_iDrive(ByVal hWnd As Long, ByRef lProc As Long, ByVal WndProc As Long) As Long
If lProc = 0& Then lProc = SetWindowLong(hWnd, GWL_WNDPROC, WndProc)
End Function
Code:
Dim lProcSounds As Long
Dim hWndSounds As Long
hWndSounds = iDriveSound.hWnd
Subclass_iDrive hWndSounds, lProcSounds, AddressOf WndProc_Sounds
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
I haven't tried the helper yet. What I didn't mention is that the SubClass_iDriveSound routine should be in the .BAS because it will be called by 7 different forms.
I was just attempting to simplify things, the way it resides now is that I have a
If lProc = False Then lProc = SetWindowLong (hWnd, GWL_WNDPROC, AddressOf WndProc) in each form, changing the variables as required. All the different WndProc's reside in the .BAS
Last edited by vb_lover; Feb 15th, 2016 at 12:36 PM.
A helper function, like the I provided, should do the trick since you are not using 1 window procedure. Having 7 different Window Procedures seems a bit inefficient, especially if they will basically being subclassing the same window or type of window.
Insomnia is just a byproduct of, "It can't be done"
A helper function, like the I provided, should do the trick since you are not using 1 window procedure.
Isn't the helper function somewhat redundant?
Code:
SubClass_iDrive hWndSounds, lProcSounds, GetAddressOf(AddressOf WndProc_Sounds)
SubClass_iDrive hWndSounds, lProcSounds, AddressOf WndProc_Sounds '<-- Should work just fine
@ vb_lover
If possible, can you please show more of your code so we can get a clearer picture of what you're trying to do?
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
@Bonnie. My original response was an answer to the title of the posting.
Now, it does appear that the OP has multiple subclass routines in the module. I'm assuming the request to store it in a variable will be for use within the form that is performing subclassing.
Above being said, not sure how the different subclassing procedures will know which lProc value to pass to CallWindowProc().
I agree that we probably need to see more code to help offer optional strategies.
Insomnia is just a byproduct of, "It can't be done"
The control I wish to subclass (combobox) resides on each form, the WndProc for each resides in the .BAS, more than one form could be open simultaneously, then wouldn't I need separate WndProcs? I was just trying to create a subcall that would contain the variables necessary to address each WndProc. Just a matter of having cleaner code I guess and I was curious if it could be done in this manner.
The goal was to have a common SubClass call "SubClass_iDrive" in the various Form_Loads with the variables required for that form's Control's subclassing to be handled properly.
I am actually subclassing the Edit box of the combobox so included in SubClass_iDrive is the sub which obtains the handle of the Edit box via enumeration of Child windows, this is why I thought about making one routine do it all. Its the Address Of that was my problem.
Last edited by vb_lover; Feb 19th, 2016 at 11:48 AM.
Try the attached subclassing example and see if it meets your needs. It uses the newer subclassing APIs and is based on Karl E. Peterson's Subclassing the XP Way article.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0