keyboard hook

i am trying to catch keyboard keys pressed but my code is not working can someone help me,i am trying it on a notepad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	if (nCode < 0 || nCode != HC_ACTION)
      return CallNextHookEx(hkb, nCode, wParam, lParam);
	
	fout<<(char)wParam;

   return CallNextHookEx(hkb, nCode, wParam, lParam);
}

BOOL __declspec(dllexport)__stdcall installhook()
{
	fout.open("c:\\reportvv.txt");

	HWND targetWnd;
    HANDLE hProcess;
    unsigned long processID = 0;

	targetWnd = FindWindowA("Notepad",NULL);
    DWORD ID = GetWindowThreadProcessId(targetWnd, &processID);

	hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins, NULL);

	int jx = GetLastError();

	 
	return TRUE;
}
BOOL __declspec(dllexport)  UnHook()
{
    	
     BOOL unhooked = UnhookWindowsHookEx(hkb);
	 fout.close();
     return unhooked;
} 

i am using vs 2005 on windows server 2008 r2
I am not sure about the code, but do You run the code with administrator privileges?
thanks for reply
Yes i have run it with administrator privileges.
The code is incomplete.Where is shared data segment from this DLL implemented?
The dll will alsobe injected in notepad process.
here is cpp file code, i modified it getlasterror() do not return any error code.
but nothing come in log file ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// hookdll2.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "hookdll2.h"
#include<fstream>
using namespace std;


#ifdef _MANAGED
#pragma managed(push, off)
#endif

static HHOOK hkb=NULL;
FILE *f1;
HINSTANCE hins;

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	
	switch (ul_reason_for_call)
	{
		
	case DLL_PROCESS_ATTACH:
		hins = hModule;
		break;
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
		
	}
    return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

// This is an example of an exported variable
HOOKDLL2_API int nhookdll2=0;

// This is an example of an exported function.
HOOKDLL2_API int fnhookdll2(void)
{
	return 42;
}

// This is the constructor of a class that has been exported.
// see hookdll2.h for the class definition
Chookdll2::Chookdll2()
{
	return;
}


ofstream fout;


LRESULT   __declspec(dllexport)__stdcall  CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	if (nCode < 0 || nCode != HC_ACTION)
      return CallNextHookEx(hkb, nCode, wParam, lParam);

	BYTE ks[256];
	WORD w;
	ToAscii(wParam,0,ks,&w,0);
	char ch = char(w);
	if(fout.is_open())
	{
		fout<<ch;
	}

   return CallNextHookEx(hkb, nCode, wParam, lParam);
}

BOOL __declspec(dllexport)__stdcall installhook()
{
	fout.open("c:\\viv.txt",ios::app);

	HWND targetWnd;
    HANDLE hProcess;
    unsigned long processID = 0;

	targetWnd = FindWindowA("Notepad",NULL);
    DWORD ID = GetWindowThreadProcessId(targetWnd, &processID);

	hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins, ID);

	int jx = GetLastError();

	return TRUE;
}
BOOL __declspec(dllexport)  UnHook()
{
    	
     BOOL unhooked = UnhookWindowsHookEx(hkb);
	 fout.close();
     return unhooked;
} 

whatthefuckamireading.jpg
don't worry , you need not to understand , little same work on windows 7 ultimate 32 bit os
with vs 2010 ultimate

but on windows server 2008 r2 standard an 64 bit os with vs 2005 professional edition won't work.

well let me see what makes difference.
i tried new code in windows xp using code blocks how ever here the callback function is not getting called

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "main.h"
#include<fstream>
using namespace std;
// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}

static HHOOK hkb=NULL;
FILE *f1;
HINSTANCE hins;

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
        hins = hinstDLL;
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}

fstream fout;


LRESULT KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	fout<<"here";

	fout<<nCode<<"  "<<wParam<<"  "<<lParam<<endl;

	if (nCode < 0 || nCode != HC_ACTION)
      return CallNextHookEx(hkb, nCode, wParam, lParam);

	if (((DWORD)lParam & 0x80000000) &&(HC_ACTION == nCode))
    {
        if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100))
		{
			BYTE ks[256];
			WORD w;
			ToAscii(wParam,0,ks,&w,0);
			char ch = char(w);
			if(fout.is_open())
			{
				fout<<ch;
			}
		}
	}

   return CallNextHookEx(hkb, nCode, wParam, lParam);
}

extern "C" BOOL DLL_EXPORT installhook()
{
	fout.open("c:\\viv.txt",ios::app);

	HWND targetWnd;
    HANDLE hProcess;
    unsigned long processID = 0;

	targetWnd = FindWindowA("Notepad",NULL);
    DWORD ID = GetWindowThreadProcessId(targetWnd, &processID);

    //SetLastError(0);
	hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,getcurrent);

	int jx = GetLastError();

	return TRUE;
}
extern "C" BOOL DLL_EXPORT  UnHook()
{

     BOOL unhooked = UnhookWindowsHookEx(hkb);
	 fout.close();
     return unhooked;
}



solved many problems but only remain is repeating character how i can remove them



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

LRESULT   __declspec(dllexport)__stdcall  CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	ofstream fout;
	fout.open("c:\\viv.txt",ios::app);

	static BOOL flag = FALSE ;
	static BYTE key ;

	if ( nCode < 0 )
		return CallNextHookEx ( hkb, nCode, wParam, lParam ) ;

	if ( ( nCode == HC_ACTION ) &&( ( DWORD ) lParam & 0x80000000 ) == 0 )
	{
		fout<<(BYTE)wParam;
	}
		
	fout.close();

   return CallNextHookEx(hkb, nCode, wParam, lParam);
}
Solved here is complete keyboard procedure

do not misuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

LRESULT  __declspec(dllexport)__stdcall CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
  ofstream fout;
  fout.open("c:\\viv2.txt",ios::app);

	if (nCode < 0 || nCode != HC_ACTION)
   return CallNextHookEx(hkb, nCode, wParam, lParam);


	if ( (((DWORD)lParam & 0x80000000) == 0) && (HC_ACTION == nCode))
  {
    if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||((wParam>=0x2f ) &&(wParam<=0x100)))
		{
      if(GetAsyncKeyState(wParam) == -32767) //this solve it i got it on google but how ??
			{
        BYTE ks[256];
        GetKeyboardState(ks);

        WORD w;
        UINT scan=0;
        ToAscii(wParam,scan,ks,&w,0);
        fout<<char(w);
			}
		}
	}

	 fout.close();

  return CallNextHookEx(hkb, nCode, wParam, lParam);
}
well now i am trying same with mouse and facing same problem of repeating message

any idea to remove it ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

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

	if (wParam == WM_MOUSEMOVE)
		return CallNextHookEx(hkb, nCode, wParam, lParam);


	ofstream fout;
	fout.open("c:\\viv2.txt",ios::app);
	short retVal;

	//if(GetAsyncKeyState(wParam) == -32767)
	{
		if (nCode == HC_ACTION)
		{
			if (lParam)
			{
				MOUSEHOOKSTRUCT *pMH = reinterpret_cast<MOUSEHOOKSTRUCT *>(lParam);
				
				switch (wParam)
				{
				case WM_LBUTTONDOWN:
					fout<<"here lbutton \n";
					break;
				case WM_MBUTTONDOWN:
					break;
				case WM_RBUTTONDOWN:
					fout<<"here rbutton \n";
					break;
				case WM_LBUTTONDBLCLK:
					fout<<"here Dlbutton \n";
					break;
				case WM_MBUTTONDBLCLK:
					break;
				case WM_RBUTTONDBLCLK:
					fout<<"here rrbut Dlbutton \n";
					break;
				case WM_MOUSEWHEEL:
					break;
				default:
					break;
				}
			}
		}
	}
	fout.close();
	return ::CallNextHookEx(hkb, nCode, wParam, lParam);
}
can you give us a link to explain about this "tutorial" :D ? i'm so eager to learn this...
chipp - search MSDN for "Hooks". The first search result should lead you to an oveerview.
Last edited on
Topic archived. No new replies allowed.