<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Problem mit MessageHook]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe ein seltsam anmutendes Problem mit einem Hook-Programm.<br />
Ich versuche gerade, ein Beispiel aus Jeffrey Richters Experten-Buch zu verstehen, bekomme es aber gar nicht erst richtig zum Laufen.</p>
<p>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.<br />
Lasse ich diese MessageBox aber weg, piept der System-Lautsprecher einmal kurz und nix passiert, das Programm landet irgendwo im Nirwana.</p>
<p>Ich habe das mal auf das Allernotwendigste gekürzt:</p>
<pre><code class="language-cpp">/******************************************************************************
Module:  LastMsgBoxInfoLib.h
******************************************************************************/
#ifndef LASTMSGBOXINFOLIBAPI 
#define LASTMSGBOXINFOLIBAPI extern &quot;C&quot; __declspec(dllimport)
#endif
///////////////////////////////////////////////////////////////////////////////
LASTMSGBOXINFOLIBAPI BOOL WINAPI LastMsgBoxInfo_HookAllApps(BOOL fInstall, 
   DWORD dwThreadId);
</code></pre>
<pre><code class="language-cpp">/******************************************************************************
Module:  LastMsgBoxInfoLib.cpp
Notices: Copyright (c) 2000 Jeffrey Richter
******************************************************************************/
#define WINVER       0x0502
#include &lt;windows.h&gt;
#include &lt;WindowsX.h&gt;
#include &lt;tchar.h&gt;
#include &lt;stdio.h&gt;

#define LASTMSGBOXINFOLIBAPI extern &quot;C&quot; __declspec(dllexport)
#include &quot;LastMsgBoxInfoLib.h&quot;

// MinGW-Variante für Shared-Daten:
#define SHARED __attribute__((section(&quot;.shr&quot;), 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, &amp;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, &quot;vor SetHook&quot;, &quot;In DLL&quot;, MB_OK);

      // Install the Windows' hook
      g_hhook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, 
         ModuleFromAddress((void*)LastMsgBoxInfo_HookAllApps), dwThreadId);

      MessageBox(0, &quot;nach SetHook&quot;, &quot;In DLL&quot;, MB_OK);

      fOk = (g_hhook != NULL);
   } else {

      MessageBox(0, &quot;vor UnHook&quot;, &quot;In DLL&quot;, MB_OK);

      fOk = UnhookWindowsHookEx(g_hhook);

      MessageBox(0, &quot;nach UnHook&quot;, &quot;In DLL&quot;, MB_OK);

      g_hhook = NULL;
   }

   return(fOk);
}
</code></pre>
<pre><code class="language-cpp">/******************************************************************************
Module:  LastMsgBoxInfo.cpp
Notices: Copyright (c) 2000 Jeffrey Richter
******************************************************************************/

#include &lt;windows.h&gt;
#include &lt;windowsx.h&gt;
#include &lt;tchar.h&gt;
#include &quot;LastMsgBoxInfoLib.h&quot;

int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) {

   if(LastMsgBoxInfo_HookAllApps(TRUE, 0))
   	MessageBox(0, &quot;Hooked&quot;, &quot;Info&quot;, MB_OK);
   else
   	MessageBox(0, &quot;not Hooked&quot;, &quot;Info&quot;, MB_OK);

   LastMsgBoxInfo_HookAllApps(FALSE, 0);
   return(0);
}
</code></pre>
<p>Eine grundsätzliche Frage habe ich noch:<br />
GetMsgProc soll ja wohl von allen Prozessen künftig aufgerufen werden. Wieso muß diese Funktion nicht exportiert werden?</p>
<p>Erstellt wird die DLL und die EXE bei mir mit MinGW.<br />
Meine Frage ist: Wieso kann sich das (Nicht)Anzeigen einer MessageBox dermaßen auswirken?<br />
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 (&quot;nach UnHook&quot;) mit &quot;unknown Software Exception&quot; ab.</p>
<p>Vielleicht hat hier ja jemand eine Idee, welche Probleme in dem Code schlummern?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/231000/problem-mit-messagehook</link><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Apr 2026 18:47:24 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/231000.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 05 Jan 2009 11:45:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit MessageHook on Mon, 05 Jan 2009 11:45:24 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe ein seltsam anmutendes Problem mit einem Hook-Programm.<br />
Ich versuche gerade, ein Beispiel aus Jeffrey Richters Experten-Buch zu verstehen, bekomme es aber gar nicht erst richtig zum Laufen.</p>
<p>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.<br />
Lasse ich diese MessageBox aber weg, piept der System-Lautsprecher einmal kurz und nix passiert, das Programm landet irgendwo im Nirwana.</p>
<p>Ich habe das mal auf das Allernotwendigste gekürzt:</p>
<pre><code class="language-cpp">/******************************************************************************
Module:  LastMsgBoxInfoLib.h
******************************************************************************/
#ifndef LASTMSGBOXINFOLIBAPI 
#define LASTMSGBOXINFOLIBAPI extern &quot;C&quot; __declspec(dllimport)
#endif
///////////////////////////////////////////////////////////////////////////////
LASTMSGBOXINFOLIBAPI BOOL WINAPI LastMsgBoxInfo_HookAllApps(BOOL fInstall, 
   DWORD dwThreadId);
</code></pre>
<pre><code class="language-cpp">/******************************************************************************
Module:  LastMsgBoxInfoLib.cpp
Notices: Copyright (c) 2000 Jeffrey Richter
******************************************************************************/
#define WINVER       0x0502
#include &lt;windows.h&gt;
#include &lt;WindowsX.h&gt;
#include &lt;tchar.h&gt;
#include &lt;stdio.h&gt;

#define LASTMSGBOXINFOLIBAPI extern &quot;C&quot; __declspec(dllexport)
#include &quot;LastMsgBoxInfoLib.h&quot;

// MinGW-Variante für Shared-Daten:
#define SHARED __attribute__((section(&quot;.shr&quot;), 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, &amp;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, &quot;vor SetHook&quot;, &quot;In DLL&quot;, MB_OK);

      // Install the Windows' hook
      g_hhook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, 
         ModuleFromAddress((void*)LastMsgBoxInfo_HookAllApps), dwThreadId);

      MessageBox(0, &quot;nach SetHook&quot;, &quot;In DLL&quot;, MB_OK);

      fOk = (g_hhook != NULL);
   } else {

      MessageBox(0, &quot;vor UnHook&quot;, &quot;In DLL&quot;, MB_OK);

      fOk = UnhookWindowsHookEx(g_hhook);

      MessageBox(0, &quot;nach UnHook&quot;, &quot;In DLL&quot;, MB_OK);

      g_hhook = NULL;
   }

   return(fOk);
}
</code></pre>
<pre><code class="language-cpp">/******************************************************************************
Module:  LastMsgBoxInfo.cpp
Notices: Copyright (c) 2000 Jeffrey Richter
******************************************************************************/

#include &lt;windows.h&gt;
#include &lt;windowsx.h&gt;
#include &lt;tchar.h&gt;
#include &quot;LastMsgBoxInfoLib.h&quot;

int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) {

   if(LastMsgBoxInfo_HookAllApps(TRUE, 0))
   	MessageBox(0, &quot;Hooked&quot;, &quot;Info&quot;, MB_OK);
   else
   	MessageBox(0, &quot;not Hooked&quot;, &quot;Info&quot;, MB_OK);

   LastMsgBoxInfo_HookAllApps(FALSE, 0);
   return(0);
}
</code></pre>
<p>Eine grundsätzliche Frage habe ich noch:<br />
GetMsgProc soll ja wohl von allen Prozessen künftig aufgerufen werden. Wieso muß diese Funktion nicht exportiert werden?</p>
<p>Erstellt wird die DLL und die EXE bei mir mit MinGW.<br />
Meine Frage ist: Wieso kann sich das (Nicht)Anzeigen einer MessageBox dermaßen auswirken?<br />
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 (&quot;nach UnHook&quot;) mit &quot;unknown Software Exception&quot; ab.</p>
<p>Vielleicht hat hier ja jemand eine Idee, welche Probleme in dem Code schlummern?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1639722</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1639722</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Mon, 05 Jan 2009 11:45:24 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit MessageHook on Mon, 05 Jan 2009 12:19:10 GMT]]></title><description><![CDATA[<p>der code ist soweit in ordnung, ist der MinGW krempel mit dem shared datensegment da auch sicher richtig?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1639749</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1639749</guid><dc:creator><![CDATA[TrueHooker]]></dc:creator><pubDate>Mon, 05 Jan 2009 12:19:10 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit MessageHook on Mon, 05 Jan 2009 12:54:15 GMT]]></title><description><![CDATA[<p>Ich denke schon, ich habe das in einem anderen DLL-Testprogramm erfolgreich getestet.</p>
<p><a href="http://gcc.info" rel="nofollow">gcc.info</a> sagt hierzu:</p>
<blockquote>
<p>On Microsoft Windows, in addition to putting variable definitions<br />
in a named section, the section can also be shared among all<br />
running copies of an executable or DLL. For example, this small<br />
program defines shared data by putting it in a named section<br />
`shared' and marking the section shareable:</p>
<p>int foo __attribute__((section (&quot;shared&quot;), shared)) = 0;</p>
<p>int<br />
main()<br />
{<br />
/* Read and write foo. All running<br />
copies see the same value. */<br />
return 0;<br />
}</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1639765</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1639765</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Mon, 05 Jan 2009 12:54:15 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit MessageHook on Mon, 05 Jan 2009 14:35:20 GMT]]></title><description><![CDATA[<p>Ich habe mal eine Frage zu dem Buch &quot;Advanced Windows&quot; von Jeffrey Richter, gib es das auch als Deutsche Auflage?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1639862</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1639862</guid><dc:creator><![CDATA[semtex]]></dc:creator><pubDate>Mon, 05 Jan 2009 14:35:20 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit MessageHook on Mon, 05 Jan 2009 14:42:18 GMT]]></title><description><![CDATA[<p>nich mehr</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1639868</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1639868</guid><dc:creator><![CDATA[nöö]]></dc:creator><pubDate>Mon, 05 Jan 2009 14:42:18 GMT</pubDate></item></channel></rss>