Search code examples
c#cominterop.net-2.0idispatch

.NET2.0 C# Interop: How to call COM code from C#?


In my last development environment, I was able to easily interact with COM, calling methods on COM objects. Here is the original code, translated into C# style code (to mask the original language):

public static void SpawnIEWithSource(String szSourceHTML)
{
    OleVariant ie; //IWebBrowser2
    OleVariant ie = new InternetExplorer();
    ie.Navigate2("about:blank");

    OleVariant webDocument = ie.Document;
    webDocument.Write(szSourceHTML);
    webDocument.close;

    ie.Visible = True;
}

Now begins the tedious, painful, process of trying to interop with COM from managed code.

PInvoke.net already contains the IWebBrower2 translation, the relavent porition of which is:

[ComImport, 
   DefaultMember("Name"), 
   Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E"), 
   InterfaceType(ComInterfaceType.InterfaceIsIDispatch), 
   SuppressUnmanagedCodeSecurity]
public interface IWebBrowser2
{
    [DispId(500)]
    void Navigate2([In] ref object URL, [In] ref object Flags, [In] ref object TargetFrameName, [In] ref object PostData, [In] ref object Headers);

    object Document { [return: MarshalAs(UnmanagedType.IDispatch)] [DispId(0xcb)] get; }
}

I've created the COM class:

[ComImport]
[Guid("0002DF01-0000-0000-C000-000000000046")]
public class InternetExplorer
{
}

So now it's time for my actual C# transaction:

public static void SpawnIEWithSource(String szHtml)
{
    PInvoke.ShellDocView.IWebBrowser2 ie;
    ie = (PInvoke.ShellDocView.IWebBrowser2)new PInvoke.ShellDocView.InternetExplorer();

    //Navigate to about:blank to initialize the browser
    object o = System.Reflection.Missing.Value;
    String url = @"about:blank";
    ie.Navigate2(ref url, ref o, ref o, ref o, ref o);

    //stuff contents into the document
    object webDocument = ie.Document;
    //webDocument.Write(szHtml);
    //webDocument.Close();

    ie.Visible = true;
}

The careful readers notice that IWebBrowser2.Document is a late-bound IDispatch. We're using Visual Studio 2005, with .NET 2.0 on our, and our customer's, machines.

So what's the .NET 2.0 method to invoke methods on an object that, on some level, only supports late-bound IDispatch?

A quick search of Stack Overflow for using IDispatch from C# turns up this post saying what I want is not possible in .NET.

So is it possible to use COM from C# .NET 2.0?


The question is that there is an accepted design pattern that I want to use in C#/.NET. It involves launching Internet Explorer out of process, and giving it HTML content, all the while not using temporary files.

A rejected design idea is hosting Internet Explorer on a WinForm.

An acceptable alternative is launching the system registered web browser, giving it HTML to display, without using a temporary file.

The stumbling block is continuing to use COM objects in the .NET world. The specific problem involves performing late-binding calls to IDispatch without needing C# 4.0. (i.e. while using .NET 2.0)


Solution

  • Late bound IDispatch called is relativly easy in .NET, although piss-poor:

    public static void SpawnIEWithSource(String szHtml)
    {
        // Get the class type and instantiate Internet Explorer.
        Type ieType = Type.GetTypeFromProgID("InternetExplorer.Application");
        object ie = Activator.CreateInstance(ieType);
    
        //Navigate to the blank page in order to make sure the Document exists
        //ie.Navigate2("about:blank");
        Object[] parameters = new Object[1];
        parameters[0] = @"about:blank";
        ie.GetType().InvokeMember("Navigate2", BindingFlags.InvokeMethod | BindingFlags.IgnoreCase, null, ie, parameters);
    
        //Get the Document object now that it exists
        //Object document = ie.Document;
        object document = ie.GetType().InvokeMember("Document", BindingFlags.GetProperty | BindingFlags.IgnoreCase, null, ie, null);
    
        //document.Write(szSourceHTML);
        parameters = new Object[1];
        parameters[0] = szHtml;
        document.GetType().InvokeMember("Write", BindingFlags.InvokeMethod | BindingFlags.IgnoreCase, null, document, parameters);
    
        //document.Close()
        document.GetType().InvokeMember("Close", BindingFlags.InvokeMethod | BindingFlags.IgnoreCase, null, document, null);
    
        //ie.Visible = true;
        parameters = new Object[1];
        parameters[0] = true;
        ie.GetType().InvokeMember("Visible", BindingFlags.SetProperty | BindingFlags.IgnoreCase, null, ie, parameters);
    }
    

    The referenced SO question that originally said "not possible until C# 4.0" was amended to show how it is possible in .NET 2.0.

    Does C# .NET support IDispatch late binding?