[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
hWndSounds = iDriveSound.hWnd
SubClass_iDrive hWndSounds, lProcSounds, AddressOf WndProc_Sounds
How can this be accomplished???
Re: Is it possible to put AddressOf in a variable
You can do it via a helper function
Code:
Public Function GetAddressOf(inAddressOf As Long) As Long
GetAddressOf = inAddressOf
End Function
Sample call: SubClass_iDrive hWndSounds, lProcSounds, GetAddressOf(AddressOf WndProc_Sounds)
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
Re: Is it possible to put AddressOf in a variable
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
Re: Is it possible to put AddressOf in a variable
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
Re: Is it possible to put AddressOf in a variable
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.
Re: Is it possible to put AddressOf in a variable
Quote:
Originally Posted by
LaVolpe
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?
Re: Is it possible to put AddressOf in a variable
@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.
Re: Is it possible to put AddressOf in a variable
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.
1 Attachment(s)
Re: Is it possible to put AddressOf in a variable
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.
Re: Is it possible to put AddressOf in a variable
I didn't need a helper Sub/Function after all.
This resolved it, it was that AddressOf that was confusing me.
in the .BAS
Public hwnd_frmSoundsEditbox as Long 'two global variables required for every form that uses SubClassing
Public lProcSoundsiDrive as Long
Function WndProc_Sounds
'working stuff is in here which disables the context popup
End Function
Function SubClass_iDrive(driveHwnd as Long, lProc as Long, WndProc as Long) as Long
Dim editboxHwnd as long
editboxHwnd = Get_ChildWindow (driveHwnd, "edit") 'this function enumerates the child windows for the textbox part of the combobox
If editboxHwnd > 0 then
If lProc = False Then lProc = SetWindowLong (editboxHwnd, GWL_WNDPROC, WndProc)
end if
SubClass_iDrive = editboxHwnd
End Function
My form load call:
hwnd_frmSoundsEditbox = SubClass_iDrive (iDriveSound.hWnd, lProcSoundsiDrive, AddressOf WndProc_Sounds)
and of course in the form Unload there is a call to stop subclassing using the two .BAS variables for each form that has subclassing turned on.