Primary interop assembly for MSHTML increases size of redistributable file (305622)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition

This article was previously published under Q305622

SYMPTOMS

When you use the Microsoft Internet Explorer MSHTML.dll file in a Visual Studio .NET application, you may notice that the Primary Interop Assembly (PIA) for MSHTML is greater than 7.5 megabytes (MB). This is three times greater than the size of MSHTML itself. The size of this assembly is particularly problematic if you want to distribute your application over the Web.

CAUSE

If you use the Ildasm.exe command from the Visual Studio .NET command prompt to open the Interop assembly, you can see that the process to generate the Interop DLL creates objects and interfaces for all of the corresponding items in the MSHTML.dll file. Additionally, the process to generate the Interop DLL creates a delegate for every event on every object in the Document Object Model (DOM). This necessitates a large assembly.

RESOLUTION

The best alternative is to call only MSHTML through late binding, which does not require the distribution of an MSHTML Interop assembly.

To do this, use the classes in the System.Runtime.InteropServices namespace to call the IDispatch interface against the Internet Explorer DOM. The following code demonstrates how to do this:

Microsoft Visual Basic .NET code
Imports System.Runtime.InteropServices
Imports System.Reflection

    Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, _
    ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) _
    Handles AxWebBrowser1.DocumentComplete
        Dim htm As Object
        Dim typ As Type
        Dim docTitle As String

        typ = Type.GetTypeFromCLSID(New System.Guid("{3050F4E7-98B5-11CF-BB82-00AA00BDCE0B}"))
        htm = AxWebBrowser1.Document
        docTitle = typ.InvokeMember("title", BindingFlags.GetProperty, Nothing, htm, Nothing)
        MessageBox.Show("Title is " & docTitle)
    End Sub
				
Microsoft Visual C# .NET code
using System.Runtime.InteropServices;
using System.Reflection;
		
private void DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
	{
	object htm;
         Type typ;
         string docTitle;

         typ = Type.GetTypeFromCLSID(new System.Guid("{3050F4E7-98B5-11CF-BB82-00AA00BDCE0B}"));
         htm = axWebBrowser1.Document;
         docTitle = typ.InvokeMember("title", BindingFlags.GetProperty, null, htm, null).ToString();
         MessageBox.Show("Title is " + docTitle);
	}
				

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce the problem

  1. In your Visual Studio .NET project, click Project, and then click Add Reference.
  2. On the COM tab, click Microsoft HTML Object Library to add the PIA.

REFERENCES

For more information, visit the following Microsoft Web sites: For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

311284 How to handle document events in a Visual Basic .NET application

312777 How to handle document events in a Visual C# .NET application


Modification Type:MinorLast Reviewed:7/14/2006
Keywords:kbCOMInterop kbprb KB305622