PDA

Click to See Complete Forum and Search --> : Problems in Hooks


moinkhan
Feb 21st, 2002, 03:09 PM
Hello everybody....
I am trying to set a global CBTProc Hook..
I've written a Hook procedure and set it with the help of SetWindowsHookEX API function...
This Hook procedure calls, it means hook is set successfully.. but i can't figure out why it is giving illegal operation message..i've done so many things.. but still helpless....
Please Help!!!
Anyone knows how to set up the hooks in VB without errors???
MOIN.

DerFarm
Feb 21st, 2002, 04:21 PM
Kinda tough with no code.

abhid
Feb 21st, 2002, 11:46 PM
Try to debug your code and locate exact point of error. Most of
the times it turns out to be some wrong assignment or very small
but BIG mistake.

Crunchy Cat
Feb 22nd, 2002, 01:46 AM
Hi, there is not really enough data to give you an accurate
diagnosis, but I would recommend the following:

* Ensure you are calling the next hook in the hook chain
after you are finished processing the current callback.
* Ensure your callback is not changing any data that is
not meant to be changed.
* Ensure your callback is implemented in a standard Win32
compliant DLL (especially if you are hooking into other
processes).

One thing that I like to do when debugging hooking problems
is put Beep () statements in my callback when certain conditions
occur. This helps me understand the natural flow of events in
the absence of a debugger.

-CC

MerrionComputin
Feb 22nd, 2002, 03:11 AM
In lieu of Beep statements, you can use the OutputDebugString API call - this can fire debug messages from a compiled exe that you can view using DBMON or a similar debug monitor tool.

If you are getting a crash when your CBTProc is called, the most likely thing is that your procedure definition is wrong - wrong return type or parameter type(s)?

Try the following:

'\\ [VB_HOOKCBTPROC]------------------------------------------------------
'\\ typedef LRESULT (CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM lParam);
'\\ code - type of hook,
'\\ Wparam, Lparam - message specific
'\\ lMsgRet = The message to pass to the calling code
'\\ --------------------------------------------------------------------------------
'\\ (c) 2001 - Merrion Computing. All rights to use, reproduce or publish this code reserved
'\\ Please check http://www.merrioncomputing.com for updates.
'\\ --------------------------------------------------------------------------------
Public Function VB_HOOKCBTPROC(ByVal Code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

On Local Error Resume Next

Dim Params() As Variant
Dim lret As Long
Dim lMsgRet As Long

'\\ Note: If the code passed in is less than zero, it must be passed direct to the next hook proc
If Code < 0 Then
VB_HOOKCBTPROC = CallNextHookEx(Eventhandler.HookIdByType(WH_CBT), Code, wParam, lParam)
End If

'\\ Do whatever with the message here.......


'\\ Pass this message on to the next hook proc in the chain (if any)
lret = CallNextHookEx(Eventhandler.HookIdByType(WH_CBT), Code, wParam, lParam)
If Err.LastDllError > 0 Then
Debug.Print Err.LastDllError & " in VB_HOOKCBTPROC "
End If

'\\ If the message isn't cancelled, return the next hook's message...
If Not (lMsgRet) Then
'\\ Return value to calling code....
VB_HOOKCBTPROC = lret
End If

End Function


HTH,
Duncan