开发者

How do I find the window handle for a running process?

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.M开发者_Python百科ainWindowHandle 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}");


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜