<?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[Fehler in einem Hook]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich will ein Programm schreiben, welches einen Hook für ein anderes Programm installiert und alle WM_CHAR Nachrichten verändert. Es soll eine Datei in einen globalen char Array geladen und mit einer index Variable durchlaufen werden. In der Hook-Funktion soll danach üperpfüft werden, ob die WM_CHAR Nachricht dem aktuellen char des Array entspicht, falls dies nicht der Fall ist soll der Hook die WM_CHAR nachrichten entsprechend verändern. Die Hookfunktion kann jedoch nicht auf den char Array zuegreifen (das Programm lässt sich zwar fehlerfrei Compilern&amp;Linken, der Array hat in der Hook-Funktion jedoch immer den Wert Null). Wiso ist dies so? Hier der Quellcode der Dll und der exe-Datei:</p>
<pre><code class="language-cpp">/*Die Dll enthätlt 3 Funktionen, die eine Lädt eine Datei in einen globalen char Array, die andere installiert den Hook und die dritte ist die HookFunktion*/ 

#include &lt;windows.h&gt; 

char Schreibvorlage[1024]; 
HHOOK hook; 
HINSTANCE hDllInstance; 
int zaehler=0; 

__declspec (dllexport) LRESULT CALLBACK Filtern(int,WPARAM,LPARAM); 
__declspec (dllexport) LRESULT CALLBACK dateiladen(void); 
__declspec (dllexport) LRESULT CALLBACK Hooksetzen(void); 

int APIENTRY DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved) 
{ 
hDllInstance=hInstance; 
return TRUE; 
} 

__declspec (dllexport) LRESULT CALLBACK Filtern(int nCode, WPARAM wParam, LPARAM lParam) 
{ 
/* Die Hook-Funktion kann seltsamerweise nicht auf den Array Schreibvorlage zugrefien, er Array hat den Wert NULL) 

MSG *Nachricht; 
char v; 

if(nCode&lt;0) 
CallNextHookEx(hook,nCode,wParam,lParam); 

switch(nCode) 
{ 
case HC_ACTION: 
Nachricht=(MSG*) lParam; 
if(Nachricht-&gt;message==WM_CHAR) 
{ 
v=Nachricht-&gt;wParam; 

if(v!=Schreibvorlage[zaehler]) 
{ 
Nachricht-&gt;wParam=Schreibvorlage[zaehler]); 
lParam=(LPARAM)Nachricht; 
zaehler++; 
CallNextHookEx(hook,nCode,wParam,lParam); 
return 0; 
} 
zaehler++; 
return 0; 
} 
default: 
break; 
} 
return 0; 
} 

__declspec (dllexport) LRESULT CALLBACK Hooksetzen(void) 
{ 
PROCESS_INFORMATION pi; 
STARTUPINFO su; 

GetStartupInfo(&amp;su); 

CreateProcess(&quot;C:\\Programme\\Tastaturschreiben\\Tastatur.exe&quot;,NULL,0,0,0,0,NULL,NULL,&amp;su,&amp;pi); 
WaitForInputIdle(pi.hProcess,3000); 

hook = SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)Filtern,hDllInstance,pi.dwThreadId); 
return 0; 
} 

__declspec (dllexport) LRESULT CALLBACK dateiladen(void) 
{ 
static OPENFILENAME ofn; 
HANDLE hFile; 
int dateigroesse; 
DWORD gelesenebytes; 
static char Dateiname[256], Pfad[256]; 
const char szFilter[] = &quot;Schreibvorlage(*.TXT)\0*.txt\0\0&quot;; 
ofn.lStructSize = sizeof(OPENFILENAME); 
ofn.hwndOwner = FindWindow(NULL,&quot;Gosch&quot;); 
ofn.hInstance=NULL; 
ofn.lpstrFilter=szFilter; 
ofn.lpstrCustomFilter=NULL; 
ofn.nMaxCustFilter=0; 
ofn.nFilterIndex=0, 
ofn.lpstrFile=Pfad; 
ofn.nMaxFile=MAX_PATH; 
ofn.lpstrFileTitle=NULL; 
ofn.nMaxFileTitle=MAX_PATH; 
ofn.lpstrInitialDir=NULL; 
ofn.lpstrTitle=Dateiname; 
ofn.Flags=OFN_HIDEREADONLY | OFN_CREATEPROMPT; 
ofn.nFileOffset=0; 
ofn.nFileExtension=0; 
ofn.lpstrDefExt=&quot;txt&quot;; 
ofn.lCustData = 0L; 
ofn.lpfnHook = NULL; 
ofn.lpTemplateName =NULL; 
GetOpenFileName(&amp;ofn); 
hFile=CreateFile(Pfad,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL); 
dateigroesse=GetFileSize(hFile,NULL); 
ReadFile(hFile,Schreibvorlage,dateigroesse,&amp;gelesenebytes,NULL); 
CloseHandle(hFile); 
return 0; 
}
</code></pre>
<pre><code class="language-cpp">/* Die Exe ruft die beiden Funktionen dateiladen() und Hooksetzen auf*/ 

#include &lt;windows.h&gt; 

__declspec (dllexport) LRESULT CALLBACK Filtern(int,WPARAM,LPARAM); 
__declspec (dllexport) LRESULT CALLBACK dateiladen(void); 
__declspec (dllexport) LRESULT CALLBACK Hooksetzen(void); 

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
int WINAPI WinMain (HINSTANCE hInstance, 
               HINSTANCE hPrevInstance, 
               PSTR szCmdLine, 
               int iCmdShow) 

{ 
HWND hwnd; 
MSG msg; 
WNDCLASS wndclass; 
static char szAppName[] = &quot;Gosch&quot;; 

wndclass.style = NULL; 
wndclass.lpfnWndProc = WndProc; 
wndclass.cbClsExtra = 0; 
wndclass.cbWndExtra = 0; 
wndclass.hInstance = hInstance; 
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); 
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
wndclass.lpszMenuName = NULL; 
wndclass.lpszClassName = szAppName; 

RegisterClass(&amp;wndclass); 

hwnd =    CreateWindow(szAppName,szAppName, 
      WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 
      CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); 

UpdateWindow(hwnd); 

while(GetMessage(&amp;msg,NULL,0,0)) 
{ 
TranslateMessage(&amp;msg); 
DispatchMessage(&amp;msg); 
} 

return msg.wParam; 
} 

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
HANDLE hFilterdll; 

   switch (message) 
   { 

case WM_CREATE: 
    dateiladen(); 

    Hooksetzen(); 

    break; 

    case WM_QUIT: 

    break; 
    } 
    return DefWindowProc (hwnd, message, wParam, lParam); 

}
</code></pre>
<p>Ich hoffe ich konnte mein Problem möglichst genau schildern und danke im Voraus für alle Antworten.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/139393/fehler-in-einem-hook</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Jul 2026 05:39:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/139393.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 06 Mar 2006 12:52:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Fehler in einem Hook on Mon, 06 Mar 2006 12:52:00 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich will ein Programm schreiben, welches einen Hook für ein anderes Programm installiert und alle WM_CHAR Nachrichten verändert. Es soll eine Datei in einen globalen char Array geladen und mit einer index Variable durchlaufen werden. In der Hook-Funktion soll danach üperpfüft werden, ob die WM_CHAR Nachricht dem aktuellen char des Array entspicht, falls dies nicht der Fall ist soll der Hook die WM_CHAR nachrichten entsprechend verändern. Die Hookfunktion kann jedoch nicht auf den char Array zuegreifen (das Programm lässt sich zwar fehlerfrei Compilern&amp;Linken, der Array hat in der Hook-Funktion jedoch immer den Wert Null). Wiso ist dies so? Hier der Quellcode der Dll und der exe-Datei:</p>
<pre><code class="language-cpp">/*Die Dll enthätlt 3 Funktionen, die eine Lädt eine Datei in einen globalen char Array, die andere installiert den Hook und die dritte ist die HookFunktion*/ 

#include &lt;windows.h&gt; 

char Schreibvorlage[1024]; 
HHOOK hook; 
HINSTANCE hDllInstance; 
int zaehler=0; 

__declspec (dllexport) LRESULT CALLBACK Filtern(int,WPARAM,LPARAM); 
__declspec (dllexport) LRESULT CALLBACK dateiladen(void); 
__declspec (dllexport) LRESULT CALLBACK Hooksetzen(void); 

int APIENTRY DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved) 
{ 
hDllInstance=hInstance; 
return TRUE; 
} 

__declspec (dllexport) LRESULT CALLBACK Filtern(int nCode, WPARAM wParam, LPARAM lParam) 
{ 
/* Die Hook-Funktion kann seltsamerweise nicht auf den Array Schreibvorlage zugrefien, er Array hat den Wert NULL) 

MSG *Nachricht; 
char v; 

if(nCode&lt;0) 
CallNextHookEx(hook,nCode,wParam,lParam); 

switch(nCode) 
{ 
case HC_ACTION: 
Nachricht=(MSG*) lParam; 
if(Nachricht-&gt;message==WM_CHAR) 
{ 
v=Nachricht-&gt;wParam; 

if(v!=Schreibvorlage[zaehler]) 
{ 
Nachricht-&gt;wParam=Schreibvorlage[zaehler]); 
lParam=(LPARAM)Nachricht; 
zaehler++; 
CallNextHookEx(hook,nCode,wParam,lParam); 
return 0; 
} 
zaehler++; 
return 0; 
} 
default: 
break; 
} 
return 0; 
} 

__declspec (dllexport) LRESULT CALLBACK Hooksetzen(void) 
{ 
PROCESS_INFORMATION pi; 
STARTUPINFO su; 

GetStartupInfo(&amp;su); 

CreateProcess(&quot;C:\\Programme\\Tastaturschreiben\\Tastatur.exe&quot;,NULL,0,0,0,0,NULL,NULL,&amp;su,&amp;pi); 
WaitForInputIdle(pi.hProcess,3000); 

hook = SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)Filtern,hDllInstance,pi.dwThreadId); 
return 0; 
} 

__declspec (dllexport) LRESULT CALLBACK dateiladen(void) 
{ 
static OPENFILENAME ofn; 
HANDLE hFile; 
int dateigroesse; 
DWORD gelesenebytes; 
static char Dateiname[256], Pfad[256]; 
const char szFilter[] = &quot;Schreibvorlage(*.TXT)\0*.txt\0\0&quot;; 
ofn.lStructSize = sizeof(OPENFILENAME); 
ofn.hwndOwner = FindWindow(NULL,&quot;Gosch&quot;); 
ofn.hInstance=NULL; 
ofn.lpstrFilter=szFilter; 
ofn.lpstrCustomFilter=NULL; 
ofn.nMaxCustFilter=0; 
ofn.nFilterIndex=0, 
ofn.lpstrFile=Pfad; 
ofn.nMaxFile=MAX_PATH; 
ofn.lpstrFileTitle=NULL; 
ofn.nMaxFileTitle=MAX_PATH; 
ofn.lpstrInitialDir=NULL; 
ofn.lpstrTitle=Dateiname; 
ofn.Flags=OFN_HIDEREADONLY | OFN_CREATEPROMPT; 
ofn.nFileOffset=0; 
ofn.nFileExtension=0; 
ofn.lpstrDefExt=&quot;txt&quot;; 
ofn.lCustData = 0L; 
ofn.lpfnHook = NULL; 
ofn.lpTemplateName =NULL; 
GetOpenFileName(&amp;ofn); 
hFile=CreateFile(Pfad,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL); 
dateigroesse=GetFileSize(hFile,NULL); 
ReadFile(hFile,Schreibvorlage,dateigroesse,&amp;gelesenebytes,NULL); 
CloseHandle(hFile); 
return 0; 
}
</code></pre>
<pre><code class="language-cpp">/* Die Exe ruft die beiden Funktionen dateiladen() und Hooksetzen auf*/ 

#include &lt;windows.h&gt; 

__declspec (dllexport) LRESULT CALLBACK Filtern(int,WPARAM,LPARAM); 
__declspec (dllexport) LRESULT CALLBACK dateiladen(void); 
__declspec (dllexport) LRESULT CALLBACK Hooksetzen(void); 

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
int WINAPI WinMain (HINSTANCE hInstance, 
               HINSTANCE hPrevInstance, 
               PSTR szCmdLine, 
               int iCmdShow) 

{ 
HWND hwnd; 
MSG msg; 
WNDCLASS wndclass; 
static char szAppName[] = &quot;Gosch&quot;; 

wndclass.style = NULL; 
wndclass.lpfnWndProc = WndProc; 
wndclass.cbClsExtra = 0; 
wndclass.cbWndExtra = 0; 
wndclass.hInstance = hInstance; 
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); 
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
wndclass.lpszMenuName = NULL; 
wndclass.lpszClassName = szAppName; 

RegisterClass(&amp;wndclass); 

hwnd =    CreateWindow(szAppName,szAppName, 
      WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 
      CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); 

UpdateWindow(hwnd); 

while(GetMessage(&amp;msg,NULL,0,0)) 
{ 
TranslateMessage(&amp;msg); 
DispatchMessage(&amp;msg); 
} 

return msg.wParam; 
} 

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
HANDLE hFilterdll; 

   switch (message) 
   { 

case WM_CREATE: 
    dateiladen(); 

    Hooksetzen(); 

    break; 

    case WM_QUIT: 

    break; 
    } 
    return DefWindowProc (hwnd, message, wParam, lParam); 

}
</code></pre>
<p>Ich hoffe ich konnte mein Problem möglichst genau schildern und danke im Voraus für alle Antworten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1009660</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1009660</guid><dc:creator><![CDATA[Gosch]]></dc:creator><pubDate>Mon, 06 Mar 2006 12:52:00 GMT</pubDate></item><item><title><![CDATA[Reply to Fehler in einem Hook on Mon, 06 Mar 2006 17:49:50 GMT]]></title><description><![CDATA[<p>Gosch schrieb:</p>
<blockquote>
<p>Ich hoffe ich konnte mein Problem möglichst genau schildern und danke im Voraus für alle Antworten.</p>
</blockquote>
<p>Wer so viel Source-Code postet, kann das Problem ja nicht wirklich eingekreist zu haben, oder? Somit hast Du es auch auch nicht genau beschrieben...<br />
Für Hooks allgemein siehe:<br />
<a href="http://www.codeproject.com/system/hooksys.asp" rel="nofollow">http://www.codeproject.com/system/hooksys.asp</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1009957</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1009957</guid><dc:creator><![CDATA[Jochen Kalmbach]]></dc:creator><pubDate>Mon, 06 Mar 2006 17:49:50 GMT</pubDate></item></channel></rss>