2

I have been trying to get the Reference to the Main window of my Process for quite sometime. I went through many sites looking for a solution. But it looks like everyone provides a solution to get the Window handle and not the window itself.

Getting a Process's Mainwindow handle is quite straight forward. I seem to get the Window title also as expected.

I went through most of the APIs provided by User32.dll but couldn't find what i was looking for.

I need a reference to the window because i want to access few members of the window class to accomplish my work. The window i am referring to is a WPF window.

Any help in this regard will be highly appreciated:)

Thanks in advance.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Anee
  • 463
  • 9
  • 26
  • 3
    Are you trying to get the MainWindow from WITHIN the same application, or from another application? – dognose Aug 26 '13 at 10:59
  • You get the window from the handle: http://stackoverflow.com/questions/5822026/get-wpf-window-by-hwnd – CKII Aug 26 '13 at 11:39
  • 2
    You can't get a reference to an instance of a window class that resides in a different process. You can only get at the window's data that is exposed through the Windows API. Anything more requires IPC such as via Windows Communication Foundation. – Matthew Watson Aug 26 '13 at 11:48
  • 1
    @MatthewWatson. Not only that. You also can't get a reference to an instance of a window class that resides in the same process, but in another AppDomain. – sloth Aug 26 '13 at 11:56
  • @All thanks for your responses. As i told, I have gone through several links but never found any solution anywhere. Let me tell you what exactly i am trying to achieve. I have a .exe file which is working in one environment and not working in another. Tried fixing it but with no luck. So i thought of this way since getting it working was not my top priority. I just wanted to check whether getting access to the class inside the process would help me achieve what i wanted since its working in the former environment. – Anee Aug 27 '13 at 03:48

1 Answers1

2

You can use the HwndSource.FromHwnd method.

Here's a simple LinqPad-ready example:

void Main()
{
    var mw = new MainW();
    mw.Show();

    var hWnd = FindWindowByCaption(IntPtr.Zero, "testwindow");
    var rootVisual = System.Windows.Interop.HwndSource.FromHwnd(hWnd).RootVisual;
    MainW m2 = (MainW)rootVisual;
    Thread.Sleep(500);
    m2.Title="is going";
    Thread.Sleep(500);
    m2.Title="to close...";
    Thread.Sleep(500);
    m2.Close();
}

[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

class MainW: System.Windows.Window
{
    public MainW()
    {
      Title = "testwindow";
      Width = 230;
      Height = 100;
      Background = System.Windows.Media.Brushes.AliceBlue;
    }
}
sloth
  • 99,095
  • 21
  • 171
  • 219
  • I have tried this option as well but the HWndSource.FromHwnd() returns null. I am running the .exe file using Process.Start() and i get the Window handle using the process.MainWindowHandle. After this if i try doing what you suggested i get null from that call. Am i missing something? – Anee Aug 27 '13 at 03:54
  • 3
    @Anee You can't get a reference to an instance of a window class that resides in another process or another AppDomain. – sloth Aug 27 '13 at 07:05
  • I had realized it after looking around for sometime. But still i wanted to hear it from someone that its not possible at all because i wasn't sure if i had missed something. Anyways, thanks for all your replies. – Anee Aug 27 '13 at 08:23