1

I have two projects (TestVisual and RecordPlayBack). TestVisual has two windows (MainWindow and TestWindow). I can get the TestWindow instance by using below code,

var windows = System.Windows.Application.Current.Windows;
IntPtr twHandle = new System.Windows.Interop.WindowInteropHelper(windows[2]).Handle;

Now, i run the RecordPlayBack.exe from TestVisual.wpf application. So, the Application.Current holds RecordPlayBack application where the TestWindow is not available. In this case, how to get the TestWindow instance of TestVisual application?

i have tried this below code,

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

IntPtr twHandle =(IntPtr) User32.FindWindow("test", null);

Please suggest me any ideas.

Note: RecordPlayBack project is added as referrence to TestVisual project.

Thanks,

Prithiv
  • 504
  • 5
  • 20
  • This has possibly been asked before, take a look for example https://stackoverflow.com/questions/1953582/how-to-i-get-the-window-handle-by-giving-the-process-name-that-is-running or https://stackoverflow.com/questions/13547639/return-window-handle-by-its-name-title. Hope these answers help! – GrahamMc Dec 06 '17 at 10:54
  • Hi @GrahamMc, Thanks for your suggestions. I have added the screenshot of my application, please look that. I have tried the below suggestion, [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); but it capture the "RecordWindow" because i'm trying to capture the "TestWindow" while clicking stop button in RecordWindow. So in this case, RecordWindow comes front. Is there any other suggestion? – Prithiv Dec 06 '17 at 12:12
  • maybe take a look here https://stackoverflow.com/questions/2281429/how-to-enumerate-all-windows-within-a-process for how to get a list of all windows in a process. Once you get the process using your answer, you can then list all the windows and find the correct window? – GrahamMc Dec 07 '17 at 09:52

1 Answers1

1

I can get the window handle in another application by using below code,

System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Process p in processes)
{
    if(p.MainWindowTitle == "Test Window")
    {
        twHandle = p.MainWindowHandle;
        break;
    }
}
Prithiv
  • 504
  • 5
  • 20