<?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[InstallHook lässt sich nur einmal erfolgreich ausführen.]]></title><description><![CDATA[<p>Hallo,<br />
ich habe das Problem, dass mein Hook sich pro Programmstart nur einmal installieren lässt.<br />
Wenn ich also InstallHook(), UnInstallHook() ausführe und den Hook dann wieder installieren möchte mit InstallHook() liefert SetWindowsHookEx() NULL zurück.<br />
Meine dll.h sieht so aus:</p>
<pre><code class="language-cpp">// mhook.h
#ifndef _mhook_
#define _mhook_

#include &lt;windows.h&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#using &lt;system.dll&gt;

using namespace std;

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

using namespace System;
extern &quot;C&quot;
{
	DECLDIR BOOL InstallHook();
	DECLDIR BOOL UninstallHook();

	LRESULT CALLBACK MouseProc(int nCode, WPARAM, LPARAM);
}

#endif
</code></pre>
<p>Meine dll.cpp sieht so aus:</p>
<pre><code class="language-cpp">// Dies ist die Haupt-DLL.

#include &quot;stdafx.h&quot;

#include &quot;mhook.h&quot;

//#define DLL_EXPORT

#pragma data_seg(&quot;shared&quot;)
// Globale Variablen 
HHOOK g_hMouseHook = NULL; // Handle unseres Hooks (als &quot;shared&quot; Deklariert) 
HINSTANCE g_hInst = NULL;     // Handle der DLL selbst 

#pragma data_seg()
#pragma comment(linker, &quot;/SECTION:shared,RWS&quot;)

extern &quot;C&quot;{
	#pragma unmanaged
	BOOL APIENTRY DllMain (	HINSTANCE hInst     /* Library instance handle. */ , 
									DWORD reason        /* Reason this function is being called. */ , 
									LPVOID reserved     /* Not used. */ ){ 
		g_hInst = hInst; 

		return TRUE; 
	}

	DECLDIR BOOL InstallHook(){ 
			if(g_hMouseHook != NULL) 
				return TRUE; 
			g_hMouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInst, 0); 
			if(g_hMouseHook == NULL) {
				DWORD error = GetLastError();
				char* temp;
				temp = new char[100];
				sprintf(temp, &quot;%d&quot;, error);
				MessageBoxA(NULL,LPCSTR(temp) ,NULL,MB_OK);
				delete []temp;
				return FALSE; 
			}
			return TRUE; 
		} 

	DECLDIR BOOL UninstallHook(){ 
		if(g_hMouseHook != NULL) 
		{ 
			BOOL ret = UnhookWindowsHookEx(g_hMouseHook);
			g_hMouseHook = NULL;
			if(ret==FALSE){
				DWORD error = GetLastError();
				char* temp;
				temp = new char[100];
				sprintf(temp, &quot;%d&quot;, error);
				MessageBoxA(NULL,LPCSTR(temp) ,NULL,MB_OK);
				delete [] temp;
				return FALSE;
			}

		} 
		return TRUE; 
	}

	LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)	{ 
		if (nCode &lt; 0) 
		return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam); 
		if(nCode == HC_ACTION) 
			{ 
			if ( (wParam == WM_RBUTTONDOWN) ) { 
				MOUSEHOOKSTRUCT *mhs = (MOUSEHOOKSTRUCT*)lParam; 

				HWND caller = FindWindowA(&quot;WindowsForms10.Window.8.app.0.378734a&quot;,&quot;meinprog&quot;);
				if(caller != NULL) 
					PostMessage(caller, WM_APP+123, 0, MAKELPARAM(mhs-&gt;pt.x, mhs-&gt;pt.y)); 
			} 
		} 
		return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam); 
	}
}
</code></pre>
<p>GetLastError() liefert in der InstallHook dann übrigens auch 0.</p>
<p>Der Load-Code:</p>
<pre><code class="language-cpp">hInstLibrary = LoadLibraryA(&quot;E:\\Eigene Dateien\\Visual Studio 2008\\Projects\\mhook\\Debug\\mhook.dll&quot;);
			if(hInstLibrary){
				InstallHook = (MYFUNC) GetProcAddress(hInstLibrary, &quot;InstallHook&quot;); 
				if(InstallHook == NULL) { 
					MessageBoxA(NULL, &quot;Error: Installhook not found&quot;, &quot;Error&quot;,  MB_ICONHAND|MB_OK|MB_TOPMOST);					
				} 
				UninstallHook = (MYFUNC) GetProcAddress(hInstLibrary, &quot;UninstallHook&quot;); 
				if(UninstallHook == NULL) { 
					MessageBoxA(NULL, &quot;Error: Uninstallhook not found&quot;, &quot;Error&quot;, MB_ICONHAND|MB_OK|MB_TOPMOST); 
					return; 
				}			
			}
</code></pre>
<p>install und uninstall vom hook in der checkedChanged:</p>
<pre><code class="language-cpp">if(!hInstLibrary)
					this-&gt;CBShortcuts-&gt;Checked=false;
				if(this-&gt;CBShortcuts-&gt;Checked){
					if(hInstLibrary){
						BOOL ret = InstallHook();
						if(ret == FALSE) 
							MessageBoxA(NULL, &quot;InstallHook failed!&quot;, &quot;Error!&quot;, MB_OK|MB_TOPMOST); 

					}
					else this-&gt;CBShortcuts-&gt;Checked = false;
				}
				else{
					if(hInstLibrary){
						BOOL ret = UninstallHook();
						if(ret == FALSE) 
							MessageBoxA(NULL, &quot;UninstallHook failed!&quot;, &quot;Error!&quot;, MB_OK|MB_TOPMOST); 
					}
				}
</code></pre>
<p>Hat jemand eine Idee woran es liegt?</p>
<p>danke im voraus<br />
veio</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/208134/installhook-lässt-sich-nur-einmal-erfolgreich-ausführen</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 10:51:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/208134.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 15 Mar 2008 09:59:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to InstallHook lässt sich nur einmal erfolgreich ausführen. on Sat, 15 Mar 2008 09:59:38 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich habe das Problem, dass mein Hook sich pro Programmstart nur einmal installieren lässt.<br />
Wenn ich also InstallHook(), UnInstallHook() ausführe und den Hook dann wieder installieren möchte mit InstallHook() liefert SetWindowsHookEx() NULL zurück.<br />
Meine dll.h sieht so aus:</p>
<pre><code class="language-cpp">// mhook.h
#ifndef _mhook_
#define _mhook_

#include &lt;windows.h&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#using &lt;system.dll&gt;

using namespace std;

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

using namespace System;
extern &quot;C&quot;
{
	DECLDIR BOOL InstallHook();
	DECLDIR BOOL UninstallHook();

	LRESULT CALLBACK MouseProc(int nCode, WPARAM, LPARAM);
}

#endif
</code></pre>
<p>Meine dll.cpp sieht so aus:</p>
<pre><code class="language-cpp">// Dies ist die Haupt-DLL.

#include &quot;stdafx.h&quot;

#include &quot;mhook.h&quot;

//#define DLL_EXPORT

#pragma data_seg(&quot;shared&quot;)
// Globale Variablen 
HHOOK g_hMouseHook = NULL; // Handle unseres Hooks (als &quot;shared&quot; Deklariert) 
HINSTANCE g_hInst = NULL;     // Handle der DLL selbst 

#pragma data_seg()
#pragma comment(linker, &quot;/SECTION:shared,RWS&quot;)

extern &quot;C&quot;{
	#pragma unmanaged
	BOOL APIENTRY DllMain (	HINSTANCE hInst     /* Library instance handle. */ , 
									DWORD reason        /* Reason this function is being called. */ , 
									LPVOID reserved     /* Not used. */ ){ 
		g_hInst = hInst; 

		return TRUE; 
	}

	DECLDIR BOOL InstallHook(){ 
			if(g_hMouseHook != NULL) 
				return TRUE; 
			g_hMouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInst, 0); 
			if(g_hMouseHook == NULL) {
				DWORD error = GetLastError();
				char* temp;
				temp = new char[100];
				sprintf(temp, &quot;%d&quot;, error);
				MessageBoxA(NULL,LPCSTR(temp) ,NULL,MB_OK);
				delete []temp;
				return FALSE; 
			}
			return TRUE; 
		} 

	DECLDIR BOOL UninstallHook(){ 
		if(g_hMouseHook != NULL) 
		{ 
			BOOL ret = UnhookWindowsHookEx(g_hMouseHook);
			g_hMouseHook = NULL;
			if(ret==FALSE){
				DWORD error = GetLastError();
				char* temp;
				temp = new char[100];
				sprintf(temp, &quot;%d&quot;, error);
				MessageBoxA(NULL,LPCSTR(temp) ,NULL,MB_OK);
				delete [] temp;
				return FALSE;
			}

		} 
		return TRUE; 
	}

	LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)	{ 
		if (nCode &lt; 0) 
		return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam); 
		if(nCode == HC_ACTION) 
			{ 
			if ( (wParam == WM_RBUTTONDOWN) ) { 
				MOUSEHOOKSTRUCT *mhs = (MOUSEHOOKSTRUCT*)lParam; 

				HWND caller = FindWindowA(&quot;WindowsForms10.Window.8.app.0.378734a&quot;,&quot;meinprog&quot;);
				if(caller != NULL) 
					PostMessage(caller, WM_APP+123, 0, MAKELPARAM(mhs-&gt;pt.x, mhs-&gt;pt.y)); 
			} 
		} 
		return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam); 
	}
}
</code></pre>
<p>GetLastError() liefert in der InstallHook dann übrigens auch 0.</p>
<p>Der Load-Code:</p>
<pre><code class="language-cpp">hInstLibrary = LoadLibraryA(&quot;E:\\Eigene Dateien\\Visual Studio 2008\\Projects\\mhook\\Debug\\mhook.dll&quot;);
			if(hInstLibrary){
				InstallHook = (MYFUNC) GetProcAddress(hInstLibrary, &quot;InstallHook&quot;); 
				if(InstallHook == NULL) { 
					MessageBoxA(NULL, &quot;Error: Installhook not found&quot;, &quot;Error&quot;,  MB_ICONHAND|MB_OK|MB_TOPMOST);					
				} 
				UninstallHook = (MYFUNC) GetProcAddress(hInstLibrary, &quot;UninstallHook&quot;); 
				if(UninstallHook == NULL) { 
					MessageBoxA(NULL, &quot;Error: Uninstallhook not found&quot;, &quot;Error&quot;, MB_ICONHAND|MB_OK|MB_TOPMOST); 
					return; 
				}			
			}
</code></pre>
<p>install und uninstall vom hook in der checkedChanged:</p>
<pre><code class="language-cpp">if(!hInstLibrary)
					this-&gt;CBShortcuts-&gt;Checked=false;
				if(this-&gt;CBShortcuts-&gt;Checked){
					if(hInstLibrary){
						BOOL ret = InstallHook();
						if(ret == FALSE) 
							MessageBoxA(NULL, &quot;InstallHook failed!&quot;, &quot;Error!&quot;, MB_OK|MB_TOPMOST); 

					}
					else this-&gt;CBShortcuts-&gt;Checked = false;
				}
				else{
					if(hInstLibrary){
						BOOL ret = UninstallHook();
						if(ret == FALSE) 
							MessageBoxA(NULL, &quot;UninstallHook failed!&quot;, &quot;Error!&quot;, MB_OK|MB_TOPMOST); 
					}
				}
</code></pre>
<p>Hat jemand eine Idee woran es liegt?</p>
<p>danke im voraus<br />
veio</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1474690</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1474690</guid><dc:creator><![CDATA[veio]]></dc:creator><pubDate>Sat, 15 Mar 2008 09:59:38 GMT</pubDate></item><item><title><![CDATA[Reply to InstallHook lässt sich nur einmal erfolgreich ausführen. on Sat, 15 Mar 2008 10:54:52 GMT]]></title><description><![CDATA[<p>Kleiner Hinweis :</p>
<p>Das Betriebssystem garantiert nicht, daß eine DLL in jedem Prozess an die gleiche Adresse gemappt wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1474725</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1474725</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sat, 15 Mar 2008 10:54:52 GMT</pubDate></item><item><title><![CDATA[Reply to InstallHook lässt sich nur einmal erfolgreich ausführen. on Sat, 15 Mar 2008 11:14:29 GMT]]></title><description><![CDATA[<p>hm, ich lade die DLL ja nur einmal in meinem Prozess, oder wird die dll noch woanders automatisch gemapped?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1474736</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1474736</guid><dc:creator><![CDATA[veio]]></dc:creator><pubDate>Sat, 15 Mar 2008 11:14:29 GMT</pubDate></item><item><title><![CDATA[Reply to InstallHook lässt sich nur einmal erfolgreich ausführen. on Sat, 15 Mar 2008 11:31:50 GMT]]></title><description><![CDATA[<p>Wenn der letzte Parameter von SetWindowsHookEx 0 ist, dann wird der Hook global und die DLL wird in jeden Prozess gemappt.</p>
<p>Probier mal das :</p>
<pre><code class="language-cpp">g_hMouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInst, GetCurrentThreadId ());
</code></pre>
<p>Dann bleibt der Hook lokal und (vorerst) gut ist. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1474747</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1474747</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sat, 15 Mar 2008 11:31:50 GMT</pubDate></item><item><title><![CDATA[Reply to InstallHook lässt sich nur einmal erfolgreich ausführen. on Sat, 15 Mar 2008 11:56:18 GMT]]></title><description><![CDATA[<p>Dann bekomme ich aber nicht alle mausklicks oder nicht? Und das wäre dann nicht der Sinn an der Sache <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1474761</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1474761</guid><dc:creator><![CDATA[veio]]></dc:creator><pubDate>Sat, 15 Mar 2008 11:56:18 GMT</pubDate></item></channel></rss>