|
Introduction
Hooks are one of the most powerful features of Windows. We can
hooks to trp all the events in the Windows environment. This example
shows how to trap keyboard events and save the keys to a text file.
In the Microsoft® Windows™ operating system, a hook is a mechanism by which
a function can intercept events (messages, mouse actions, keystrokes) before they
reach an application. The function can act on events and, in some cases, modify or
discard them. Functions that receive events are called filter functions and are classified
according to the type of event they intercept. For example, a filter function might
want to receive all keyboard or mouse events. For Windows to call a filter
function, the filter function must be installed — that is, attached to a Windows
hook (for example, to a keyboard hook). Attaching one or more filter functions
to a hook is known as setting a hook. If a hook has more than one filter function attached,
Windows maintains a chain of filter functions. The most recently installed function is at
the beginning of the chain, and the least recently installed function is at the end.
When a hook has one or more filter functions attached and an event occurs that triggers
the hook, Windows calls the first filter function in the filter function chain. This action
is known as calling the hook. For example, if a filter function is attached to the
Computer Based Training (CBT) hook and an event that triggers the hook occurs (for example,
a window is about to be created), Windows calls the CBT hook by calling the first function
in the filter function chain.
To maintain and access filter functions, applications use the
SetWindowsHookEx and the
UnhookWindowsHookEx functions.
An Example
The CALLBACK function in my example is given below..
LRESULT __declspec(dllexport)__stdcall CALLBACK KeyboardProc(int nCode,WPARAM wParam,
LPARAM lParam)
{
char ch;
if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==nCode))
{
if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100))
{
f1=fopen("c:\\report.txt","a+");
if (wParam==VK_RETURN)
{
ch='\n';
fwrite(&ch,1,1,f1);
}
else
{
BYTE ks[256];
GetKeyboardState(ks);
WORD w;
UINT scan=0;
ToAscii(wParam,scan,ks,&w,0);
ch = char(w);
fwrite(&ch,1,1,f1);
}
fclose(f1);
}
}
LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );
return RetVal;
}
The installhook function that is installing the hook function in my example
is given below.
BOOL __declspec(dllexport)__stdcall installhook()
{
f1=fopen("c:\\report.txt","w");
fclose(f1);
hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,0);
return TRUE;
}
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 116 (Total in Forum: 116) (Refresh) | FirstPrevNext |
|
|
 |
|
|
Hey. I'm new to Windows programming, so I'm quite lost.
I downloaded this software. I built hodll.lib and hodll.dll with Microsoft Visual C++.
I built installhook, making sure to point the linker at hodll.lib.
When I run installhook.exe and click the "Install Hook..." button, I get the error:
"Unhandled exception at 0x00000000 in installhook.exe: 0xC0000005: Access violation reading location 0x00000000."
This is on Windows XP SP2.
What may cause this?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I seem to be able to put the hodll.dll into the same directory as installhook.exe and it works.
Can that please be added to the documentation that is distributed?
I guess my issue is with Microsoft Visual C++. I suppose. Not sure what I'm doing wrong.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
When a person type a word "wed", the program will display "wednesday".I want to ask you how to save these words on file and how to do it? This program will setup on system and run on all application (word,notepad,...)
|
| Sign In·View Thread·PermaLink | 1.80/5 (4 votes) |
|
|
|
 |
|
|
Hi,
I wanted to make a program that hides applications (Enemy Territory - WM_KEY..., Quake 4 - DirectInput) which don't accept alt-tab. When I press X, first Quake 4 is to disappear, then if I press X again, it should pop out (X will be changed to something configurable through the loader app). My approach is as follows:
#pragma data_seg (".shared") static bool keys[256]; static const TCHAR CLASS_ET[] = TEXT("Enemy Territory"); static const TCHAR CLASS_Q4[] = TEXT("Quake4"); static int cmdShow = SW_HIDE; // I first hide Quake, then i show it, etc #pragma data_seg ()
and:
LIBRARY "KBD" EXPORTS KeyboardProc @1 InstallHook @2 ReleaseHook @3 SECTIONS .shared SHARED in the .def file.
I've noticed that if i don't do the trick with the shared data segment, the DllMain of my DLL will be called with DLL_PROCESS_ATTACH for each new application, with a different cmdShow state-variable. For example, if I load my DLL, then I go into Quake 4 and press X, Quake 4 will receive a ShowWindow(SW_HIDE), then if I get into Total Commander and press X, Quake 4 will receive
ShowWindow(SW_HIDE). I think that's because a different version of my DLL file exists for the Windows Desktop and a different one for Total Commander (despite LoadLibrary is only called from within my loader application).
Am I correct that my DLL is being injected into all the applications, with the DLL local variables instantiated each time?
Here's the DLL code for reference:
#include #include "KBD.h" #include
static HHOOK hkb; static HINSTANCE hins; static HWND hwnd;
#pragma data_seg (".shared") static bool keys[256]; static const TCHAR CLASS_ET[] = TEXT("Enemy Territory"); static const TCHAR CLASS_Q4[] = TEXT("Quake4"); static int cmdShow = SW_HIDE; #pragma data_seg ()
int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved) { hkb = NULL; hins = hInstance; return TRUE; }
extern "C" LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { FILE *fp = fopen("f:\\src\\C\\SDK\\testkb\\Debug\\log.log", "a+"); if (HC_ACTION == nCode) { if (0 == (static_cast(lParam) & (1 << 31)) && 0 == (static_cast(lParam) & (1 << 30))) { keys[wParam] = true; fprintf(fp, "pressed %c\n", wParam); if (keys['X']) { HWND wnd = FindWindow(CLASS_ET, NULL); if (NULL == wnd) wnd = FindWindow(CLASS_Q4, NULL); if (NULL != wnd) { fprintf(fp, "\tShowWindow(%s)\n", SW_HIDE == cmdShow ? "SW_HIDE" : "SW_SHOW"); ShowWindow(wnd, cmdShow); cmdShow = ((SW_HIDE == cmdShow) ? SW_SHOW : SW_HIDE); } } } else if (static_cast(lParam) & (1 << 31)) { keys[wParam] = false; fprintf(fp, "released %c\n", wParam); } } fflush(fp); fclose(fp); LRESULT RetVal = CallNextHookEx(hkb, nCode, wParam, lParam); return RetVal; }
extern "C" void InstallHook(HWND hWnd) { hwnd = hWnd; hkb = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc, hins, 0); if (NULL == hkb) { TCHAR msg[256]; wsprintf(msg, TEXT("error code: %d"), GetLastError()); MessageBox(NULL, msg, TEXT("error"), MB_ICONERROR); } }
extern "C" void ReleaseHook() { if (hkb != NULL) UnhookWindowsHookEx(hkb); }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I wrote a c program using dev-c++ for keyboard hooks. my hook is a global one. i have given a message box in my hook procedure to display the keypress for trial and error and it is working gobally.
the problem is no other operations are taking place like sending the key value to the main windows textbox though i got the handle of that text box in the dll...
it works only when the window is active and when teh main window is inactive no operation is taking place except mseeage box... what is supposed to be done???
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
yea, i've noticed the same problem here, don't quite know what to think
but in my example program, i've tried to fopen("log.log", "a+") and dump some info to that file. what i've got?
well, i've got a whole bunch of log.log files all over my hard disk: when i'm on the desktop, i get a log.log file on the desktop while in trillian, a log.log file in program files\trillian etc, you get the point
i believe our problems are somehow related
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Using a WH_KEYBOARD hook is efficient to track keyboard messages in many GUI windows applications. But it has its drawbacks such as beeing intercepted by debug hooks and much more: A logging application using such hook policy can not log typed keystrokes at console application such as CMD.EXE. This, makes the heigh level keyboard hook unreliable. To avoid this, we can install a low level keyboard hook ( WH_KEYBOARD_LL ) allowing us controlling all typed keystrokes and bypassing security strategies implemented by some security products ( Zone Alarm ...).
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
I have used a KeyboardHook, but after locking the Workstation the hook ist gone. Also a reinstallation of the Hook didn't work.
Can some one Help me about that?
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Hi,
I am working on an application which can restrict notepad application from moving. For that, I am writing a HOOK dll and using WH_CALLWNDPROC to trap WM_WINDOWPOSCHANGED & WM_WINDOWPOSCHANGING messages. As per the MSDN we can not modify the message in WH_CALLWNDPROC hook.
Is there any way to restrict a window(Notepad, Word, IE) movement ?
Regards
Anuj
Anuj
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
To restrict movement of a window you could use a CBT hook instead and then move the window if the window's position moves.
Another alternative is to use a mouse hook and "throw away" the "down click" and "up click" if the mouse's position is anywhere on the notepad's window.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
The combination keyboard hook should not work how to do?
Esc+a-z Alt+a-z Ctrl+a-z Tab+a-z
my hook should not work for special keys ,can you pls tell me how to do?
chandrasekaran
|
| Sign In·View Thread·PermaLink | 1.20/5 (2 votes) |
|
|
|
 |
|
|
Iam using keyboard hook ,and iam also using WinUser.h
LRESULT CALLBACK SetKeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) { KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT* ) lParam;
if (nCode==HC_ACTION) { BOOL bCtrlKeyDown = GetAsyncKeyState(VK_CONTROL)>>((sizeof(SHORT) * 8) - 1);
if ((pkh->vkCode==VK_ESCAPE && bCtrlKeyDown) || // Ctrl+Esc (pkh->vkCode==VK_TAB && (pkh->flags & LLKHF_ALTDOWN)) || // Alt+TAB (pkh->vkCode==VK_ESCAPE && (pkh->flags & LLKHF_ALTDOWN))|| // Alt+Esc ( bCtrlKeyDown && pkh->vkCode == VK_DELETE )|| (pkh->vkCode==VK_LWIN || pkh->vkCode==VK_RWIN))// || //(pkh->vkCode==VK_F4 && (pkh->flags & LLKHF_ALTDOWN))) { if ((wParam==WM_SYSKEYDOWN||wParam==WM_KEYDOWN)) { MessageBeep(0); // only beep on downstroke if requested } return 1; // gobble it: go directly to jail, do not pass go } } return CallNextHookEx(g_hKeyboadProcHook,nCode,wParam,lParam); //use this to continue further action
return 0; }
Display Error
error C2065: 'KBDLLHOOKSTRUCT' : undeclared identifier : error C2065: 'pkh' : undeclared identifier error C2059: syntax error : ')' error C2227: left of '->vkCode' must point to class/struct/union error C2227: left of '->vkCode' must point to class/struct/union error C2065: 'LLKHF_ALTDOWN' : undeclared identifier error C2227: left of '->vkCode' must point to class/struct/union error C2227: left of '->flags' must point to class/struct/union error C2227: left of '->vkCode' must point to class/struct/union error C2227: left of '->vkCode' must point to class/struct/union error C2227: left of '->vkCode' must point to class/struct/union error C2065: 'WH_KEYBOARD_LL' : undeclared identifier
Kindly help me how to solve this errors .
Chandrasekaran
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
A quick look here shows that you have to have included Windows.h
Not sure if that's the problem, but I'm sure Google can help you quicker than posting here...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Iam new to vc++ i want to write a application when i click buttons through the keyboard the pressed key should display in the message box ,how to do?,Can you pls help me and i need sample application .
|
| Sign In·View Thread·PermaLink | 1.25/5 (4 votes) |
|
|
|
 |
|
|
As usual, excellent work. I combined it woth Plamen Pletkov's journaller and got an excellent little module.
JR
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
void CInstallhookDlg::OnOk() { static HINSTANCE hinstDLL; typedef BOOL (CALLBACK *inshook)(); inshook instkbhook; hinstDLL = LoadLibrary((LPCTSTR) "hodll.dll"); instkbhook = (inshook)GetProcAddress(hinstDLL, "installhook");
if(!instkbhook) return;//added for validating the acquired func pointer. instkbhook(); ShowWindow(SW_MINIMIZE); }
BTW, Nice article. My 5.
Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hi all my brothers/sisters!
I am new in WindowAPI programming. I want write a simple example that is described as bellow:
My program will connect to a remote computer and send all keyboard events to a specified application on that computer. I did capture all keyboard events that interact with my program using HOOK technology. My program sent events to remote computer and force it to perform events on the specified application but it worked incorrectly (I type "A" on my program but in the specified app it is displayed "a" or something else). I think I need some document or examples to make clear about KEYBOARD (include: Keyboard architecture, keyboard operation).
Can you share me your experiences, docs or examples?
Sorry for my bad English. Thanks in advance!
Tuan Phan
|
| Sign In·View Thread·PermaLink | 1.20/5 (2 votes) |
|
|
|
 |
|
|
Dear all my brothers/sisters.
I have a problem as below:
I want to write a small program captures all events (keyboard events, mouse events) from a program and force another application to perform the event. Hence, I use HOOK technology for the program. And I can capture events but I can't force other application events. Example: I start my program and press key 'A' and I want other program (Notepad, Word,...) to display 'A' character on its window.
Please bing me to sunshine. Please help me with your happiness.
Tuan Phan
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
Hi,
Steps - 1. Capture keyboard key code using HOOK. 2. Convert Key code to unicode value. 3. Set Focus to notepad 4. Use SendInput Win32 API to send that unicode value to notepad.
Anuj
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
Hi Anuj, I tried your method, but SetFocus() needs AttachThreadInput() because notepad is a seperate application.
Do you have a sample code that you can share with?
thanks!
Zhi
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Althought I set the 4th parameter in: hook = SetWindowsHookEx( WH_KEYBOARD,hookproc,hinstance,NULL); to NULL, I opened word and clicked. No event was hooked.
Any ideas ?
Thanks, Udi
|
| Sign In·View Thread·PermaLink | 1.60/5 (5 votes) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|