A week ago, I was discussing how to simulate keyboard input into another application with C#. While the method of using GetProcessByName() works fine, it seems to be dependent on the Windows performance counters being enabled. I'm not sure if its just my Comodo Firewall which is blocking the writing to the restricted Windows area or its something else, but my old Visual Studio 2003 application could not successfully use the GetProcessByName() method that I used with my Visual Studio 2005 test. The FindWindow API call seems to be the more universal approach of getting the handle of another running Windows application.
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
IntPtr hWnd;
if (externalAppType == "CLASS")
hWnd = FindWindow(externalAppName, null);
else
hWnd = FindWindowByCaption(IntPtr.Zero, externalAppName);
if (hWnd != IntPtr.Zero)
{
if (SetForegroundWindow(hWnd))
{
System.Threading.Thread.Sleep(50);
System.Windows.Forms.SendKeys.SendWait(id + "{ENTER}");
}
}
1 comment:
Post a Comment