简体   繁体   中英

C# Can't Find Java application button

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

If lpClassName is NULL FindWindow will search for the window by the lpWindowName (window's title) only. This is useful if the class of a particular window is variable,

My question, are you giving the correct window's title ?

You can find it by using process explorer - https://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

Let me know if that solved the issue for you.

In case it wasn't helpful - I've found the below code snippet:

private void SendKeysToWindow(string WindowName, string KeysToSend)
    { 
        IntPtr hWnd = FindWindow(null, WindowName);            
        ShowWindow(hWnd, SW_SHOWNORMAL);
        SetForegroundWindow(hWnd);
        Thread.Sleep(50);
        SendKeys.SendWait(KeysToSend);           
    }

Source: Sending keystrokes from a C# application to a Java application - strange behaviour?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM