views:

1099

answers:

3

Is there a way to get the window handle (IntPtr) for a window after its launched from a C# app with Process.Start()?

+6  A: 

If it's the main window you're after, Process.MainWindowHandle will give you what you need.

Noldorin
+3  A: 

Use

process.MainWindowHandle;

It probably is 0 when launching the app, so you might want to loop and sleep until it is filled up.

Jan Jongboom
Hi Jan,For me, the handle is always zero and never goes to any other value, even after infinite looping. I use the following code:proc.StartInfo.FileName = @"C:\Documents and Settings\SUM\Desktop\PortableApps\Notepad++Portable\Notepad++Portable.exe"; proc.StartInfo.Arguments = String.Empty; proc.Start(); IntPtr appHwnd = proc.MainWindowHandle; while (appHwnd == IntPtr.Zero) { Thread.Sleep(100); appHwnd = proc.MainWindowHandle; }What's wrong?
Marcel
Marcel
+1  A: 

You could also call Refresh() on the process to be sure the info in accurate

Tristan
Good info, I'll keep that in mind
James Cadd