How to use FindWindowEx and SendMessage/PostMessage? RRS feed

  • Question

  • Hi, I'm making a forms app in c# that will press a button in a java application. I have found on how to do it for a Notepad but I don't know how to do it in java. How could I send a "W" key press to a java application? I think I have to change the child but I don't know to what or how to get it.

                        IntPtr hWndNotepad = FindWindow("javaw", null);
                        IntPtr hWndEdit = FindWindowEx(hWndNotepad, IntPtr.Zero, "Edit", null);


    Monday, November 18, 2019 10:18 AM

All replies

  • The code works for Notepad because the edit control is a child window of Notepad's main window.

    You should use a tool like Spy++ to examine the window tree of your java application. 

    FindWindowEx only searches direct child windows of the designated parent.  It does not search descendant child windows.

    So you may need to call FindWindowEx several times to get to your desired target window or use EnumChildWindows (which enumerates all child windows) to find the target of interest.

    Monday, November 18, 2019 10:32 AM
  • Thanks for your reply, but I'm pretty new to this kind of stuff and don't understand how to find my application on spy++ and how to use EnumChildWindows, could you maybe give me some examples of it? 


    Monday, November 18, 2019 10:55 AM
  • Java apps don't have children (see apps like Pixelitor)

    You can try SendInput or SendKeys

    Monday, November 18, 2019 11:00 AM
  • Does SendInput work on non-active apps, apps in the background?


    Monday, November 18, 2019 11:04 AM
  • Does SendInput work on non-active apps, apps in the background?


    Only UIAutomation, but not for Java...

    Monday, November 18, 2019 11:14 AM
  • I need to make it so it presses a key in a java app in the background how could I do that? Is it possible with something else than c# if not on c++? 


    Monday, November 18, 2019 11:36 AM
  • Nothing in this thread is specific to C#.  We're not saying "it can't be done in C#", we're saying "it can't be done."

    Your only choice is to force the application into the foreground using SetForegroundWindow, and then use SendKeys or SendInput.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, November 18, 2019 7:10 PM
  • A critical question is whether the Java application is a browser application (if it uses HTML as the UI) or not. The following are articles about each type. If the Java application is not a browser application then you must learn how to use Spy++.

    The web site scraping article might not help a lot but it describes accessing the HTML DOM from C#. You might not be able to use a WebBrowser control for the Java application but first determine if it is a browser application.

    Sam Hobbs

    Friday, November 22, 2019 6:08 PM