Code:
IntPtr hWnd = FindWindow(null,"*name*");
That shouldn't compile because you can't use any functions in that space. But since it's unmanaged, it'll probably work. And it's a bad idea to search for window handles using FindWindow, period. use
Code:
Process. ... .MainWindowHandle
Code:
FindWindow(null, "*name*");
That won't do anything since the function only does read operations.
You might want to consider this. It's an example of posting a keydown message:
Code:
//MapVirtualKey
[DllImport("user32.dll")]
public static extern int MapVirtualKey(int uCode, uint uMapType);
private void Form1_Load(object sender, EventArgs e)
{
PostMessage(hWnd, (uint)WM_KEYDOWN, (int)Keys.W, MapVirtualKey((int)Keys.W, 0) << 16);
}
Obviously, it sends the W key to the window with the specified handle.
The entire thing (revised):
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
namespace NAMESPACE
{
public partial class Form1 : Form
{
//PostMessage
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
//MapVirtualKey
[DllImport("user32.dll")]
public static extern int MapVirtualKey(int uCode, uint uMapType);
IntPtr hWnd = Process.GetProcessesByName("Firefox")[0].MainWindowHandle;
const int WM_KEYDOWN = 0x100;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
PostMessage(hWnd, (uint)WM_KEYDOWN, (int)Keys.W, MapVirtualKey((int)Keys.W, 0) << 16);
}
}
}