C# SetWindowsHookEx
Beschreibung:
Mit
SetWindowHookEx sagt man dem Windows-System, dass ein Systemvorgang wie eine Tastatureingabe oder Mausbewegung zusätzlich
an die eigene Anwendung informiert wird. Das Windows-System sendet dann den Vorgang an eine Funktion in der eigenen Anwendung.
Man gibt als 1. Parameter den Ereignistyp an wie Tastatureingabe und als 2. Parameter, die Funktion, an die der Vorgang geschickt werden soll und im 3.Parameter das Modul, in dem sich die Funktion befindet.
Interner Aufschrieb zur Analyse, was man eingeben muss
Parameterwerte zur Laufzeit:
Hier am Beispiel: ich möchte die Keyboard-Eingabe, und diese soll an LowLevelKeyboardProc in meinem Modul 100944512=ScreenCapture_to_Clipboard.vshost.exe gesendet werden
Damit man SetWindowHookEx in WPF verwenden kann muss man die User32.dll einbinden
Für den Befehlt SetWindowsHookEx muss man folgende Zeile in die Klasse schreiben
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
|
Und den Namespace im Header
using System.Runtime.InteropServices;
|
Beschreibung der Funktion unter MSDN
Unter
MSDN-> SetWindowsHookEx function
Installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread.
Quelle https://msdn.microsoft.com/de-de/library/windows/desktop/ms644990(v=vs.85).aspx
HHOOK WINAPI SetWindowsHookEx(
_In_ int idHook,
_In_ HOOKPROC lpfn,
_In_ HINSTANCE hMod,
_In_ DWORD dwThreadId
);
Beschreibung der Parameter:
1.Paramenter: idHook
->Inter Nummer, welche festlegt, welcher Typ von Hook installiert werden soll
Wesentlich sind
WH_KEYBOARD
2
|
Installs a hook procedure that monitors keystroke messages. For more information, see theKeyboardProchook procedure.
|
WH_KEYBOARD_LL
13
|
Installs a hook procedure that monitors low-level keyboard input events. For more information, see theLowLevelKeyboardProchook procedure.
|
WH_MOUSE
7
|
Installs a hook procedure that monitors mouse messages. For more information, see theMouseProchook procedure.
|
WH_MOUSE_LL
14
|
Installs a hook procedure that monitors low-level mouse input events. For more information, see theLowLevelMouseProch
|
2.Parameter
HOOKPROC lpfn,
Type: HOOKPROC
A pointer to the hook procedure. .. lpfn parameter must point to a hook procedure in a DLL.
Otherwise, lpfn can point to a hook procedure in the code associated with the current process.
Im gezeigten Fall ist das
proc {Method = {IntPtr LowLevelKeyboardProc(Int32, UIntPtr, IntPtr)}} ScreenCapture_to_Clipboard.InterceptKeys.LowLevelKeyboardProc
|
Als Beipiel die Parameterwerte zur Laufzeit:
Hier die Hookproc Paramenter als Rückgabeadresse auf eine Prozedur in der eigenen Anwendung im Debugger als
proc
Und die Rückgabe-Zielprozedur weiter aufgegliedert in Details
Die Zieladresse wurde zuvor definiert in der Klasse mit:
public delegate IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam);
|
Und die eigentliche interne Ziel-Procedure
/// <summary>
/// Actual callback hook.
///
/// <remarks>Calls asynchronously the asyncCallback.</remarks>
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.NoInlining)]
private IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
if (wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_KEYDOWN ||
wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_KEYUP ||
wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_SYSKEYDOWN ||
wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_SYSKEYUP)
hookedKeyboardCallbackAsync.BeginInvoke((InterceptKeys.KeyEvent)wParam.ToUInt32(), Marshal.ReadInt32(lParam), null, null);
return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam);
}
|
betrifft: interne Funktion Notiz