I'm trying to send a button click to another application, in this case its a Java application. I'm using FindWindow(). I can use SendKeys.SendWait() to send keys to the application window, however when I attempt to click the Register button, Findwindowex() returns 0 for the button pointer. My only thought is that perhaps the FindWindowEx() doesn't like the parent and child handles being the same, but in this case there is no child window handle. Any help would be greatly appreciated.
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
public void Start()
{
IntPtr zero = IntPtr.Zero;
for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++)
{
Thread.Sleep(500);
zero = FindWindow(null, "EDM Autosync Client Login");
}
if (zero != IntPtr.Zero)
{
SetForegroundWindow(zero);
SendKeys.SendWait("username");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("password");
SendKeys.SendWait("{ENTER}");
SendKeys.Flush();
}
}
public void register()
{
IntPtr zero = IntPtr.Zero;
IntPtr hwndChild = IntPtr.Zero;
int BN_CLICKED = 245;
int WM_CLOSE = 16;
for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++)
{
Thread.Sleep(500);
zero = FindWindow(null, "Autosync Connection Registration");
}
if (zero != IntPtr.Zero)
{
SetForegroundWindow(zero);
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("10.75.12.10");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("username");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("password");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.Flush();
for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++)
{
Thread.Sleep(500);
hwndChild = FindWindowEx(zero, IntPtr.Zero, "Button", "Register");
}
SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero);
}
}