Global Keyboard Hook Example?

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Global Keyboard Hook Example?

Postby rlev » Sat Jul 17, 2010 12:04 pm

Hi Guys,

I have a program that start a external win32 app using CreateProcess API.
This external app is controlled by keyboard command....what i would like to do is disable the keyboard in this external app.

I have search online for answers and I found that a global keyboard hook could do this.

I have found a example on : http://www.leunen.com/cbuilder/hook.html but throw me a access violation when I run it.
I work with Codegear BC++ 2009 under Windows Vista.

Also I found this thread:

viewtopic.php?f=10&t=905

I tried to compile the example but I think the code is incomplete.

Can somebody post a example of a global keyboard hook in C++ Builder?

Thanks in advanced
rlev
 
Posts: 3
Joined: Sat Mar 27, 2010 11:08 am

Re: Global Keyboard Hook Example?

Postby gambit47 » Mon Jul 19, 2010 5:08 pm

rlev wrote:I have a program that start a external win32 app using CreateProcess API.
This external app is controlled by keyboard command....what i would like to do is disable the keyboard in this external app.


Is it a Console app or a GUI app?

rlev wrote:I have search online for answers and I found that a global keyboard hook could do this.


You do not need a global hook. CreateProcess() returns the ID of the spawned process's main thread. You can pass that ID to SetWindowsHookEx() to install a thread-specific hook into just the new process without having to hook any other running processes (if the new process is multi-threaded, then you would need a global hook, unless you call SetWindowsHookEx() on each individual thread). You do need to implement your hook inside a DLL, since you are hooking an external process.

Do keep in mind, however, that hooking is not always a good way to go, not to mention that it is subject to OS restrictions, such as security rights, UAC elevation, the inability to hook 64-bit apps with a 32-bit DLL, etc. So you might not be able to install the hook into the other process unless your app is running with admin rights and the app is not a 64-bit app.

rlev wrote:I have found a example on : http://www.leunen.com/cbuilder/hook.html but throw me a access violation when I run it.


You did not say where the AV is occuring. But that code is incomplete. For instance, it is missing a key requirement in global hooks - it is not sharing the HHOOK handle amongst multiple instances of the hook DLL (the code is installing a global hook, which is hooking every running process). That is important so the correct HHOOK value gets passed to CallNextHookEx(), but that in itself is not likely to be causing the AV.

rlev wrote:Also I found this thread:

viewtopic.php?f=10&t=905

I tried to compile the example but I think the code is incomplete.


Why do you think that?
Remy Lebeau (TeamB)
http://www.lebeausoftware.org
User avatar
gambit47
BCBJ Author
BCBJ Author
 
Posts: 472
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA


Return to Technical

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests