I have starter new System.Diagnostics.Process and it launchs FireFox. How can I obtain the Handler of that window? Any ideas - C#, winAPI functions... anything (just not ASM ;) )
Once you started the process, you can call process.Refresh()
and the process.MainWindowHandle
property will eventually contain the native windows handle of the application's main window.
You might have to wait a little bit after you started the process for it to get populated.
Here is some code:
Process process = new Process();
// Fill process.StartInfo
process.Start();
do
{
System.Threading.Sleep(100);
process.Refresh();
}
while(process.MainWindowHandle == IntPtr.Zero && !process.HasExited);
if(!process.HasExited)
{
IntPtr hwnd = process.MainWindowHandle;
// Do whatever you need to do with hwnd
}
For example... i do somesing like this:
IntPtr hwnd = IntPtr.Zero;
System.Diagnostics.Process browserProc = new System.Diagnostics.Process();
browserProc.StartInfo.FileName = getDefaultBrowser();
browserProc.StartInfo.Arguments = webBrowser1.Url.ToString();
browserProc.StartInfo.UseShellExecute = true;
browserProc.Start();
do{
Thread.Sleep(100);
browserProc.Refresh();
} while (browserProc.MainWindowHandle == IntPtr.Zero && !browserProc.HasExited);
if (!browserProc.HasExited)
{
hwnd = browserProc.MainWindowHandle;
browserProc.WaitForInputIdle();
MoveWindow(browserProc.MainWindowHandle, p.X, p.Y, this.Width, this.Height, true);
UpdateWindow(browserProc.MainWindowHandle);
}
And what? I'm receving an error message, if FF was already open (but, I must say, that when it isn't, all is fine).
Ok, I find commands, but it's not a solution... I find commands for launching new instance of the window and they work fine, until none of them are launched. If i have any instance of browser alreadi launched, then aplication crashed with message "process has completed, so the requested information is not available". New instance of window is launched (only by firefox), but aplication is not receaving handle and crashed...
So, any ideas? Here is the code:
private void button2_Click(object sender, EventArgs e)
{
Point p = this.Location;
this.HideBrowser();
IntPtr hwnd = IntPtr.Zero;
string arguments = string.Empty;
string browser = getDefaultBrowser(); // phisical path to default browser
if (browser.Contains("firefox"))
arguments = "-new-window " + webBrowser1.Url.ToString();
if (browser.Contains("opera"))
arguments = "-newwindow " + webBrowser1.Url.ToString();
if (browser.Contains("iexplore"))
arguments = "-nomerge " + webBrowser1.Url.ToString();
if (browser.Contains("chrome"))
arguments = "-app-launch-as-panel " + webBrowser1.Url.ToString();
System.Diagnostics.Process browserProc = new System.Diagnostics.Process();
browserProc.StartInfo.FileName = browser;
browserProc.StartInfo.Arguments = arguments;
browserProc.StartInfo.UseShellExecute = true;
browserProc.Start(); // запускаем процесс
do{
Thread.Sleep(100);
browserProc.Refresh();
} while (browserProc.MainWindowHandle == IntPtr.Zero && !browserProc.HasExited);
if (!browserProc.HasExited)//если что-то поймали
{
hwnd = browserProc.MainWindowHandle;
browserProc.WaitForInputIdle();
MoveWindow(browserProc.MainWindowHandle, p.X, p.Y, this.Width, this.Height, true);//устанавливаем новые координаты окна
UpdateWindow(browserProc.MainWindowHandle);
}
}