views:

511

answers:

3

I am trying to get the handle of a window from a process. Now, that process shows a splash screen, so, if i try ProcessInstance.MainWindowHandle all I get is the handle of that processes main window, that is the splash screen.

How can I find the window I want? Maybe if I could get a list of windows that process has I could pick the one I want.

Heres the code I am trying :

AutomationElement aeProcessInstance =
  AutomationElement.FromHandle(mProcess.MainWindowHandle);

AutomationElement aeButton = aeProcessInstance.FindFirst(
  TreeScope.Descendants,
  new PropertyCondition(AutomationElement.NameProperty, "Start"));

aeButton.SetFocus();
Thread.Sleep(1000);
SendKeys.SendWait("{ENTER}");
+1  A: 

This is a challenging task. An application can have any number of top level windows. These can come and go as the application runs.

I see you are using SendKeys - are you writing an automation or test system? If so, you may want to look at the accepted answer to this question.

Could you better explain what you are trying to do? For example, are you working with random applications? Or is the target process something you have control over?

Update

Ok, your extra information means this problem is more tractable. You need to use Spy++, a debugger, or Xperf, Process Explorer, or some other tool to undertand the windowing and threading behavior.

Once you know that, you can then use various Window management functions to find the window you need and deal with it.

Also, be aware of the Windows Integrity Mechanism. The application you use to find windows in another process and send them messages must be at a higher Integrity Level (IL) that the driven application.

Foredecker
I am not working with random applications, its a specific application, and the control logic is "hardcoded" for that specific application. But, it is not my application so I dont have control over it (by control i mean source code wise).I am writing an automation, basically what it does is, check if process is running if its running click on the button Start in it... if it isnt running or isnt responding restart it. Its for a webserver like application that sometimes tends to crash, and I need to find a way (read: hack) to force it to actually work xD
SilentWarrior
Actually, I want to use C# because I plan on adding more features, non the less, I do not want to learn AutoIt or something else just because I cant find a way to enumerate the windows of a process :/
SilentWarrior
A: 

If you know ID of the UI thread in the process, you can find out which windows that thread owns by pinvoking EnumThreadWindows

The EnumThreadWindows function enumerates all nonchild windows associated with a thread by passing the handle to each window, in turn, to an application-defined callback function. EnumThreadWindows continues until the last window is enumerated or the callback function returns FALSE. To enumerate child windows of a particular window, use the EnumChildWindows function.

If the application doesn't have any dialog boxes showing, this should give you it's top level window. If it has dialog boxes, you will have to use properties of the windows to figure out which is the app window. Window styles usually work for this.

John Knoeller