ist es möglich, die funktion SetWindowsHookEx in einer consolen application (ohne jeglicher form) zu verwenden ?
dh ich möchte in einer consolen-app auch die events (zb keyboard-events) abhören können - oder ist dies nur mittels win-application (da message enthalten) möglich ?
hm leider funktioniert es bis jetzt in der consolen app noch nicht - nur eben in einer win app oder wenn ich in der consolen app eine form erzeuge ...
was veranlasst dich zu glauben ,das du eine messagepumpe brauchst?
evtl wird dir hiernach einiges klarer: http://www.pinvoke.net/default.aspx/user32/setwindowshookex.html
was veranlasst dich zu glauben ,das du eine messagepumpe brauchst?
weil es eben erst dann funktioniert, wenn eine form vorhanden ist ...
den linke kenne ich bereits - danke
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace HookMe {
class MyClass {
//Delegate Function HookProc(ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
private HookProc myCallbackDelegate = null;
public MyClass() {
// initialize our delegate
this.myCallbackDelegate = new HookProc(this.MyCallbackFunction);
// setup a keyboard hook
//WH_KEYBOARD_LL = 13,
SetWindowsHookEx(13, this.myCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId());
}
[DllImport("user32.dll")]
protected static extern IntPtr SetWindowsHookEx(int code, HookProc func, IntPtr hInstance, int threadID);
[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(wParam);
//return the value returned by CallNextHookEx
return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace HookMe {
class Program {
static void Main(string[] args) {
MyClass c = new MyClass();
Console.ReadLine();
}
}
}
1.)Ein Fehler, mit dem es garnicht gehen kann: du musst eine referenz auf Myclass am Leben erhalten, weil diese das callback und den delegaten enthält. das ist extrem wichtig bei hooks!
MyClass c = new MyClass();
Console.ReadLine();
GC.KeepAlive(c);
2.)Wenns ned passt, dann ersetz zusätzlich (Gc.KeepALive muss bleiben) Console.ReadLine durch Application.Run();
1.)Ein Fehler, mit dem es garnicht gehen kann: du musst eine referenz auf Myclass am Leben erhalten, weil diese das callback und den delegaten enthält. das ist extrem wichtig bei hooks!
ja das hatte ich in meinen weiteren tests bereits drinnen ...
und genau das meinte ich : Application.Run(); ist ja die message loop ...