tags:

views:

59

answers:

3

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 ;) )

+3  A: 

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
}
Coincoin
That's all true and I do like this, an it's even work fine with IE. BUT! WIth Chrome, FF and, I'm afraid, Opera etc. it wont work..Or mb. I;m missing something?
Pritorian
For a native win32 app, there is no such thing as a main window. In the case of firefox, it could open more than one "main window" when it restores the previous session. The best thing you can do is enumerate the top level windows belonging to that application.
Anders
A: 

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).

Pritorian
MB someone know's console argument for FF, Chrome and Opera, that start's browser as new window, not tab? For example, as -nomerge for IE?
Pritorian
And where can I find actual information about default browser in registry? because, if I change default browser between FF or Chrome - the informetion is correct. For Opera, IE, Safary - it's refers to FF or Chrome (which one was the last).
Pritorian
A: 

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);

            }

    }
Pritorian
As I can understand, the problem is in specific process model of FF and Chrome...
Pritorian