Problem mit MessageHook
-
Hallo,
ich habe ein seltsam anmutendes Problem mit einem Hook-Programm.
Ich versuche gerade, ein Beispiel aus Jeffrey Richters Experten-Buch zu verstehen, bekomme es aber gar nicht erst richtig zum Laufen.Das Merkwürdige ist: Wenn ich vor dem Aufruf von SetWindowsHookEx eine MessageBox ausgebe, um zu sehen, bis wohin mein Programm überhaupt läuft, funktioniert ... naja, schon mal ein bißchen.
Lasse ich diese MessageBox aber weg, piept der System-Lautsprecher einmal kurz und nix passiert, das Programm landet irgendwo im Nirwana.Ich habe das mal auf das Allernotwendigste gekürzt:
/****************************************************************************** Module: LastMsgBoxInfoLib.h ******************************************************************************/ #ifndef LASTMSGBOXINFOLIBAPI #define LASTMSGBOXINFOLIBAPI extern "C" __declspec(dllimport) #endif /////////////////////////////////////////////////////////////////////////////// LASTMSGBOXINFOLIBAPI BOOL WINAPI LastMsgBoxInfo_HookAllApps(BOOL fInstall, DWORD dwThreadId);/****************************************************************************** Module: LastMsgBoxInfoLib.cpp Notices: Copyright (c) 2000 Jeffrey Richter ******************************************************************************/ #define WINVER 0x0502 #include <windows.h> #include <WindowsX.h> #include <tchar.h> #include <stdio.h> #define LASTMSGBOXINFOLIBAPI extern "C" __declspec(dllexport) #include "LastMsgBoxInfoLib.h" // MinGW-Variante für Shared-Daten: #define SHARED __attribute__((section(".shr"), shared)) HHOOK g_hhook SHARED = 0; /////////////////////////////////////////////////////////////////////////////// static LRESULT WINAPI GetMsgProc(int code, WPARAM wParam, LPARAM lParam) { return(CallNextHookEx(g_hhook, code, wParam, lParam)); } /////////////////////////////////////////////////////////////////////////////// // Returns the HMODULE that contains the specified memory address static HMODULE ModuleFromAddress(PVOID pv) { MEMORY_BASIC_INFORMATION mbi; return((VirtualQuery(pv, &mbi, sizeof(mbi)) != 0) ? (HMODULE) mbi.AllocationBase : NULL); } /////////////////////////////////////////////////////////////////////////////// BOOL WINAPI LastMsgBoxInfo_HookAllApps(BOOL fInstall, DWORD dwThreadId) { BOOL fOk; if (fInstall) { /* Wenn ich folgende MessageBox aktiviere, werden alle MessageBox's angezeigt, dann läuft das Programm augenscheinlich durch sämtlichen Code, anderenfalls wird die nächste MB nach SetWindowsHookEx gar nicht angezeigt */ //MessageBox(0, "vor SetHook", "In DLL", MB_OK); // Install the Windows' hook g_hhook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, ModuleFromAddress((void*)LastMsgBoxInfo_HookAllApps), dwThreadId); MessageBox(0, "nach SetHook", "In DLL", MB_OK); fOk = (g_hhook != NULL); } else { MessageBox(0, "vor UnHook", "In DLL", MB_OK); fOk = UnhookWindowsHookEx(g_hhook); MessageBox(0, "nach UnHook", "In DLL", MB_OK); g_hhook = NULL; } return(fOk); }/****************************************************************************** Module: LastMsgBoxInfo.cpp Notices: Copyright (c) 2000 Jeffrey Richter ******************************************************************************/ #include <windows.h> #include <windowsx.h> #include <tchar.h> #include "LastMsgBoxInfoLib.h" int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) { if(LastMsgBoxInfo_HookAllApps(TRUE, 0)) MessageBox(0, "Hooked", "Info", MB_OK); else MessageBox(0, "not Hooked", "Info", MB_OK); LastMsgBoxInfo_HookAllApps(FALSE, 0); return(0); }Eine grundsätzliche Frage habe ich noch:
GetMsgProc soll ja wohl von allen Prozessen künftig aufgerufen werden. Wieso muß diese Funktion nicht exportiert werden?Erstellt wird die DLL und die EXE bei mir mit MinGW.
Meine Frage ist: Wieso kann sich das (Nicht)Anzeigen einer MessageBox dermaßen auswirken?
Ach so, noch was, wenn die MessageBox vor dem Hooken aktiviert wird, wird zwar sämtlicher Code durchlaufen, allerdings stürzt das Programm dann nach Anzeige der letzten MB ("nach UnHook") mit "unknown Software Exception" ab.Vielleicht hat hier ja jemand eine Idee, welche Probleme in dem Code schlummern?
-
der code ist soweit in ordnung, ist der MinGW krempel mit dem shared datensegment da auch sicher richtig?
-
Ich denke schon, ich habe das in einem anderen DLL-Testprogramm erfolgreich getestet.
gcc.info sagt hierzu:
On Microsoft Windows, in addition to putting variable definitions
in a named section, the section can also be shared among all
running copies of an executable or DLL. For example, this small
program defines shared data by putting it in a named section
`shared' and marking the section shareable:int foo __attribute__((section ("shared"), shared)) = 0;
int
main()
{
/* Read and write foo. All running
copies see the same value. */
return 0;
}
-
Ich habe mal eine Frage zu dem Buch "Advanced Windows" von Jeffrey Richter, gib es das auch als Deutsche Auflage?
-
nich mehr