469,317 Members | 1,986 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,317 developers. It's quick & easy.

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 4097
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by daFritz | last post: by
2 posts views Thread by Christoph Brüser | last post: by
reply views Thread by BobAchgill | last post: by
7 posts views Thread by jpierson | last post: by
reply views Thread by Hema | last post: by
1 post views Thread by Louis Cypher | last post: by
1 post views Thread by Louis Cypher | last post: by
2 posts views Thread by Justin | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.