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
);
}
}