[RESOLVED] [VB6] - about Hook windows api procedure
the Hook windows API procedure is a loop or just activated when the user do an action?
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?
Re: [VB6] - about Hook windows api procedure
Quote:
Originally Posted by
jmsrickland
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?
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
Re: [VB6] - about Hook windows api procedure
Quote:
Originally Posted by
jmsrickland
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?)
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).
Re: [VB6] - about Hook windows api procedure
Quote:
Originally Posted by
jmsrickland
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