screen2
Goto Top

Cpp Hook installieren

Hallo zusammen,

hab ein kleines Problem bei einem C++ Programm. Ich will auf die Funktion SetWindowsHookEx zugreifen, um einen globalen Hook zu installieren. Das Programm schmiert jedoch immer beim Aufruf dieser Funktion ab. Die vier Parameter hab ich auch durchgecheckt. Laut MSDN muss der zweite Parameter der Pointer auf die Callbackfunktion sein , allerdings ist im Internet manchmal an dieser Stelle ein typedef (HOOKPROC) zu finden. Habs schon auf beide Varianten probiert geht aber nicht. Der dritte Parameter ist das Handle auf die DLL, in der sich die Callbackfunktion befiindet. Dies sollte eigentlich auch stimmen, da der Aufruf der SetWindowsHookEx funktion sich in einer Methode der DLL befindet und diese sauber exportiert worden ist und die Methode bis auf die SetWindowsHookEx einwandfrei abegarbeitet wird. Hier mal mein Programmcode:

Deklaration der Handles:
HHOOK keyHook SHARED = NULL;	// Handle des Hooks
HINSTANCE g_hInst SHARED = NULL;     // Handle der DLL

Das Instance Handle wird nachher in der DllMain noch entsprechen gesetzt.

Installieren des Hooks:
DLLIMPORT bool InstallHook()
{ 
    if(keyHook != NULL)
		return TRUE;
    MessageBox(NULL, "Kontrolle", "Fehler", MB_OK);  
	keyHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, g_hInst, 0); 
	if(keyHook == NULL)
	    return FALSE;
	    
    return TRUE;
}

Aufruf von InstallHook():
bool ret = InstallHook();
                       if (ret == FALSE)
                          MessageBox(NULL,"InstallHook fehlgeschlagen","Fehler",MB_OK);  

Da ich ein Anfänger in punkto C/C++(WinAPI) bitte nicht zu kompliziert erklären. Danke

Gruß screen2

Content-Key: 115112

Url: https://administrator.de/contentid/115112

Printed on: April 25, 2024 at 21:04 o'clock

Member: chfr77
chfr77 May 01, 2009 at 22:19:00 (UTC)
Goto Top
So geht das nicht, dieser Handle der DLL muss außerhalb der installierenden Anwendung verfügbar sein und die DLL separat von der installierenden Anwendung. Also eine extra-dll die nicht von der Anwendung geladen ist, muss explizit geladen werden (LoadLibrary), der handle davon ist dann das was SetWindowsHookEx braucht. Ebenso für die Prozedur daraus. Im MSDN steht das so:

You must place a global hook procedure in a DLL separate from the application installing the hook procedure. The installing application must have the handle to the DLL module before it can install the hook procedure. To retrieve a handle to the DLL module, call the LoadLibrary function with the name of the DLL. After you have obtained the handle, you can call the GetProcAddress function to retrieve a pointer to the hook procedure. Finally, use SetWindowsHookEx to install the hook procedure address in the appropriate hook chain. SetWindowsHookEx passes the module handle, a pointer to the hook-procedure entry point, and 0 for the thread identifier, indicating that the hook procedure should be associated with all threads in the same desktop as the calling thread. This sequence is shown in the following example.
HOOKPROC hkprcSysMsg; 
static HINSTANCE hinstDLL; 
static HHOOK hhookSysMsg; 
 
hinstDLL = LoadLibrary((LPCTSTR) "c:\\windows\\sysmsg.dll");   
hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "SysMessageProc");   
hhookSysMsg = SetWindowsHookEx(WH_SYSMSGFILTER,hkprcSysMsg,hinstDLL,0); 

http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx
Member: screen2
screen2 May 05, 2009 at 20:17:49 (UTC)
Goto Top
Vielen Dank hat gepasst. Hab das MSDN an dieser Stelle etwas missverstanden. Läuft jetzt.

Gruß screen2