Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

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

 

I don't know if any of you are familiar with Dino Esposito article in MSDN magazine on .NET Hooks but I used the class he created named LocalWindowsHook that imports the below API calls

 

protected static extern IntPtr SetWindowsHookEx(HookType code,

HookProc func,

IntPtr hInstance,

int threadID);

 

and a few other necessary functions from user32.dll to implement a hook.

 

I ended up changing his Install function from

 

public void Install()

{

m_hhook = SetWindowsHookEx(

m_hookType, m_filterFunc,

IntPtr.Zero,

(int)AppDomain.GetCurrentThreadId());

}

 

to

 

public void Install()

{

m_hhook = SetWindowsHookEx(

m_hookType,

m_filterFunc,

GetHInstance(),

0);

}

 

public static IntPtr GetHInstance()

{

return Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]);

}

 

In an attempt to create a global hook using WH_KEYBOARD.

 

The problem I have is it doesn't work. It works globally with WH_KEYBOARD_LL but as soon as I use WH_KEYBOARD it doesn't work at all. This meaning it doesn't capture keyboard strokes in either the windows form or anywhere else. Now Dino's code worked fine for a local hook (aka the form) but I want a global or system hook.

 

Now I have the class he wrote in an external DLL but there must be something else i am missing. If anyone has any suggestions they would be very much appreciated.

 

Thanks very much.

 

Dino Esposito modified project and my test windows form can be found here

 

http://scaninc.ca/hook/globalhook.zip

  • *Gurus*
Posted

Global hooks need to be implemented with standard c dlls, and lightweight ones at that. .NET is not suitable.

 

Also, even if it were, the guidelines on this forum prohibit discussion of such things as they can clearly be used for malicious purposes.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Guest
This topic is now closed to further replies.
×
×
  • Create New...