First you need to create a Windows Forms application using Visual Studio and on the Form, add a TextBox and a Button control. Also, double click on the button control to wirte its click event handler.
You also need to import two namespacces by adding "using SHDocVw;" and "using System.Runtime.InteropServices;" to the top of your code.
You will probably need to add the Microsoft Internet Controls reference as well.
WebBrowserApp webBrowser = null;
InternetExplorer explorer = new InternetExplorerClass();
ShellWindows shWin = new ShellWindows();
public Form1()
{
InitializeComponent();
webBrowser = (IWebBrowserApp)explorer;
}
private void button1_Click(object sender, EventArgs e)
{
//open browser to url typed into text box
IEOpenOnURL(textBox1.Text);
}
public void IEOpenOnURL(string url)
{
object o = null;
bool b = false;
//check if window is already open
foreach (InternetExplorer ies in shWin)
{
if (ies.HWND == webBrowser.HWND)
b = true;
}
try
{
if (b)
{
webBrowser .Visible = true;
webBrowser .Navigate(url, ref o, ref o, ref o, ref o);
}
else
{
//its not open so create new instance
explorer = new InternetExplorerClass();
webBrowser = (IWebBrowserApp)explorer;
webBrowser .Visible = true;
webBrowser .Navigate(url, ref o, ref o, ref o, ref o);
}
}
catch (Exception)
{ // implement your exception handling here
}
}
Vijay ThakarePosted Aug 29, 2012, 2:50 AM
Hi I am working this code on win 7 in this code i.e open first time, but when reopen same i get following error. The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))
Sam HobbseditedPosted May 15, 2012, 2:49 PMEdited May 15, 2012, 3:00 PM
The error RPC_E_DISCONNECTED is occuring because the browser window was closed; it has nothing to do with the version of Windows. The sample code does not check to determine if the window has been closed so it crashes. I think that can be fixed by using try/catch blocks for "if (ies.HWND == webBrowser.HWND) .... b = true;". I think this program can be simplified significantly if "explorer" were set to null and then in the button1_Click the program can simply check if "explorer" is null. Finally, if it were me, I would use the InternetExplorer.OnQuit event to detect the closing of the window and then I would set "explorer" to null so that the next time it is used it will get a new instance. See the following for an example of using InternetExplorer.OnQuit event (note that you do not need to use the AutoResetEvent): http://www.c-sharpcorner.com/Forums/Thread/153311/visit-web-site-before-it-shows-in-browser.aspx
p pPosted Apr 10, 2012, 3:58 AM
hi, When I run above code on win 7 .it is throwing below error. Above code works fine with XP and Vista. Unable to cast COM object of type 'SHDocVw.InternetExplorerClass' to interface type 'SHDocVw.IWebBrowserApp'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{0002DF05-0000-0000-C000-000000000046}' failed due to the following error: The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED)). Please help on it.