Instance A is trying to restore instance B’s window, but I can’t get the B’s window handle. I think the problem is that the window is being minimized by B to the system tray using:
this.Visibility = Visibility.Hidden;
And A is trying to get B’s window handle using:
Process process = Process.GetCurrentProcess();
Process.GetProcessesByName(process.ProcessName).First().MainWindowHandle;
Which is equal to IntPtr.Zero.
I also tried to get the window handle by class name using Spy++ but the class name has a per-instance GUID in the following format:
HwndWrapper[FileName.exe;;ad445199-cf93-48a4-bd24-2f97d54c8af8]
That is because what you want basically doesn’t exists, and the concept of
MainWindowHandleis a gross misnomer that sneaked into the .Net Framework for everlasting confusion. From There can be more than one (or zero): Converting a process to a window:The topic is also elaborated in MSDN Q&A Get the Main Window: