Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 02-18-2004, 02:15 PM   #1 (permalink)
Crazy
 
Location: Raleigh, NC
[c++] windows keyboard hooks

Can someone give me a basic description of how to make a simple keyboard hook? I have looked in the msdn library and am still unclear about how to implement the SetWindowsHookEx() function.

Thanks
__________________
"Good artists copy, great artists steal." - Pablo Picasso

Last edited by Digilogic; 02-18-2004 at 02:31 PM..
Digilogic is offline  
Old 02-23-2004, 05:12 PM   #2 (permalink)
Crazy
 
Location: Raleigh, NC
Wow it seems like nobody that uses the tfp knows anything about windows hook programming :-/
__________________
"Good artists copy, great artists steal." - Pablo Picasso
Digilogic is offline  
Old 02-23-2004, 07:22 PM   #3 (permalink)
kel
WARNING: FLAMMABLE
 
Location: Ask Acetylene
Java... Unless it's interactive I don't really need anything else.
__________________
"It better be funny"
kel is offline  
Old 02-23-2004, 08:28 PM   #4 (permalink)
Junkie
 
Location: San Francisco
I've screwed around with it trying to inject code into another process on Windows 9x (on NT you can just use VirtualAllocEx and CreateRemoteThread but the latter isn't implemented on 9x), but I couldn't get it working and stopped bothering because I figure everyone should be using 2000 or XP by now anyway. ;-) It is kind of confusing and I'm not sure why I couldn't do it before but some code I just wrote seems to work.

Basically you need a
LRESULT CALLBACK HookProc(int, WPARAM, LPARAM),
called by Windows when the thread(s) you are monitoring get whatever type of message, and then you have to call SetWindowsHookEx with a pointer to that function, the handle to the DLL the function is in if necessary, the type of hook, and which threads. If you want the hook on another process you have to put the callback function in a DLL.

First the driver program, as simple as it gets:
Code:
#include <windows.h>

extern "C" void startKeyMonitoring();
extern "C" void stopKeyMonitoring();

int main()
{
	startKeyMonitoring();
	Sleep(10000);
	stopKeyMonitoring();
	return 0;
}
And now the DLL. The .lib file needs to be linked with the driver program for the start and stop functions. It then loads itself at runtime with the LoadLibrary API because you need the handle for SetWindowsHookEx. For some reason Visual Studio is making me use a .def file even though I have dllexport; if anyone knows why please enlighten me. Passing 0 for the dwThreadId parameter of SetWindowsHookEx monitors all threads in the same desktop.

Code:
#include <windows.h>
#include <sstream>

BOOL __stdcall DllMain(HANDLE, DWORD, LPVOID)
{
	return TRUE;
}

HHOOK g_hhk;

extern "C" __declspec(dllexport)
void startKeyMonitoring()
{
	HMODULE hLib = LoadLibrary("hooklib.dll");
	HOOKPROC proc = (HOOKPROC)GetProcAddress(hLib, "KeyProc");
	g_hhk = SetWindowsHookEx(WH_KEYBOARD, proc, hLib, 0);
}

extern "C" __declspec(dllexport)
void stopKeyMonitoring()
{
	UnhookWindowsHookEx(g_hhk);
}

extern "C" __declspec(dllexport)
LRESULT CALLBACK KeyProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	std::ostringstream oss;
	oss << "Here I am in process " << GetCurrentProcessId() << " with a message for you.";
	MessageBox(NULL, oss.str().c_str(), "Hello", MB_OK);
	return CallNextHookEx(g_hhk, nCode, wParam, lParam);
}
And there you go, you get some message boxes with the PID of whatever process received the keyboard messages.

Last edited by n0nsensical; 02-23-2004 at 08:32 PM..
n0nsensical is offline  
 

Tags
hooks, keyboard, windows

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 02:32 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

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