Desktop Functions: Smart Device Functions:
|
setwindowshookex (user32)
C# Signature:
[DllImport("user32.dll")] User-Defined Types:A HookType constant specifying the type of hook to install. A HookProc, LowLevelKeyboardProc or LowLevelMouseProc delegate representing the hook procedure method. Notes:This will enable you to install application hooks. However, you cannot implement global hooks in Microsoft .NET Framework except low level hooks. To install a global hook, a hook must have a native dynamic-link library (DLL) export to inject itself in another process that requires a valid, consistent function to call into. This requires a DLL export, which .NET Framework does not support. Managed code has no concept of a consistent value for a function pointer because these function pointers are proxies that are built dynamically. AppDomain.GetCurrentThreadId is marked as deprecated in favour of Thread.ManagedThreadId but this does not seem to work! To hook low level events use:
IntPtr hMod = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]); Tips & Tricks:Remember to keep the HookProc delegate alive manually, otherwise the garbage collector will clean up your hook delegate eventually, resulting in your code throwing a System.NullReferenceException. Remember to call CallNextHookEx. Note how you can import the same function several times with different overloaded signatures to handle the common case where one parameter is an opaque pointer pointing to a struct that depends on another parameter. Sample Code:
// this sample installs a keyboard hook Alternative Managed API:Do you know one? Please contribute it! SetWindowsHookEx on MSDN MSDN example of a mouse hook in C#: http://support.microsoft.com/default.aspx?scid=kb;en-us;318804#3 MSDN article on hooks in .NET: http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/ MSDN article on KeyboardProc: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/keyboardproc.asp Steven Toub entry on low-level keyboard hook in C#: http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx Steven Toub entry on low-level mouse hook on C#: http://blogs.msdn.com/toub/archive/2006/05/03/589468.aspx Please edit this page!Do you have...
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more). |
|