-1

I have a C# Windows Desktop app that needs to handle a given external app, - unmanaged code - and populate it's input fields.

Note that this must be done programatically, so the use of SPY++ to get windows names is out of discussion, I just can get windows classes using SPY++.

Looking and reading over there I'found that it can be accomplished with the following steps (pseducode):

  1. Get or start the external app process.
  2. Get the main window using FindWindow();
  3. Get the childWindows, which are the input fields, using FindWindowEx.
  4. Send messages using Sendmessage().

I've tested populating a notepad, however dealing with my given app has been a PITA. Can't populate anything.

here is my actual code:

  /*
  List of references, which includes System.Diagnostics, 
  System.Runtime.InteropServices,...
 */
  [DllImport("user32.dll")]
  public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

  [DllImport("user32.dll")]
  public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

  [DllImport("user32.dll")]
  public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);


  private void click_test(object sender, RoutedEventArgs e)
    {
       Process[] apps = Process.GetProcessesByName("givenApp");

       if (apps.Length == 0) return;
       if (apps[0] != null)
       {

            IntPtr mainWindow = FindWindow("class name", null);
            IntPtr child = FindWindowEx(apps[0].MainWindowHandle, new IntPtr(0), "Class Name", null);
            SendMessage(child, 0x000C, 0, input_test.Text);
        }
    }

With this code I can populate notepad. However populating notepad is a test stage. I need to populate other app.

What is missing, what is wrong?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
digitai
  • 1,870
  • 2
  • 20
  • 37
  • 2
    Does the external app support UIA (https://learn.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-overview) ? – Dave M Apr 13 '18 at 23:19
  • 1
    If your code works for Notepad but not the app, the problem is probably due to something not in your code example. There is no way we can tell. – John Wu Apr 13 '18 at 23:20
  • 1
    You can find out with an app called inspect: https://msdn.microsoft.com/en-us/library/windows/desktop/dd318521(v=vs.85).aspx – Dave M Apr 13 '18 at 23:23
  • @DaveM - if FindWindowEx works, there's no reason why UIA wouldn't, this is clearly a job for UIA (you should post an answer IMHO). One important thing to check: both the automated app and the automating app must run at the same security/UAC level. – Simon Mourier Apr 14 '18 at 05:09
  • 1
    The crux of the question is the other app. How it is implemented and what automation is supported. With no details of the other app, you aren't going to get anything beyond the comments above. – David Heffernan Apr 14 '18 at 05:53
  • @DaveM Don't know, is a totally unmanaged app, written in Java many years ago. I have SPY in VS2017 and also have windows detective, this is not the problem. – digitai Apr 14 '18 at 18:10
  • Why the downvote, please at least explain, my question conforms to SO standards. – digitai Apr 14 '18 at 18:13

1 Answers1

0

I found what the error was, simply:

When instantiating a child, instead of calling the generic MainWindowHadle, call the App Window handle, which is previously defined in a variable called MainWindow which invokes the FindWindow() Class.

This is wrong:

IntPtr child = FindWindowEx(apps[0].MainWindowHandle, new IntPtr(0), "Class Name", null);

This is Correct:

IntPtr child = FindWindowEx(mainWindow, new IntPtr(0), "Class Name", null);
digitai
  • 1,870
  • 2
  • 20
  • 37