Have a look here, accepted answer:
Problem with sendmessage in C#[
^]
I made a short test on it and it does really work.
I hope it helps.
Edit Fast and dirty test code snippet
public partial class FormMain : Form
{
[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
private static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, UInt32 wParam, UInt32 lParam);
Process process = null;
private void buttonStart_Click(object sender, EventArgs e)
{
if (process != null)
return;
process = new Process();
try
{
string winpath = Environment.GetEnvironmentVariable("windir");
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = winpath + @"\system32\notepad.exe";
process.Start();
process.WaitForInputIdle();
{
Thread.Sleep(200);
SetParent(process.MainWindowHandle, this.panelClient.Handle);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonSendKeys_Click(object sender, EventArgs e)
{
if (process == null)
return;
UInt32 WM_KEYDOWN = 0x0100;
UInt32 repeatCount = 13;
UInt32 scanCode = 0x2D;
UInt32 extended = 0;
UInt32 context = 0;
UInt32 previousState = 0;
UInt32 transition = 0;
UInt32 lParam = repeatCount
| (scanCode << 16)
| (extended << 24)
| (context << 29)
| (previousState << 30)
| (transition << 31);
IntPtr editHandle = FindWindowEx(process.MainWindowHandle, IntPtr.Zero, "EDIT", null);
PostMessage(editHandle, WM_KEYDOWN, 'A', lParam);
}
}