<?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[KeyboardHook, FAQ bsp will nicht funktionieren]]></title><description><![CDATA[<p>Hallo,</p>
<p>also ich will mich in ein anderes programm einhaken und die messages abgreifen. Als einstieg dachte ich mir gucke ich das KeyboardHook FAQ an.</p>
<p>Dann habe ich noch das Forum durchforstet und den Source angepasst, leider will gar nichts funktionieren obwohl die dll und die funktion geladen wird.</p>
<p>hier einmal der Source von &quot;hookdll.h&quot;</p>
<pre><code class="language-cpp">// hookdll.h

#ifdef __cplusplus
#define EXPORT extern &quot;C&quot; __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif
// Funktonalität sowohl unter C als auch unter C++ gegeben

EXPORT BOOL SetupHook (HWND) ;
EXPORT BOOL UninstallHook (void) ;
// Die Prototypen der EXPORT Funktionen

/////////////////////////////////////////////////////////////
</code></pre>
<p>nun der &quot;hookdll.cpp&quot;</p>
<pre><code class="language-cpp">// hookdll.cpp

// Der Keyboardhook wird beim aufruf die Funktion SetupHook (HWND)
// vom Hauptprogramm aus gesetzt. Dabei wird das Fensterhandle des
// aufrufendes Programms an die Funktion übergeben. Als Ergebnis
// sendet die hookdll.dll über die Funktion KeyboardHookProc
// jedes mal wenn eine Taste betätigt wird eine Nachricht
// vom Typ (WM_USER + 2) an das Hauptprogramm. Dabaei steht der
// Tastencode der gedrückten Taste in dem Paramter &quot;wParam&quot;.
// Wenn der Hook nicht mehr benötigt wird, löscht man ihn über die
// Funkion UninstallHook ()

#include &lt;windows.h&gt;
#include &quot;hookdll.h&quot;    /* -&gt; Siehe Oben */

// Weise den Compiler an, die Variable hWindow in einem
// separaten Abschnitt namens Shared unterzubringen
// Darüber hinaus ist dann auch noch dem Linker mitzuteilen,
// dass die Daten in diesem Abschnitt von allen Instanzen
// dieser Anwendung gemeinsam verwendet werden sollen.
// Ganz wichtig dabei ist, dass die Variablen initialisiert
// sein müssen.

#define SHARED __attribute__((section(&quot;.shr&quot;), shared)) 
HWND hWindow = 0 ;

// Weise den Compilern, den Abschnitt Shared als lesbar,
// beschreibbar und zur gemeinsamen Verwendung zu deklarieren - &quot;RSW&quot;.

#pragma comment (linker, &quot;/section:Shared,RWS&quot;)

LRESULT CALLBACK KeyboardHookProc (int, WPARAM, LPARAM) ;
// Der Prototyp der Funktion KeyboardHookProc

HHOOK       hhkHook SHARED=0 ;
HINSTANCE   hDllInstance ;
// Definition globaler Variablen

/************************************************************************/
/* DllMain: wird automatisch aufgerufen, wenn die DLL mit LoadLibrary() */
/*          geladen, oder mit FreeLibrary wieder freigegeben wird.      */
/* Eingabe Parameter: Systemspezifische Daten                           */
/* Return-Wert: TRUE                                                    */
/************************************************************************/

int APIENTRY DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved){
    switch (fdwReason){
        case DLL_PROCESS_ATTACH :
            // Die DLL wird in den Adressraum des aktuellen Prozesses
            // eingeblendet.
            hDllInstance = hInstance ;
            // Initialisierung der globalen Variable
            break ;
    }  
    return TRUE ;
}

/***********************************************************/
/* SetupHook: EXPORT Funktion, setzt den Keyboardhook      */
/* Eingabe Parameter: HWND des aufrufenden Fensters        */
/* Return-Wert: TRUE                                       */
/***********************************************************/

EXPORT BOOL SetupHook (HWND hParent){
    hWindow = hParent ;
    // Initialisierung der globalen Variable mit dem Eingabe Parameter
    hhkHook = SetWindowsHookEx (WH_KEYBOARD_LL, KeyboardHookProc, NULL, GetCurrentThreadId()) ;
    // Keyboardhook setzen und das Ergebnis in der globalen Variable
    // hhkHook speichern.
    return TRUE ;
}

/*******************************************************************/
/* KeyboardHookProc: wird jedes mal aufgerufen, wenn eine Taste    */
/*                   betätigt wird                                 */
/* Eingabe Parameter: Systemspezifische Daten                     */
/* Return-Wert: s. u.                                              */
/*******************************************************************/

LRESULT CALLBACK KeyboardHookProc (int nCode, WPARAM wParam, LPARAM lParam){
    if (nCode == HC_ACTION){
    // Verhindern das eine Nachricht mehrmals verarbeitet wird.
        if ((lParam &amp; 1073741824) != 1073741824){
        // Überprüfen ab vor dem Aufruf dieser Funktion die Taste bereits gedrückt war
            SendMessage ((HWND) hWindow, (WM_USER + 2), (WPARAM) wParam, (LPARAM) lParam) ;
            // Senden der Nachricht (WM_USER + 2) und den Tastencode der gedrückten
            // Taste (gespeichert in &quot;wParam&quot;) an das in der globalen Variable
            // hWindow gespeicherte Fensterhandle.
            MessageBox(HWND_DESKTOP,&quot;blub\n&quot;,&quot;&quot;,IDOK);
        }
    }
    return CallNextHookEx (hhkHook, nCode, wParam, lParam) ;
    // Die Nachrichten an den nächsten Hook weiterreichen.
}

/*********************************************************************/
/* UninstalHook: EXPORT Funktion, löscht den über die Funktion       */
/*               SetupHook gesetzten Hook                            */
/* Eingabe Parameter: keine                                          */
/* Return-Wert: TRUE                                                 */
/*********************************************************************/

EXPORT BOOL UninstallHook (void){
    UnhookWindowsHookEx (hhkHook) ;
    // den in der globalen Variable hhkHook gespeicherten Hook löschen.
    return TRUE ;
}

/////////////////////////////////////////////////////////////////////
</code></pre>
<p>das ganze wird als &quot;hook2_faq_winapi.dll&quot; kompiliert.</p>
<p>Nun zum Hauptprogramm:</p>
<pre><code class="language-cpp">#define _WIN32_WINNT 0x0500
#include &lt;windows.h&gt;
#include &quot;hookdll.h&quot;
#include &lt;iostream&gt;
#include &lt;string.h&gt;
#include &lt;sstream&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
using namespace std;
typedef BOOL (*SetupHookDll)(HWND);
typedef BOOL (*UninstallHookDll)(void);
string int_to_str(int i)
{
ostringstream ret;
ret &lt;&lt; i;
return ret.str();	
}
SetupHookDll     m_pSetupHookDll    = NULL;   
UninstallHookDll    m_pUninstallHookDll  = NULL; 	

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HINSTANCE hGlobalInst;
/*  Make the class name into a global variable  */
char szClassName[ ] = &quot;WindowsApp&quot;;

bool hookinit(HWND);

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&amp;wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           &quot;Windows App&quot;,       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

 hGlobalInst=hThisInstance;
    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&amp;messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&amp;messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&amp;messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hEdit;  

    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
             {    
           hEdit=CreateWindow(&quot;edit&quot;,&quot;&quot;,
           WS_CHILD|WS_VISIBLE, /* default window */
           0,0,100,20,       /* Windows decides the position */

           hwnd,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hGlobalInst,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
           ShowWindow(hEdit,SW_SHOWNORMAL);
            hookinit(hwnd);
           break;
           }
        case (WM_USER+2):
           {  
            char msg[10];
            string temp=int_to_str(wParam);
           SendMessage(hEdit,WM_SETTEXT,(WPARAM)temp.c_str(),(LPARAM)temp.length());
             MessageBox(HWND_DESKTOP,&quot;blub&quot;,&quot;&quot;,IDOK);

            break;
            } 
        case WM_DESTROY:
            //m_pUninstallHookDll();
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
bool hookinit(HWND hWnd)
{
HMODULE hMyDll = NULL;

hMyDll = LoadLibrary(&quot;C:\\hook2_faq_winapi.dll&quot;);
if(hMyDll == NULL)
{
    MessageBox(HWND_DESKTOP,&quot;Dll nicht gefunden!\n&quot;,&quot;&quot;,IDOK);
    return 0;
}
else{MessageBox(HWND_DESKTOP,&quot;DLL gefunden!\n&quot;,&quot;&quot;,IDOK);};

m_pSetupHookDll = (SetupHookDll)GetProcAddress(hMyDll, &quot;SetupHook&quot;);

if(m_pSetupHookDll == NULL)
{
    MessageBox(0,&quot;SetupHookDll Funktionen nicht gefunden!\n&quot;,&quot;&quot;,IDOK);  
    return 0;
}
else{MessageBox(HWND_DESKTOP,&quot;Funktion gefunden!\n&quot;,&quot;&quot;,IDOK);};

m_pSetupHookDll(hWnd); 

return 0;
};
</code></pre>
<p>Interresanter weise bekomme ich einen fehler wenn ich im WM_DESTROY Swtich m_pUninstallHook aufrufe. Daher hier wegegelassen.</p>
<p>Ich benutze den MinGW Compiler.</p>
<p>Es wird auch die MsgBox im KeyboardHookProc nicht aufgerufen.</p>
<p>Wär nett wenn mir einer helfen könnte.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/237886/keyboardhook-faq-bsp-will-nicht-funktionieren</link><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Apr 2026 01:20:44 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/237886.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 03 Apr 2009 09:16:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Fri, 03 Apr 2009 09:16:51 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>also ich will mich in ein anderes programm einhaken und die messages abgreifen. Als einstieg dachte ich mir gucke ich das KeyboardHook FAQ an.</p>
<p>Dann habe ich noch das Forum durchforstet und den Source angepasst, leider will gar nichts funktionieren obwohl die dll und die funktion geladen wird.</p>
<p>hier einmal der Source von &quot;hookdll.h&quot;</p>
<pre><code class="language-cpp">// hookdll.h

#ifdef __cplusplus
#define EXPORT extern &quot;C&quot; __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif
// Funktonalität sowohl unter C als auch unter C++ gegeben

EXPORT BOOL SetupHook (HWND) ;
EXPORT BOOL UninstallHook (void) ;
// Die Prototypen der EXPORT Funktionen

/////////////////////////////////////////////////////////////
</code></pre>
<p>nun der &quot;hookdll.cpp&quot;</p>
<pre><code class="language-cpp">// hookdll.cpp

// Der Keyboardhook wird beim aufruf die Funktion SetupHook (HWND)
// vom Hauptprogramm aus gesetzt. Dabei wird das Fensterhandle des
// aufrufendes Programms an die Funktion übergeben. Als Ergebnis
// sendet die hookdll.dll über die Funktion KeyboardHookProc
// jedes mal wenn eine Taste betätigt wird eine Nachricht
// vom Typ (WM_USER + 2) an das Hauptprogramm. Dabaei steht der
// Tastencode der gedrückten Taste in dem Paramter &quot;wParam&quot;.
// Wenn der Hook nicht mehr benötigt wird, löscht man ihn über die
// Funkion UninstallHook ()

#include &lt;windows.h&gt;
#include &quot;hookdll.h&quot;    /* -&gt; Siehe Oben */

// Weise den Compiler an, die Variable hWindow in einem
// separaten Abschnitt namens Shared unterzubringen
// Darüber hinaus ist dann auch noch dem Linker mitzuteilen,
// dass die Daten in diesem Abschnitt von allen Instanzen
// dieser Anwendung gemeinsam verwendet werden sollen.
// Ganz wichtig dabei ist, dass die Variablen initialisiert
// sein müssen.

#define SHARED __attribute__((section(&quot;.shr&quot;), shared)) 
HWND hWindow = 0 ;

// Weise den Compilern, den Abschnitt Shared als lesbar,
// beschreibbar und zur gemeinsamen Verwendung zu deklarieren - &quot;RSW&quot;.

#pragma comment (linker, &quot;/section:Shared,RWS&quot;)

LRESULT CALLBACK KeyboardHookProc (int, WPARAM, LPARAM) ;
// Der Prototyp der Funktion KeyboardHookProc

HHOOK       hhkHook SHARED=0 ;
HINSTANCE   hDllInstance ;
// Definition globaler Variablen

/************************************************************************/
/* DllMain: wird automatisch aufgerufen, wenn die DLL mit LoadLibrary() */
/*          geladen, oder mit FreeLibrary wieder freigegeben wird.      */
/* Eingabe Parameter: Systemspezifische Daten                           */
/* Return-Wert: TRUE                                                    */
/************************************************************************/

int APIENTRY DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved){
    switch (fdwReason){
        case DLL_PROCESS_ATTACH :
            // Die DLL wird in den Adressraum des aktuellen Prozesses
            // eingeblendet.
            hDllInstance = hInstance ;
            // Initialisierung der globalen Variable
            break ;
    }  
    return TRUE ;
}

/***********************************************************/
/* SetupHook: EXPORT Funktion, setzt den Keyboardhook      */
/* Eingabe Parameter: HWND des aufrufenden Fensters        */
/* Return-Wert: TRUE                                       */
/***********************************************************/

EXPORT BOOL SetupHook (HWND hParent){
    hWindow = hParent ;
    // Initialisierung der globalen Variable mit dem Eingabe Parameter
    hhkHook = SetWindowsHookEx (WH_KEYBOARD_LL, KeyboardHookProc, NULL, GetCurrentThreadId()) ;
    // Keyboardhook setzen und das Ergebnis in der globalen Variable
    // hhkHook speichern.
    return TRUE ;
}

/*******************************************************************/
/* KeyboardHookProc: wird jedes mal aufgerufen, wenn eine Taste    */
/*                   betätigt wird                                 */
/* Eingabe Parameter: Systemspezifische Daten                     */
/* Return-Wert: s. u.                                              */
/*******************************************************************/

LRESULT CALLBACK KeyboardHookProc (int nCode, WPARAM wParam, LPARAM lParam){
    if (nCode == HC_ACTION){
    // Verhindern das eine Nachricht mehrmals verarbeitet wird.
        if ((lParam &amp; 1073741824) != 1073741824){
        // Überprüfen ab vor dem Aufruf dieser Funktion die Taste bereits gedrückt war
            SendMessage ((HWND) hWindow, (WM_USER + 2), (WPARAM) wParam, (LPARAM) lParam) ;
            // Senden der Nachricht (WM_USER + 2) und den Tastencode der gedrückten
            // Taste (gespeichert in &quot;wParam&quot;) an das in der globalen Variable
            // hWindow gespeicherte Fensterhandle.
            MessageBox(HWND_DESKTOP,&quot;blub\n&quot;,&quot;&quot;,IDOK);
        }
    }
    return CallNextHookEx (hhkHook, nCode, wParam, lParam) ;
    // Die Nachrichten an den nächsten Hook weiterreichen.
}

/*********************************************************************/
/* UninstalHook: EXPORT Funktion, löscht den über die Funktion       */
/*               SetupHook gesetzten Hook                            */
/* Eingabe Parameter: keine                                          */
/* Return-Wert: TRUE                                                 */
/*********************************************************************/

EXPORT BOOL UninstallHook (void){
    UnhookWindowsHookEx (hhkHook) ;
    // den in der globalen Variable hhkHook gespeicherten Hook löschen.
    return TRUE ;
}

/////////////////////////////////////////////////////////////////////
</code></pre>
<p>das ganze wird als &quot;hook2_faq_winapi.dll&quot; kompiliert.</p>
<p>Nun zum Hauptprogramm:</p>
<pre><code class="language-cpp">#define _WIN32_WINNT 0x0500
#include &lt;windows.h&gt;
#include &quot;hookdll.h&quot;
#include &lt;iostream&gt;
#include &lt;string.h&gt;
#include &lt;sstream&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
using namespace std;
typedef BOOL (*SetupHookDll)(HWND);
typedef BOOL (*UninstallHookDll)(void);
string int_to_str(int i)
{
ostringstream ret;
ret &lt;&lt; i;
return ret.str();	
}
SetupHookDll     m_pSetupHookDll    = NULL;   
UninstallHookDll    m_pUninstallHookDll  = NULL; 	

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HINSTANCE hGlobalInst;
/*  Make the class name into a global variable  */
char szClassName[ ] = &quot;WindowsApp&quot;;

bool hookinit(HWND);

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&amp;wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           &quot;Windows App&quot;,       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

 hGlobalInst=hThisInstance;
    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&amp;messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&amp;messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&amp;messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hEdit;  

    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
             {    
           hEdit=CreateWindow(&quot;edit&quot;,&quot;&quot;,
           WS_CHILD|WS_VISIBLE, /* default window */
           0,0,100,20,       /* Windows decides the position */

           hwnd,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hGlobalInst,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
           ShowWindow(hEdit,SW_SHOWNORMAL);
            hookinit(hwnd);
           break;
           }
        case (WM_USER+2):
           {  
            char msg[10];
            string temp=int_to_str(wParam);
           SendMessage(hEdit,WM_SETTEXT,(WPARAM)temp.c_str(),(LPARAM)temp.length());
             MessageBox(HWND_DESKTOP,&quot;blub&quot;,&quot;&quot;,IDOK);

            break;
            } 
        case WM_DESTROY:
            //m_pUninstallHookDll();
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
bool hookinit(HWND hWnd)
{
HMODULE hMyDll = NULL;

hMyDll = LoadLibrary(&quot;C:\\hook2_faq_winapi.dll&quot;);
if(hMyDll == NULL)
{
    MessageBox(HWND_DESKTOP,&quot;Dll nicht gefunden!\n&quot;,&quot;&quot;,IDOK);
    return 0;
}
else{MessageBox(HWND_DESKTOP,&quot;DLL gefunden!\n&quot;,&quot;&quot;,IDOK);};

m_pSetupHookDll = (SetupHookDll)GetProcAddress(hMyDll, &quot;SetupHook&quot;);

if(m_pSetupHookDll == NULL)
{
    MessageBox(0,&quot;SetupHookDll Funktionen nicht gefunden!\n&quot;,&quot;&quot;,IDOK);  
    return 0;
}
else{MessageBox(HWND_DESKTOP,&quot;Funktion gefunden!\n&quot;,&quot;&quot;,IDOK);};

m_pSetupHookDll(hWnd); 

return 0;
};
</code></pre>
<p>Interresanter weise bekomme ich einen fehler wenn ich im WM_DESTROY Swtich m_pUninstallHook aufrufe. Daher hier wegegelassen.</p>
<p>Ich benutze den MinGW Compiler.</p>
<p>Es wird auch die MsgBox im KeyboardHookProc nicht aufgerufen.</p>
<p>Wär nett wenn mir einer helfen könnte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1690291</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690291</guid><dc:creator><![CDATA[zeusosc]]></dc:creator><pubDate>Fri, 03 Apr 2009 09:16:51 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Fri, 03 Apr 2009 15:46:40 GMT]]></title><description><![CDATA[<p><strong>Push</strong> (leicht dringend)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1690517</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690517</guid><dc:creator><![CDATA[zeusosc]]></dc:creator><pubDate>Fri, 03 Apr 2009 15:46:40 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Fri, 03 Apr 2009 17:24:37 GMT]]></title><description><![CDATA[<p>Jo,<br />
habe auch festgestellt das der entrypoint in der dll &quot;wohl&quot; nicht angesprochen wird. Die msgbox meldet sich aus der dll nicht.</p>
<p>hmmmm...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1690556</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690556</guid><dc:creator><![CDATA[zeusosc]]></dc:creator><pubDate>Fri, 03 Apr 2009 17:24:37 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Fri, 03 Apr 2009 19:58:47 GMT]]></title><description><![CDATA[<p>Moi, SetWindowsHookEx mit !hmodule und ThreadID!=0 ?</p>
<p>SetWindowsHookEx<br />
[...]<br />
WH_KEYBOARD_LL Global only<br />
[...]</p>
<p>Also</p>
<pre><code class="language-cpp">hhkHook = SetWindowsHookEx (WH_KEYBOARD_LL, KeyboardHookProc, hDllInstance, 0);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1690594</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690594</guid><dc:creator><![CDATA[sapero]]></dc:creator><pubDate>Fri, 03 Apr 2009 19:58:47 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Fri, 03 Apr 2009 20:19:22 GMT]]></title><description><![CDATA[<p>zeusosc schrieb:</p>
<blockquote>
<pre><code class="language-cpp">// Weise den Compiler an, die Variable hWindow in einem
// separaten Abschnitt namens Shared unterzubringen
// Darüber hinaus ist dann auch noch dem Linker mitzuteilen,
// dass die Daten in diesem Abschnitt von allen Instanzen
// dieser Anwendung gemeinsam verwendet werden sollen.
// Ganz wichtig dabei ist, dass die Variablen initialisiert
// sein müssen.

#define SHARED __attribute__((section(&quot;.shr&quot;), shared)) 
HWND hWindow = 0 ;

// Weise den Compilern, den Abschnitt Shared als lesbar,
// beschreibbar und zur gemeinsamen Verwendung zu deklarieren - &quot;RSW&quot;.

#pragma comment (linker, &quot;/section:Shared,RWS&quot;)
</code></pre>
</blockquote>
<p>Ich habe nicht alles gelesen, stolpere nur hier über den Kommentar und die folgenden Instruktionen:<br />
Laut Kommentar soll hWindow in einem shared-Segment untergebracht werden, das tust Du aber nicht. Du definierst zwar ein entsprechendes Makro für den MinGW, aber benutzt es nicht bei Definition von hWindow.<br />
Das<br />
#pragma comment (linker, &quot;/section:Shared,RWS&quot;)<br />
kannst Du hingegen weglassen, das gehört zu den MS-Compilern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1690608</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690608</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Fri, 03 Apr 2009 20:19:22 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Sat, 04 Apr 2009 12:31:08 GMT]]></title><description><![CDATA[<p>Also ich habe das folgend geändert:</p>
<pre><code class="language-cpp">#define SHARED __attribute__((section(&quot;.shr&quot;), shared))
HWND SHARED hWindow = 0 ;
HHOOK SHARED       hhkHook =0 ;
HINSTANCE SHARED   hDllInstance =0; ;

// Weise den Compilern, den Abschnitt Shared als lesbar,
// beschreibbar und zur gemeinsamen Verwendung zu deklarieren - &quot;RSW&quot;.

LRESULT CALLBACK KeyboardHookProc (int, WPARAM, LPARAM) ;
// Der Prototyp der Funktion KeyboardHookProc

// Definition globaler Variablen

/************************************************************************/
/* DllMain: wird automatisch aufgerufen, wenn die DLL mit LoadLibrary() */
/*          geladen, oder mit FreeLibrary wieder freigegeben wird.      */
/* Eingabe Parameter: Systemspezifische Daten                           */
/* Return-Wert: TRUE                                                    */
/************************************************************************/

BOOL WINAPI DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved){

    MessageBox(HWND_DESKTOP,&quot;blub\n&quot;,&quot;&quot;,IDOK);
    switch (fdwReason){
        case DLL_PROCESS_ATTACH :
            // Die DLL wird in den Adressraum des aktuellen Prozesses
            // eingeblendet.
            hDllInstance = hInstance ;
            // Initialisierung der globalen Variable
            break ;
    }  
    return TRUE ;
}
</code></pre>
<p>der entry point DllMain wird anscheinend immer noch nicht aufgerufen,...</p>
<p>hmmm solangsam weiß ich nicht mehr weiter...</p>
<p>gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1690829</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690829</guid><dc:creator><![CDATA[zeusosc]]></dc:creator><pubDate>Sat, 04 Apr 2009 12:31:08 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Sat, 04 Apr 2009 13:39:21 GMT]]></title><description><![CDATA[<p>Die keyboard hooking FAQ hier ist halt veraltet. Heutzutage (Win2000 und neuer) funktioniert es ganz einfach (ohne DLL, ohne Injection, ohne Exports):</p>
<pre><code class="language-cpp">#include &lt;Windows.h&gt;
#include &lt;tchar.h&gt;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WindowProcedure(HWND window, UINT messageId, WPARAM wordParameter, LPARAM longParameter);
LRESULT CALLBACK LowLevelKeyboardHookProcedure(int code, WPARAM wordParameter, LPARAM longParameter);

// required for the hook procedure as the message recipient window
HWND g_mainWindow = NULL;

int APIENTRY _tWinMain
(
	HINSTANCE instance,
	HINSTANCE previousInstance,
	LPTSTR    commandLine,
	int       showWindowCommand
)
{
	UNREFERENCED_PARAMETER(previousInstance);
	UNREFERENCED_PARAMETER(commandLine);

	// low level hooking requires us to have a window message processing loop
	{
		const TCHAR* const windowClassName = TEXT(&quot;LLHook&quot;);

		{
			WNDCLASSEX windowClass = {0};
			windowClass.cbSize = sizeof(windowClass);

			windowClass.style         = 0;
			windowClass.lpfnWndProc   = WindowProcedure;
			windowClass.cbClsExtra    = 0;
			windowClass.cbWndExtra    = 0;
			windowClass.hInstance     = instance;
			windowClass.hIcon         = NULL;
			windowClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
			windowClass.hbrBackground = (HBRUSH)(INT_PTR)(COLOR_WINDOW + 1);
			windowClass.lpszMenuName  = NULL;
			windowClass.lpszClassName = windowClassName;
			windowClass.hIconSm       = NULL;

			{
				const ATOM registerClassResult = RegisterClassEx(&amp;windowClass);
				if (registerClassResult == 0)
					// If the function terminates before entering the message loop, it should return zero.
					return 0;
			}
		}

		{
			const HWND mainWindow = CreateWindow
			(
				windowClassName,
				NULL,
				WS_OVERLAPPEDWINDOW,
				CW_USEDEFAULT,
				0,
				CW_USEDEFAULT,
				0,
				NULL,
				NULL,
				instance,
				NULL
			);

			if (mainWindow == NULL)
				return 0;

			g_mainWindow = mainWindow;

			ShowWindow(mainWindow, showWindowCommand);
			UpdateWindow(mainWindow);
		}
	}

	{
		MSG message;

		// Main message loop
		while (GetMessage(&amp;message, NULL, 0, 0))
			DispatchMessage(&amp;message);

		return (int) message.wParam;
	}
}

LRESULT CALLBACK WindowProcedure (HWND window, UINT messageId, WPARAM wordParameter, LPARAM longParameter)
{
	switch (messageId)
	{
	case WM_CREATE:
		{
			const HHOOK hook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardHookProcedure, NULL, 0);
			if (hook == NULL)
				return -1;
			SetLastError(0);
			SetWindowLongPtr(window, GWLP_USERDATA, (LONG_PTR)hook);
			{
				const DWORD setWindowLongPointerError = GetLastError();
				if (setWindowLongPointerError != 0)
					return -1;
			}
		}
		break;

	case WM_USER:
		{
			MessageBeep(0);
		}
		break;

	case WM_PAINT:
		{
			PAINTSTRUCT paintStructure = {0};
			BeginPaint(window, &amp;paintStructure);
			EndPaint(window, &amp;paintStructure);
		}
		break;

	case WM_DESTROY:
		{
			const HHOOK hook = (HHOOK)SetWindowLongPtr(window, GWLP_USERDATA, 0);
			if (hook != NULL)
				UnhookWindowsHookEx(hook);
			PostQuitMessage(0);
		}
		break;

	default:
		return DefWindowProc(window, messageId, wordParameter, longParameter);
	}
	return 0;
}

LRESULT CALLBACK LowLevelKeyboardHookProcedure (int code, WPARAM wordParameter, LPARAM longParameter)
{
	if (code == HC_ACTION)
		SendMessage(g_mainWindow, WM_USER, 0, 0);
	return CallNextHookEx(NULL, code, wordParameter, longParameter);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1690873</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690873</guid><dc:creator><![CDATA[Superlexx]]></dc:creator><pubDate>Sat, 04 Apr 2009 13:39:21 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Sat, 04 Apr 2009 18:23:06 GMT]]></title><description><![CDATA[<p>ok, thx erstmal. Da taucht beim call der <strong>SetWindowHookEx</strong> ein error auf,<br />
und zwar</p>
<blockquote>
<p>ERROR_HOOK_NEEDS_HMOD<br />
1428 (0x594)</p>
<p>Cannot set nonlocal hook without a module handle.</p>
</blockquote>
<p>Ich habe überlegt wie ich das problem lösen kann, dabei habe ich aus msdn</p>
<p>msdn schrieb:</p>
<blockquote>
<p>hMod<br />
[in] Handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.</p>
</blockquote>
<p>mir überlegt einfache die instance des Progs zu nehmen, leider kommt dann die gleiche fehlermeldung,..</p>
<p>jemand eine idee?</p>
<p>gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1690981</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690981</guid><dc:creator><![CDATA[zeusosc]]></dc:creator><pubDate>Sat, 04 Apr 2009 18:23:06 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Tue, 07 Apr 2009 05:12:17 GMT]]></title><description><![CDATA[<p><strong>*push*</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1692062</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1692062</guid><dc:creator><![CDATA[zeusosc]]></dc:creator><pubDate>Tue, 07 Apr 2009 05:12:17 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Tue, 07 Apr 2009 06:22:26 GMT]]></title><description><![CDATA[<p>hMod ist das Handle der DLL!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1692073</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1692073</guid><dc:creator><![CDATA[Martin Richter]]></dc:creator><pubDate>Tue, 07 Apr 2009 06:22:26 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Tue, 07 Apr 2009 06:33:30 GMT]]></title><description><![CDATA[<p>Jo Martin,</p>
<p>Wie du oben gelesen hast, habe ich schwierigkeiten mit der DLL, da der entrypoint nicht angesprochen wird.<br />
(sonst würde ja die msg Box erscheinen).</p>
<p>Deswegen habe ich aufgrund des vorletzten Post versucht inerhalb der eigenen instance den hook zu erzeugen...</p>
<p>dit will allet net so wie icke wil <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1692076</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1692076</guid><dc:creator><![CDATA[zeusosc]]></dc:creator><pubDate>Tue, 07 Apr 2009 06:33:30 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Tue, 07 Apr 2009 18:41:21 GMT]]></title><description><![CDATA[<p>Ok, ich habe mir jetzt VC++2008 Express runtergeladen um die dll zu erzeugen, diesmal bekomme ich eine meldung des entrypoints <strong>DllMain</strong> ich werde mal in einem seperaten thread nachfragen wo das problem mit dem MinGW , bzw bei mir, liegt.</p>
<p>Ich werde mich jetzt nochmal am hooking probieren,..</p>
<p>grüüße</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1692504</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1692504</guid><dc:creator><![CDATA[zeusosc]]></dc:creator><pubDate>Tue, 07 Apr 2009 18:41:21 GMT</pubDate></item><item><title><![CDATA[Reply to KeyboardHook, FAQ bsp will nicht funktionieren on Thu, 09 Apr 2009 16:42:39 GMT]]></title><description><![CDATA[<p>Sooo ich nochmal:</p>
<p>Wenn ich nur WH_KEYBOARD benutzte, funktioniert lokal alles einwandfrei, ich will mich aber in ein anderes programm hooken, das heisst WH_KEYBOARD_LL, das funktioniert nicht nur seeehhhr träge, sondern schmeisst mir <strong>immer</strong> egal welche taste die werte <strong>256</strong> und <strong>257</strong> raus, ich nehme mal an das das sooo nicht gedacht war,...</p>
<p>daher habe ich zwei fragen:<br />
a) woran kann das liegen und wie kann ich das problem beheben<br />
b) wie kann ich die dll nur auf spezielles programm anwenden und in meinen anderen programm die messages abfangen.</p>
<p>Freue mich auf eure antworten, werde mich in der zwischenzeit belesen,.. gruuuß<br />
-------------------------------------------------------------------------------<br />
<strong>Edit:</strong><br />
Ok das Problem habe ich nun gelöst.<br />
Nun habe ich mit WH_CALLWNDPROC versucht die Messages für einen bestimmten Thread zu bekommen.</p>
<p>Ich bekomme die Message das der Hook instaliert wurde, und ich bekomme ein bzw. zweimal eine nachricht das MsgProc aufgerufen wurde. Wenn ich das Window des Thread aber mit dem Mauszeiger berühre (eine andere Application ist vorher als actives fenster gekennzeichnet), was ja eine Message auslöst, bekomme ich eine Exception,</p>
<blockquote>
<p>Funktion der Adresse 0x7a4...... verweist auf den Speicher 0xffffffff (oder auch 0x000000..). &quot;read&quot; konnte nicht durchgeführt werden.</p>
</blockquote>
<p>Leider will mein debugger nicht, daher weiß ich nicht ganz wie ich herausbekomme ob das ein verweis aus der dll oder aus meinen hauptprogramm ist das diesen Fehler verursacht.</p>
<p>Prinzipiell müsste ich die Adressen aller funktionen mit deren funktionszeiger auflisten, leider weiß ich net wie ich die adressen ohne einen 30zeiler als Hex im string darstellen kann. Wisst Ihr da vlt. Weiter?</p>
<p>( GetProcAddr(hMyDll,irgendeineFkt&quot;) gibt mir leider die vals:00 raus)</p>
<p>Grüüüße und <strong>*push*</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1692551</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1692551</guid><dc:creator><![CDATA[zeusosc]]></dc:creator><pubDate>Thu, 09 Apr 2009 16:42:39 GMT</pubDate></item></channel></rss>