CBT Hook


A

Arnold

Hi,
I want to make a Global CBT Hook, but the CallBack Method is never raised.
SetWindowsHookEx return me a Handle to the Hook, but when i try to uninstall
the hook the UnhookWindowsHookEx generate an error (1404: Invalid hook
handle.). A part of code is this:
Install Hook:
....
CBTCallBack = new GlobalHook.GlobalHook.HookProc(CBTCallBackEventHandler);
CBTHandle =
GlobalHook.SetWindowsHookEx(GlobalHook.HookType.WH_CBT,CBTCallBack,
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
....
CallBack Function:
....
private int CBTCallBackEventHandler(int nCode, IntPtr wParam, IntPtr lParam)
{
CBTACTIVATESTRUCT WndData;
if(nCode >= 0)
{
if(wParam.ToInt32() == 5)
{
WndData =
(CBTACTIVATESTRUCT)Marshal.PtrToStructure(lParam,typeof(CBTACTIVATESTRUCT));
if(this.Activated != null)
{
Activated(this,new CBTEventArgs(WndData.hWndActive,"none"));
}
}
}
return GlobalHook.CallNextHookEx(this.CBTHandle,nCode, wParam, lParam);
}
....

Sombody can help me?

Thanks!
 
Ad

Advertisements

M

Mattias Sjögren

I want to make a Global CBT Hook,

Then you shouldn't write it in managed code.



Mattias
 
M

Mattias Sjögren

So, I can't do it in C#? :( :( :(

No, and even if you could you wouldn't want to. Global hook code must
be able to run in any process. To run managed code you must load the
CLR. That would mean loading the CLR into a bunch of processes for no
good reason.




Mattias
 
Ad

Advertisements

A

Arnold

I already read that article, but the CBT hook is in local app, not global.

Thanks!



"Mick Doherty"
The following two MSDN articles should help you out.

http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/

http://msdn.microsoft.com/msdnmag/issues/02/11/CuttingEdge/


--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


Arnold said:
Hi,
I want to make a Global CBT Hook, but the CallBack Method is never
raised.
SetWindowsHookEx return me a Handle to the Hook, but when i try to
uninstall
the hook the UnhookWindowsHookEx generate an error (1404: Invalid hook
handle.). A part of code is this:
Install Hook:
...
CBTCallBack = new
GlobalHook.GlobalHook.HookProc(CBTCallBackEventHandler);
CBTHandle =
GlobalHook.SetWindowsHookEx(GlobalHook.HookType.WH_CBT,CBTCallBack,
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
...
CallBack Function:
...
private int CBTCallBackEventHandler(int nCode, IntPtr wParam, IntPtr
lParam)
{
CBTACTIVATESTRUCT WndData;
if(nCode >= 0)
{
if(wParam.ToInt32() == 5)
{
WndData =
(CBTACTIVATESTRUCT)Marshal.PtrToStructure(lParam,typeof(CBTACTIVATESTRUCT));
if(this.Activated != null)
{
Activated(this,new
CBTEventArgs(WndData.hWndActive,"none"));
}
}
}
return GlobalHook.CallNextHookEx(this.CBTHandle,nCode, wParam,
lParam);
}
...

Sombody can help me?

Thanks!
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top