Results 1 to 7 of 7

Thread: [RESOLVED] [VB6] - about Hook windows api procedure

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Resolved [RESOLVED] [VB6] - about Hook windows api procedure

    the Hook windows API procedure is a loop or just activated when the user do an action?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [VB6] - about Hook windows api procedure

    I cannot find any API that is named Hook. Are you referring to an API or the action to Hook?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: [VB6] - about Hook windows api procedure

    Quote Originally Posted by jmsrickland View Post
    I cannot find any API that is named Hook. Are you referring to an API or the action to Hook?
    what name\term for that windows procedure that you catch windows messages?
    can i use these procedure like a normal loop, or these procedure is only for catch windows messages?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [VB6] - about Hook windows api procedure

    In your Form code

    Code:
    Private Sub Command1_Click()
     OldWindowProc = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf MyWindowProc)
    End Sub
    Private Sub Command2_Click()
     '
     ' Restore the old window procedure before exiting
     '
     SetWindowLong Me.hWnd, GWL_WNDPROC, OldWindowProc
    
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
     Dim frm As Form
        
     '
     ' Restore the old window procedure before exiting
     '
     SetWindowLong Me.hWnd, GWL_WNDPROC, OldWindowProc
        
     Unload Me
    End Sub
    In your Module code

    Code:
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Public Const WM_LBUTTONDOWN = &H201
    
    Public OldWindowProc As Long
    
    Public Const GWL_WNDPROC& = (-4)
    Public Function MyWindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
     Dim Processed As Boolean
     
     Select Case uMsg
       Case WM_LBUTTONDOWN
         MsgBox "Left Button Down"
         Processed = True
       Case Else
         MyWindowProc = CallWindowProc(OldWindowProc, hWnd, uMsg, wParam, lParam)
     End Select
     
     If Processed Then
       MyWindowProc = CallWindowProc(OldWindowProc, hWnd, uMsg, wParam, lParam)
     End If
    End Function


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: [VB6] - about Hook windows api procedure

    Quote Originally Posted by jmsrickland View Post
    In your Form code

    Code:
    Private Sub Command1_Click()
     OldWindowProc = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf MyWindowProc)
    End Sub
    Private Sub Command2_Click()
     '
     ' Restore the old window procedure before exiting
     '
     SetWindowLong Me.hWnd, GWL_WNDPROC, OldWindowProc
    
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
     Dim frm As Form
        
     '
     ' Restore the old window procedure before exiting
     '
     SetWindowLong Me.hWnd, GWL_WNDPROC, OldWindowProc
        
     Unload Me
    End Sub
    In your Module code

    Code:
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Public Const WM_LBUTTONDOWN = &H201
    
    Public OldWindowProc As Long
    
    Public Const GWL_WNDPROC& = (-4)
    Public Function MyWindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
     Dim Processed As Boolean
     
     Select Case uMsg
       Case WM_LBUTTONDOWN
         MsgBox "Left Button Down"
         Processed = True
       Case Else
         MyWindowProc = CallWindowProc(OldWindowProc, hWnd, uMsg, wParam, lParam)
     End Select
     
     If Processed Then
       MyWindowProc = CallWindowProc(OldWindowProc, hWnd, uMsg, wParam, lParam)
     End If
    End Function
    yes... i'm speak about these code.
    what term you use for call it?
    can i use these code for normal loop, or it's only for computer\user actions?
    (can i calculate milliseconds with these code?)
    VB6 2D Sprite control

    To live is difficult, but we do it.

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [VB6] - about Hook windows api procedure

    You don't call it. It is called by the OS because you have placed a procedure, MyWindowProc, in your code that replaces the default procedure that handles messages sent to the Form window.

    Run app. Click on Start Subclassing. This will start the subclassing process (call it hooking if you want to). Now left mouse click on Form and that event is passed to the MyWindowProc and there you do what you want to do (in my example I just put a MsgBox but you can do whatever you want to do)

    Click on Stop Subclassing. Left mouse click on Form and nothing happens because now the default Windows procedure (you don't see this one because the OS has it) gets the mouse event instead of your procedure.

    My example only captures one event; WM_LBUTTONDOWN, but you can get the other events by looking at API Viewer and copying all the WM_ messages and putting them in your procedure. The only thing is you will need to know how to handle those events but at least it will get you started.

    WORD OF WARNING: Subclassing can be hazardous to your program. Make sure you ALWAYS save your changes before you run your project because any error in your subclassing code will cause your project and VB IDE to crash.

    So, to answer your question, the process is only driven by some event or action applied to the Form. One exception to this, however, some other application can send a message to your app and the message it sends will be captured in your procedure if your procedure is looking for that message code (WM_ message).
    Last edited by jmsrickland; Jul 1st, 2012 at 12:51 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  7. #7

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: [VB6] - about Hook windows api procedure

    Quote Originally Posted by jmsrickland View Post
    You don't call it. It is called by the OS because you have placed a procedure, MyWindowProc, in your code that replaces the default procedure that handles messages sent to the Form window.

    Run app. Click on Start Subclassing. This will start the subclassing process (call it hooking if you want to). Now left mouse click on Form and that event is passed to the MyWindowProc and there you do what you want to do (in my example I just put a MsgBox but you can do whatever you want to do)

    Click on Stop Subclassing. Left mouse click on Form and nothing happens because now the default Windows procedure (you don't see this one because the OS has it) gets the mouse event instead of your procedure.

    My example only captures one event; WM_LBUTTONDOWN, but you can get the other events by looking at API Viewer and copying all the WM_ messages and putting them in your procedure. The only thing is you will need to know how to handle those events but at least it will get you started.

    WORD OF WARNING: Subclassing can be hazardous to your program. Make sure you ALWAYS save your changes before you run your project because any error in your subclassing code will cause your project and VB IDE to crash.

    So, to answer your question, the process is only driven by some event or action applied to the Form.
    thanks for all
    Last edited by joaquim; Jul 1st, 2012 at 01:08 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

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