[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 946
  • Last Modified:

Open Browser Window from Windows App

I have a little application that opens a browser window from a windows application built using VS2005.

Added references for  Microsoft.mshtml  and   interop.shdocvw.

Then created a browser class:

    public class StaticBrowser
    {
        public static IWebBrowser2 TheInstance = new InternetExplorerClass()
                    as IWebBrowser2;
    }


Next used the following code in a menu click to open a browser window:

        private void staticBrowserToolStripMenuItem_Click(object sender,
                            EventArgs e)
        {
            object missing = System.Reflection.Missing.Value;
            StaticBrowser.TheInstance.Navigate("http://www.ezywrap.com",
                    ref missing, ref missing, ref missing, ref missing);
            StaticBrowser.TheInstance.Visible = true;
        }


This is all fine and the browser window opens  -  if I click the menu item again  the same browser window is used and you can see it re-display  -  the same window.

The problem is if the user closes the browser window that was open the following error happens when the user clicks the menu item again to open the browser window.

System.Runtime.InteropServices.COMException (0x80010108): The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))

Is there any way to bring up this window again -  I know I can use a non static Browser but that results in a new window with every click which is something I was trying to avoid.

thanks
0
cholden
Asked:
cholden
  • 2
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I think it has an OnQuit() Event that you can subscribe to.  When it fires, set "TheInstance" to a new instance.
0
 
choldenAuthor Commented:
Thank you for the reply  and it points me in a good direction but I am stuck on just how to implement it. I have been unable to find any info elsewhere on this.  

thanks
0
 
Éric MoreauSenior .Net ConsultantCommented:
instead of creating an instance, could you simply use :
Process.Start("http://www.ezywrap.com");
0
 
choldenAuthor Commented:
What I finally ended up doing was catching the exception and re-initializing the instance -  was trying to avoid doing it this way,  but it works:

            object missing = System.Reflection.Missing.Value;
            try
            {
                StaticBrowser.TheInstance.Navigate("http://www.ezywrap.com", ref missing, ref missing, ref missing, ref missing);
            }
            catch
            {
                StaticBrowser.TheInstance = new InternetExplorerClass() as IWebBrowser2;
                StaticBrowser.TheInstance.Navigate("http://www.ezywrap.com", ref missing, ref missing, ref missing, ref missing);
            }
            StaticBrowser.TheInstance.Visible = true;
        }

thanks
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now