Keyboard Hook RRS feed

  • Question

  • Hello,
    I have been trying for quite a while now to install a keyboard hook with no luck at all. I have tried a simple DLL shared application and it works fine meaning i can call the procedure implemented in the dll from my application. The problem is that i am using MFC shared dll application for dll and just a console application for calling application. According to msdn,

    You must place a global hook procedure in a DLL separate from the application installing the hook procedure.
    //This is the code in my dll.h and dll.cpp, first dll.h

    #pragma once

    #ifndef __AFXWIN_H__

    #error "include 'stdafx.h' before including this file for PCH"


    #include "resource.h" // main symbols

    // CsndApp

    // See snd.cpp for the implementation of this class


    LRESULT __declspec( dllexport )__stdcall CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam);

    bool __declspec( dllexport )__stdcall func2();

    class CsndApp : public CWinApp




    // Overrides


    virtual BOOL InitInstance();



    //dll.cpp is

    // snd.cpp : Defines the initialization routines for the DLL.

    #include "stdafx.h"

    #include <windows.h>


    using namespace std;

    #include "snd.h"


    #define new DEBUG_NEW

    #undef THIS_FILE

    static char THIS_FILE[] = __FILE__;


    #pragma data_seg(".SHARDAT")

    static HHOOK hbk=NULL;

    FILE* f1;

    #pragma data_seg()

    HINSTANCE hInst;//

    //TODO: If this DLL is dynamically linked against the MFC DLLs,

    // any functions exported from this DLL which call into

    // MFC must have the AFX_MANAGE_STATE macro added at the

    // very beginning of the function. For example:

    // extern "C" BOOL PASCAL EXPORT ExportedFunction()

    // {// AFX_MANAGE_STATE(AfxGetStaticModuleState());

    // // normal function body here // }

    //// It is very important that this macro appear in each

    // function, prior to any calls into MFC. This means that

    // it must appear as the first statement within the

    // function, even before any object variable declarations

    // as their constructors may generate calls into the MFC

    // DLL.  Please see MFC Technical Notes 33 and 58 for additional

    // details.

    // CsndApp

    // snd.cpp



    // CsndApp construction

    LRESULT __declspec( dllexport )__stdcall CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam){

    //I have commented everything else to simply check if its entering the callback function


    "i am here\n");

    /*if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==nCode))


    if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100))



    if (wParam==VK_RETURN)

    { ch='\n';





    BYTE ks[256];


    WORD w;

    UINT scan;



    ch =char(w);









    LRESULT RetVal = CallNextHookEx( hbk, nCode, wParam, lParam );

    return RetVal;



    bool __declspec( dllexport )__stdcall func2(){




    return true;



    bool __declspec( dllexport )UnHook(){

    bool b=::UnhookWindowsHookEx(hbk);

    return b;




    // TODO: add construction code here,

    // Place all significant initialization in InitInstance


    // The one and only CsndApp object

    CsndApp theApp;

    // CsndApp initialization

    BOOL CsndApp::InitInstance()




    return CWinApp::InitInstance();//TRUE;


    msdn says again:
    The installing application must have the handle to the DLL module before it can install the hook procedure. To retrieve a handle to the DLL module, call the LoadLibrary function with the name of the DLL. After you have obtained the handle, you can call the GetProcAddress function to retrieve a pointer to the hook procedure. Finally, use SetWindowsHookEx to install the hook procedure address in the appropriate hook chain. SetWindowsHookEx passes the module handle, a pointer to the hook-procedure entry point, and 0 for the thread identifier, indicating that the hook procedure should be associated with all threads in the same desktop as the calling thread.

    the calling application is console application in which i am doing the following. i have included the headers and the dll and lib and its fine cuz i have tried it with a simple application before and that worked;

    VOID main(VOID)


    HOOKPROC naurin;

    static HINSTANCE hinstDLL;

    static HHOOK naurinhook;

    hinstDLL = LoadLibrary((LPCTSTR) "c:\\windows\\system32\\snd.dll");

    naurin= (HOOKPROC)GetProcAddress(hinstDLL, "KeyboardProc");

    naurinhook= SetWindowsHookEx(WH_KEYBOARD,naurin,hinstDLL,0);//instkbhook();DWORD iterations;



    what happens is that it creates the file which is supposed to happen in func2 but it doesnot call the callback function because it doesnt print "i am here",

    any suggestions/comments?


    Saturday, July 19, 2008 6:15 PM

All replies

  • Keep in mind that this DLL you created gets injected into every running process.  The plumbing to make printf("I'm here") work is not typically available in those processes.  It requires a console window and a CRT that is properly initialized.  Thread lightly, use OutputDebugString() and SysInternals' wonderful DebugView utility.
    Hans Passant.
    Saturday, July 19, 2008 11:06 PM
  • Hello thanks for ur help but printing  "i am here" was just a debugging technique i was trying to use i tried OutputDebugString and it still doesnt display anything but my primary problem is related to the hook can you please analyze the code to point out what i am doing wrong or sugest how to install a hook without using MFC application on the "calling the dll" side?

    Sunday, July 20, 2008 5:32 AM