473,500 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

local keyboard hook

Is it possible to do a local keyboard hook to a process running in a
new thread. The process is a command line application and is
instanciated when a button is pressed on the main form. I have had the
hook working Globally, but this means that the hook logs keystokes
from the main app as well as the keystrokes fom the CMD process. The
hook is WH_KEYBOARD. I have tryed passing the threadId into setkeyhook
and thence to SetWindowsHookEx but the KeyboardProc never gets called.
Has anybody got any ideas?

Apr 16 '07 #1
2 4406
"bizcor" <st***********@metronet.co.ukwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
Is it possible to do a local keyboard hook to a process running in a
new thread. The process is a command line application and is
instanciated when a button is pressed on the main form. I have had the
hook working Globally, but this means that the hook logs keystokes
from the main app as well as the keystrokes fom the CMD process. The
hook is WH_KEYBOARD. I have tryed passing the threadId into setkeyhook
and thence to SetWindowsHookEx but the KeyboardProc never gets called.
Has anybody got any ideas?

Processes do not run in threads, cmd runs a separate process not related to the thread that
spawned this process.
What you need is the thread in the cmd process that owns the main window handle, you can get
this TID by a call (through PInvoke) to "User32" GetWindowThreadProcessId API .
Willy.

Apr 16 '07 #2
Thanks Willy

I have now tried this and cannot get it to work!
can you look at the code snppits below to see if I am doing it
correctly:

Many thanks Steve[bizcor]

Code from Main form....

private void button11_Click(object sender, System.EventArgs e)
{
try
{
ThreadStart mts = new ThreadStart(runApplication);
Thread t = new Thread( mts );
t.Start();
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
}

}

private static void runApplication()
{
try
{
process1 = new Process();
process1.StartInfo.FileName = @"c:\phylip\seqboot.exe";
process1.EnableRaisingEvents = true;
process1.Exited+=new EventHandler(process1_Exited);
logger.SetFileName(@"c:\logger.txt");

// This does not work
//uint threadid1 = GetCurrentThreadId();
//Debug.Write("Current thread id 1 is: ");
//Debug.WriteLine(threadid1);
//uint threadid2 = (uint)AppDomain.GetCurrentThreadId();
//Debug.Write("Current thread id 2 is: ");
//Debug.WriteLine(threadid2);
//logger.StartLogger(threadid1);

process1.Start();

// Try Willys method
// Get processes window handle
IntPtr hdlWin = process1.MainWindowHandle;

// Set trHandle to null ?
IntPtr trdHandle = (IntPtr)0;

//Call method to get window thread
IntPtr retThread = GetWindowThreadProcessId(hdlWin,trdHandle);

Debug.Write("Window Thread is: ");
Debug.WriteLine(retThread.ToInt32());

//Call startlogger to set the hook
logger.StartLogger((uint)retThread.ToInt32());
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
}

Methods from Keylogger class ....
public bool StartLogger(uint byThread)
{
if(!isstarted)
{
FileInfo finfo = new FileInfo(sbDefaultFileName.ToString());
if(File.Exists(sbDefaultFileName.ToString()))
{
finfo.Delete();
}

using (StreamWriter swLog = finfo.CreateText())
{
swLog.Close();
}
finfo = null;
_ptrHookID = SetHook(_proc, byThread);
isstarted = true;
return true;
}
else
{
return false;
}
}

private static IntPtr SetHook(KeyboardProc _proc, uint byThread)
{
//Set hook from the current process
//Use using to allow auto Dispose
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
if(byThread==0)
{
Debug.WriteLine(curModule.ModuleName.ToString());
return SetWindowsHookEx(WH_KEYBOARD, _proc,
GetModuleHandle(curModule.ModuleName), 0);
}
else
{
Debug.WriteLine(curModule.ModuleName.ToString());
return SetWindowsHookEx(WH_KEYBOARD, _proc,
GetModuleHandle(curModule.ModuleName), byThread
);
}
}
Apr 17 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
12980
by: daFritz | last post by:
Hi! I need to prevent Task-Switching in my App, so I tried to implement a low level keyboard hook in a extra class. But it seems that the Parameters are not passed correctly to my Hook function....
2
5409
by: Christoph Brüser | last post by:
Hi, in my application I want to react to certain keys when a context menu is showing. So I installed a keyboard hook, but now whenever a key is pressed when the menu is showing, the application...
0
1371
by: BobAchgill | last post by:
When I try to hook a non letter key like left arrow (37) all is OK. If (Hookstruct.vkCode = 37) Or (Hookstruct.vkCode = 83) And CBool(Hookstruct.flags) Then But, for example, the hook for the...
7
26513
by: jpierson | last post by:
Hi, I am tryin to create a keyboard hook that sends the keystroke ctrl + pause/break. I haven't used keyboard hooks before so I'm not too sure how to use them public int MyKeyboardProc(int...
0
2601
by: Hema | last post by:
hi all, i have implemented a keyboard hook which is active only when IE is open. Its a BHO. I am adding the code below: using System; using System.Runtime.InteropServices; using...
1
6793
by: Louis Cypher | last post by:
I'm working on an application (OEM) using c# that uses input from a keyboard and a USB Barcode Scanner. I need to be able to identify keystrokes from the barcode scanner and remove them from the...
1
11163
by: Louis Cypher | last post by:
I'm working on an application (OEM) using c# that uses input from a keyboard and a USB Barcode Scanner. I need to be able to identify keystrokes from the barcode scanner and remove them from the...
2
1406
by: Frank | last post by:
In a dialog box procedure is there a way to determine if keyboard input is from the keypad or the arrow keys? I need to know if the input is from the main keyboard keys. Thanks
2
5965
by: Justin | last post by:
So I have a keyboard hook that I have implemeted into my c# app. I need to not allow any hotkey actions to be performed when my app is opened. I can capture the key events and handle them if I want...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.