Get main window of a process, if not visible .Net/powershell

1.7k Views Asked by At

I'm testing the following command in powershell console:

get-process thunderbird |% { $_.MainWindowHandle }

It returns a value, when the window is visible, but as I hide it (actually, remove WS_VISIBLE from styles using an addon), it returns 0.

As far as I understand, it's just .Net System.Diagnostics.Process.MainWindowHandle behavior. So, the question is: what's the simplest way in .Net/powershell to get the main window handle of a process, if it's not visible?

1

There are 1 best solutions below

2
On

Would this help? It requires the use of P/Invoke.NET. Going with interop/native code may be the only way to do it, if the Window is hidden.

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

http://social.msdn.microsoft.com/Forums/vstudio/en-US/1d7bd916-9bbe-4c76-b9a0-8306159035a1/faq-item-how-to-retrieve-a-window-handle-in-visual-cnet?forum=csharpgeneral