mal wieder eine der ungezählten hook-fragen
-
hi,
eigentlich möcht ich nur das shift-lock deaktivieren, bin aber nun bei den hooks gelandet und bekomme das ding nicht zum laufen.
werfen lässt sich der hook, aber einholen nicht mehr... auch wird die callback nicht angesprochen.
ka, warum das nicht klappt, dachte ich hätte alles ganz fein zusammenkopiert..
hoffe ihr könnt mir helfen
#include <windows.h> #include <stdio.h> #define DLLEXPORT __declspec(dllexport) #include "deklarations.h" /*DLLEXPORT BOOL installhook(); DLLEXPORT BOOL removehook(); DLLEXPORT LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam);*/ FILE *stream; HHOOK hook = NULL; HWND hwnd = NULL; HINSTANCE hinstance = NULL; HANDLE hMod; char lpMeldung[256]; BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH : case DLL_THREAD_DETACH : case DLL_PROCESS_DETACH: break; } hinstance = (HINSTANCE)hModule; hMod = hModule; hook = NULL; return TRUE; } DLLEXPORT BOOL installhook() { hook = NULL; hook = SetWindowsHookEx(WH_KEYBOARD,hookproc, hinstance,NULL); if(hook==NULL) { MessageBox(NULL,"Unable to install hook","Error!",MB_OK); return FALSE; } else{ MessageBox(NULL,"i'm hooking!","easy!",MB_OK); return TRUE; } } DLLEXPORT BOOL removehook() { if(!UnhookWindowsHookEx(hook)) return FALSE; else{ MessageBox(NULL,"i stopped hooking!","easy!",MB_OK); return TRUE; } } DLLEXPORT LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam) { char szBuffer[256]; int msg; msg=((LPEVENTMSG)lparam)->message; switch(msg) { case WM_KEYDOWN: if(ncode>=0 && LoadString(hinstance,msg,szBuffer,255)!=0) { wsprintf(lpMeldung,"Zeichen: %c\tMessage:\t%s\n",((LPEVENTMSG)lparam)->paramL,szBuffer); MessageBox(NULL,lpMeldung,"Error!",MB_OK); return 0; } return CallNextHookEx(hook,ncode,wparam,lparam); break; } }
-
.. mag wirklich keiner?
-
Du behandelst wParam und lParam falsch. Bei einem WH_KEYBOARD-Hook steht in lParam kein Zeiger auf eine EVENTMSG-Struktur, sondern ein zusammengesetzter Wert aus Wiederholungszahl, Scancode und ein paar Flags. In wParam steht der virtuelle Keycode.
Siehe auch hier: http://msdn.microsoft.com/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Hooks/HookReference/HookFunctions/KeyboardProc.asp
[ Dieser Beitrag wurde am 15.06.2003 um 02:34 Uhr von MFK editiert. ]
-
stimmt, aber da muss noch ein fehler drin sein, da folgende callback keine msgbox erzeugt
DLLEXPORT LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam) { MessageBox(NULL,"lala","",MB_OK); return CallNextHookEx(hook,ncode,wparam,lparam); }
-
Also bei mir funktioniert der Code prima. Vielleicht machst du in der Anwendung etwas falsch?
-
muss dann ja wohl so sein, obwohl ich es kaum glaube, da alles recht verständlich ausschaut:
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <commctrl.h> #include <stdio.h> HINSTANCE hInst; LRESULT CALLBACK MainWindow( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam ); HMODULE hMyLib; typedef BOOL (* INSTALLHOOK)(); typedef BOOL (* REMOVEHOOK)(); INSTALLHOOK InstallHook; REMOVEHOOK RemoveHook; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { hInst=hInstance; return DialogBox (hInst, IDD_MAINWINDOW, NULL, MainWindow); } LRESULT CALLBACK MainWindow( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam ) { switch( message ) { case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { if(!(RemoveHook=(REMOVEHOOK)GetProcAddress(hMyLib, "removehook"))) MessageBox(NULL, "Konnte removehook-funktion nicht ausführen", "error",MB_OK); if(!RemoveHook()) MessageBox(NULL, "Konnte hook nicht wieder einholen", "error",MB_OK); if(!FreeLibrary(hMyLib)) MessageBox(NULL, "Konnte dll nicht entladen", "error",MB_OK); EndDialog(hDlg, LOWORD(wParam)); return TRUE; } if (LOWORD(wParam) == IDC_HBTN_START) { if((hMyLib=LoadLibrary("shiftlock_dll.dll")) == NULL) MessageBox(NULL, "Konnte dll nicht laden" , "error",MB_OK); if(!(InstallHook=(INSTALLHOOK)GetProcAddress(hMyLib, "installhook"))) MessageBox(NULL, "Konnte installhook-funktion nicht ausführen" , "error",MB_OK); if(!InstallHook()) MessageBox(NULL, "Konnte hook nicht werfen" , "error",MB_OK); return TRUE; } return TRUE; } return FALSE; }
-
Und? Wird eine der sechs Messageboxen angezeigt, oder läuft das so durch?
Bei mir wird übrigens die fünfte MessageBox angezeigt, weil der Name in der DLL dekoriert ist (?installhook@@YAHXZ). Damit das so geht, müsste IMHO die Funktion in der DLL als extern "C" deklariert werden.
Warum machst du das überhaupt mit LoadLibrary und GetProcAddress?
-
ich nehme LoadLibrary um damit warm zu werden, brauch das für ein anderes prog.
bei mir lief das programm bis zur msgbox "Konnte hook nicht wieder einholen".
ich hab gerad versucht die gewonnenen erkentnisse umzusetzen, mit dem resultat, dass sich das prg selbst beendet, wenn ich InstallHook() aufrufe..
ich poste dir nochmal alles zusammen (der thread kann dann ja wieder gelöscht werden..
)
und hier die dll:
#ifdef __cplusplus #define DLLEXPORT extern "C" __declspec (dllexport) #else #define DLLEXPORT __declspec (dllexport) #endif #include <windows.h> DLLEXPORT BOOL CALLBACK installhook(); DLLEXPORT BOOL CALLBACK removehook(); HHOOK hook = NULL; HWND hwnd = NULL; HINSTANCE hinstance; LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam); BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: hinstance = hInstance; break; } return TRUE; } LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam) { MessageBox(NULL,"ey?","",MB_OK); return CallNextHookEx(hook,ncode,wparam,lparam); } DLLEXPORT BOOL CALLBACK installhook() { if((hook = SetWindowsHookEx(WH_KEYBOARD,hookproc, hinstance,NULL))==NULL) return FALSE; else return TRUE; } DLLEXPORT BOOL CALLBACK removehook() { if(!UnhookWindowsHookEx(hook)) return FALSE; else return TRUE; }
und hier das fenster
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <commctrl.h> #include <stdio.h> #include "resource.h" typedef BOOL (* INSTALLHOOK)(); typedef BOOL (* REMOVEHOOK)(); INSTALLHOOK InstallHook; REMOVEHOOK RemoveHook; HINSTANCE hInstance; LRESULT CALLBACK MainWindow( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam ); int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HMODULE hMyLib; hInstance= hInst; if( (hMyLib = LoadLibrary("shiftlock_dll.dll")) == NULL) MessageBox(NULL, "Konnte dll nicht laden" , "error",MB_OK); if(!(InstallHook = (INSTALLHOOK)GetProcAddress(hMyLib,"installhook"))) MessageBox(NULL, "Konnte installhook-funktion nicht ausführen" , "error",MB_OK); if(!(RemoveHook = (REMOVEHOOK)GetProcAddress(hMyLib,"removehook" ))) MessageBox(NULL, "Konnte removehook-funktion nicht ausführen", "error",MB_OK); if(!FreeLibrary(hMyLib)) MessageBox(NULL, "Konnte dll nicht entladen", "error",MB_OK); return DialogBox (hInst, IDD_MAINWINDOW, NULL, MainWindow); } LRESULT CALLBACK MainWindow( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam ) { switch( message ) { case WM_COMMAND: if (LOWORD(wParam) == IDC_HBTN_START) { if(!InstallHook())MessageBox(NULL, "Konnte hook nicht werfen" , "error",MB_OK); return TRUE; } if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { if(!RemoveHook())MessageBox(NULL, "Konnte hook nicht wieder einholen", "error",MB_OK); EndDialog(hDlg, LOWORD(wParam)); return TRUE; } return TRUE; } return FALSE; }
-
FreeLibrary darfst du erst aufrufen, wenn du die Funktionen nicht mehr brauchst, also nachdem DialogBox zurückkommt. Davon abgesehen, funktioniert das bei mir.
-
bei mir kommen schon diese beiden msgboxen:
MessageBox(NULL, "Konnte installhook-funktion nicht ausführen" , "error",MB_OK);
MessageBox(NULL, "Konnte removehook-funktion nicht ausführen", "error",MB_OK);hab dir das mal hochgeladen:
http://home.globalserve.de/~fr2233/hook2.zipdanke
-
Du hättest gleich sagen sollen, dass du C-Quellcode verwendest. Da ist es AFAIK nicht möglich, die Namensdekoration zu verhindern. Schau dir doch einfach mal mit Depends die DLL an. Wahrscheinlich lauten die Funktionsnamen "_installhook@0" und "_removehook@0". So musst du sie dann auch bei GetProcAddress angeben. Eine andere Möglichkeit könnte sein, in einer DEF-Datei ein Alias anzugeben.
-
es funktioniert _funktionsname@0
*strahl*
ich dank dir !!