Code level support
Often it’s useful to have a web browser control to embed in your application. The Microsoft .NET 1.x platform does not have a built-in browser control but Windows does. This fact can be leveraged to get one that .NET can use.
Internet Explorer’s functionality is contained in the WebBrowser class in the Shell Document Object and Control Library DLL (SHDocVw.dll). It’s exposed as an ActiveX control.
.NET Windows Forms cannot directly host ActiveX controls. Windows Forms need controls to be derived from System.Windows.Forms.Control. Fortunately, Microsoft provides the AxImp.exe, a utility to create an assembly for an ActiveX control. To use it fire up a command shell that has the Visual Studio environment and run the following:
aximp “SystemRoot\system32\SHDocVw.dll”
This will generate two assemblies: SHDocVw.dll and AxSHDocVw.dll. The CLR proxies for the COM types are in SHDocVw.dll and the Windows Forms proxies for the ActiveX controls are in AxSHDocVw.dll These proxies are wrapped to appear like any .NET control. Their classes are created in a namespace that’s the same name as the assembly: AxSHDocVw. These proxy assembles are called runtime callable wrappers or RCW for short.
Using the WebBrowser is quite simple. Fire up Visual Studio and create a new C# project with Windows Forms support. Add a reference to the assemblies AxSHDocVw.dll and SHDocVw.dll. Then enter the following code:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SimpleBrowser
{
public class SimpleBrowser
{
[STAThread]
public static void Main()
{
// Create the main window form
Form simpleBrowserForm = new Form();
simpleBrowserForm.Text = “Simple Browser”;
simpleBrowserForm.Size = new Size( 600, 500 );
// Create the browser control
AxSHDocVw.AxWebBrowser browser = new AxSHDocVw.AxWebBrowser();
browser.Dock = DockStyle.Fill;
simpleBrowserForm.Controls.Add( browser );
// Show the window and browse to a place where
// you can get to everywhere.
simpleBrowserForm.Show();
browser.Navigate( “http://www.google.com” );
// Main loop
Application.Run( simpleBrowserForm );
}
}
}
This code does not use the Visual Studio UI designer to keep it simple for tutorial purposes. If you build and run it you should see something like the image to the left.
The critical piece of code is AxSHDocVw.AxWebBrowser browser = new AxSHDocVw.AxWebBrowser(); which creates the WebBrowser control. After that you can treat it like any other .NET control.
If you want this functionality but don’t want to use Internet Exploiter^H^H^H^Hrer in your application there’s another solution for you. With a little more work you can use the Mozilla ActiveX Control. This is a project that implements the same COM interfaces as IE but uses Mozilla instead.
The steps are same as for IE but with the following differences:
- Install the Mozilla ActiveX Control
- Run AxImp on mozctl.dll (located in the directory where you installed the Mozilla ActiveX Control)
- Add assembly references to MOZILLACONTROLLib.dll and AxMOZILLACONTROLLib.dll in the project.
- Change
AxSHDocVw.AxWebBrowser browser = new AxSHDocVw.AxWebBrowser()toAxMOZILLACONTROLLib.AxMozillaBrowser browser = new AxMOZILLACONTROLLib.AxMozillaBrowser(). See SimpleMozBrowser.cs for details.
The downside to this approach is that you’ll need to redistribute the Mozilla ActiveX Control package with your installer. A possible work-around is since they use the some interface you could have a function to determine if the Mozilla ActiveX Control is installed and use it otherwise fall back to the Internet Explorer control.
Download the Visual Studio 2003 solution for this snippet.




— Mark Apr 22, 06:17 AM #
Exception has been thrown by the target of an invocation.
Any Idea
— gabriel de fombelle May 7, 05:40 AM #
Gabriel –
Sorry for the slow repsonse, it looks like something is broken in my comment notification system.
I downloaded the lastest Mozilla Control (1.7.7) and the source on a “clean” machine and had no problems with it. It ran cleanly.
The first thing I recommend trying is to see if you could figure out where the exception is coming from. Turn on all exceptions Debug/Exceptions and run. Once the exception is thrown look into the stack trace and see what’s going on.
The second thing I would try is adding a catch block for System.Reflection.TargetInvocationException and look at the InnerException property and see what the underlying exception was.
The third thing I would try is deleting the references to AxMozillaControlLib and MozillaControlLib and re-adding them.
Let me know what you find.
— Bryan Mau Jun 9, 07:15 AM #
An unhandled exception of type ‘InvalidActiveXStateException’ occurred in axmozillacontrollib.dll
can someone please hint me to the right direction?
— ib Aug 25, 02:56 PM #
I get the same error as Gabriel (except I’m using VB.NET). When I trap the event and look at the InnerException property, the message is “Marshaler restriction: Excessively long string”. The test string I’m passing to it is “www.bbc.com”, which hardly seems excessive! Still, I experimented with stuffing the string into a character array as well – just in case it was something to do with a .NET string – but the same error was generated.
— Andrew Oct 2, 07:42 PM #