Results 1 to 10 of 10

Thread: [RESOLVED] Is it possible to put AddressOf in a variable

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    169

    Resolved [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???
    Last edited by vb_lover; Feb 14th, 2016 at 11:10 AM. Reason: spelling

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    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
    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
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    169

    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
    Last edited by vb_lover; Feb 15th, 2016 at 12:36 PM.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Is it possible to put AddressOf in a variable

    Quote Originally Posted by LaVolpe View Post
    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
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    169

    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.
    Last edited by vb_lover; Feb 19th, 2016 at 11:48 AM.

  9. #9
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    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.
    Attached Files Attached Files
    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
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    169

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width