Keyboard Hook sendet keine Nachricht



  • Hallo

    wie der Titel schon sagt, möchte ich systemweit alle tastendrücke
    mitbekommen. dazu hab ich mir eine DLL geschrieben, die die Hookfunktion
    enthällt, und eine Testanwendung, die von der DLL Nachrichten bekommt.

    Das klappt aber nicht, es kommt nix bei dem Fenster an 😞

    DLL.cpp

    #pragma data_seg("shared")
    
    HWND hWnd;
    HINSTANCE hdll;
    HHOOK hook;
    
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:shared,RWS")
    
    LRESULT CALLBACK HookProc(int code, WPARAM wParam, LPARAM lParam)
    {
    	if (code == HC_ACTION)
    		SendMessage(hWnd, WM_USER + 123, wParam, lParam);
    	return CallNextHookEx(hook, code, wParam, lParam);
    }
    
    extern "C"
    {
    #define DLL __declspec(dllexport)
    
    	DLL unsigned InstallHook()
    	{
    		if (hook)
    			return 1;
    		hook = SetWindowsHookEx(WH_KEYBOARD, HookProc, hdll, 0);
    		return hook ? 1 : 0;
    	}
    	DLL void UninstallHook()
    	{
    		if (hook)
    			UnhookWindowsHookEx(hook);
    		hook = 0;
    	}
    	DLL void SetWindow(HWND wnd)
    	{
    		hWnd = wnd;
    	}
    }
    BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
    {
    	if (ul_reason_for_call == DLL_PROCESS_ATTACH)
    		hdll = hModule;
    	return 1;
    }
    

    TEST.cpp

    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND hWnd, unsigned msg, WPARAM wParam, LPARAM lParam)
    {
    	if (msg == WM_USER + 123)
    		__asm int 3 // ruft den debugger auf
    	return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    int WINAPI WinMain(HINSTANCE inst, HINSTANCE, char *, int)
    {
    	char buffer[5] = {0};
    	*reinterpret_cast<unsigned *>(buffer) = GetTickCount(); // unique class name
    
    	WNDCLASSA wc;
    	ZeroMemory(&wc, sizeof(wc));
    
    	wc.hInstance = inst;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = buffer;
    
    	RegisterClassA(&wc);
    
    	HWND hWnd = CreateWindowExA(0, wc.lpszClassName, "dummy_window", 0, 0, 0, 0, 0, 0, 0, wc.hInstance, 0); // dummy window
    
    	HMODULE dll = LoadLibraryA("hookdll");
    
    	typedef unsigned (*tinitfunc)();
    	typedef void (*texitfunc)();
    	typedef void (*tsetfunc)(HWND);
    
    	tinitfunc initfunc = (tinitfunc)GetProcAddress(dll, "InstallHook");
    	texitfunc exitfunc = (texitfunc)GetProcAddress(dll, "UninstallHook");
    	tsetfunc setfunc = (tsetfunc)GetProcAddress(dll, "SetWindow");
    
    	initfunc();
    	setfunc(hWnd);
    
    	MSG msg;
    	while (GetMessage(&msg, 0, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    }
    

    Die DLL wird korrekt geladen, die funktionen sind auch richtig.

    Das Fenster, auch wenn man es nicht sieht, wird auch erzeugt.

    Warum bekomme ich keine Nachrichen?
    Wenn ich Manuell SendMessage in der TEST.exe verwende, kommt die
    Nachricht ganz normal an.



  • Du musst den Hook so machen:

    hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookProc, hdll, 0);
    


  • KLG7 schrieb:

    Du musst den Hook so machen:

    hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookProc, hdll, 0);
    

    Nö wieso? WH_KEYBOARD_LL ist doch nur für low-level keyboard input events.
    WH_KEYBOARD stimmt schon



  • War bei mir aber so das der Hook erst gesendet hat wenn man ihn LL machte vorher bei mir nit


Anmelden zum Antworten