Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than user32, prefix the name with the module name and a period.
<DllImport("user32.dll")> _
Public Shared Function SetWindowsHookEx(ByVal idHook As Integer, _
ByVal lpfn As GlobalHookProcessor.HookProc, _
ByVal hInstance As IntPtr, _
ByVal threadId As Integer) As Integer
End Function
User-Defined Types:
A HookType constant specifying the type of hook to install.
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!
// start edit
Quite true System.Threading.Thread.CurrentThread.ManagedThreadId does not appear to return the correct threadid for this purpose.
I found WINS32 API GetCurrentThreadId() does the trick.
VB.NET
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Integer
using (Process process = Process.GetCurrentProcess())
using (ProcessModule module = process.MainModule)
{
IntPtr hModule = GetModuleHandle(module.ModuleName);
Remember to keep the HookProc delegate alive manually, such as using a class member as shown in the example below, otherwise the garbage collector will clean up your hook delegate eventually, resulting in your code throwing a System.NullReferenceException.
Remember to call CallNextHookEx in your callback delegate.
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
using System.Windows.Forms;
public class MyClass
{
private HookProc myCallbackDelegate = null;
public MyClass()
{
// initialize our delegate
this.myCallbackDelegate = new HookProc(this.MyCallbackFunction);
// setup a keyboard hook
SetWindowsHookEx(HookType.WH_KEYBOARD, this.myCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId());
}
[DllImport("user32.dll")]
static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
private int MyCallbackFunction(int code, IntPtr wParam, IntPtr lParam)
{
if (code < 0) {
//you need to call CallNextHookEx without further processing
//and return the value returned by CallNextHookEx
return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
}
// we can convert the 2nd parameter (the key code) to a System.Windows.Forms.Keys enum constant
Keys keyPressed = (Keys)wParam.ToInt32();
Console.WriteLine(keyPressed);
//return the value returned by CallNextHookEx
return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
}
}
Delegate Function HookProc(ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) _
As Integer
Private myCallbackDelegate As HookProc = Nothing
Public Sub New()
' initialize our delegate
Me.myCallbackDelegate = New HookProc(AddressOf Me.MyCallbackFunction)
' setup a keyboard hook
SetWindowsHookEx(HookType.WH_KEYBOARD, Me.myCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId())
End Sub
<DllImport("user32.dll")> _
Friend Shared Function SetWindowsHookEx(ByVal idHook As Integer, _
ByVal lpfn As HookProc, _
ByVal hInstance As IntPtr, _
ByVal threadId As Integer) As Integer
End Function
<DllImport("user32.dll")> _
Friend Shared Function CallNextHookEx(ByVal hhk As intptr, _
ByVal nCode As Integer, ByVal wParam As intptr, _
ByVal lParam As intptr) As Integer
End Function
'ok
Private Function MyCallbackFunction(ByVal code As Integer, ByVal wParam As intptr, ByVal lParam As intptr) As Integer
If (code < 0) Then
'you need to call CallNextHookEx without further processing
'and return the value returned by CallNextHookEx
Return CallNextHookEx(IntPtr.Zero, code, wParam, lParam) 'unt
End If
' we can convert the 2nd parameter (the key code) to a System.Windows.Forms.Keys enum constant
Dim keyPressed As Keys = CType(wParam.ToInt32, Keys)
Console.WriteLine(keyPressed)
'return the value returned by CallNextHookEx
Return CallNextHookEx(IntPtr.Zero, code, wParam, lParam)
End Function
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
The GetCurrentThreadId API
3/29/2017 11:55:16 AM - mark at [nospam]epitman.com-12.151.76.126
Represents the method called when a hook catches a monitored event.
6/24/2022 6:31:50 AM - Nicolas-93.20.31.10
The CallNextHookEx API
3/29/2016 1:15:57 PM - -65.216.74.168
The UnhookWindowsHookEx API
1/7/2019 10:40:51 AM - -93.41.3.12
Installs a hook to monitor certain types of events.
1/7/2019 10:18:01 AM - -93.41.3.12
http://mwinapi.sourceforge.net/
3/31/2008 6:53:29 AM - -217.54.254.83
TODO - a short description
3/21/2011 1:29:09 PM - anonymous
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
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).