locked
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"

    #endif

    #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

    {

    public:

    CsndApp();

    // Overrides

    public:

    virtual BOOL InitInstance();

    DECLARE_MESSAGE_MAP()

    };

    //dll.cpp is

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

    //
    #include "stdafx.h"

    #include <windows.h>

    #include<iostream>

    using namespace std;

    #include "snd.h"


    #ifdef
    _DEBUG

    #define new DEBUG_NEW

    #undef THIS_FILE

    static char THIS_FILE[] = __FILE__;

    #endif

    #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

    BEGIN_MESSAGE_MAP(CsndApp, CWinApp)

    END_MESSAGE_MAP()

    // 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

    char
    ch;


    printf(
    "i am here\n");

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

    {

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

    {

    f1=fopen("c:\\dog\\text.txt","a+");

    if (wParam==VK_RETURN)

    { ch='\n';

    fwrite(&ch,1,1,f1);

    }

    else

    {

    BYTE ks[256];

    GetKeyboardState(ks);

    WORD w;

    UINT scan;

    scan=0;

    ToAscii(wParam,scan,ks,&w,0);

    ch =char(w);

    fwrite(&ch,1,1,f1);

    }

    fclose(f1);

    }

    */

     

    //}

     

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

    return RetVal;

    }

     

    bool __declspec( dllexport )__stdcall func2(){

    f1=fopen("c:\\dog\\text.txt","w+");

    fclose(f1);

    hbk=::SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hInst,0);

    return true;

    }

     

    bool __declspec( dllexport )UnHook(){

    bool b=::UnhookWindowsHookEx(hbk);

    return b;

    }


    CsndApp::CsndApp()

    {

    // TODO: add construction code here,

    // Place all significant initialization in InitInstance

    }

    // The one and only CsndApp object

    CsndApp theApp;

    // CsndApp initialization

    BOOL CsndApp::InitInstance()

    {

    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    hInst=::AfxGetInstanceHandle();

    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;

    func2();

    }


    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?

    naurin

    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?

    naurin
    Sunday, July 20, 2008 5:32 AM