.NET2.0 C# Interop: How to call COM code from C#?
In my last development environment, i was able to easily interact with COM, and call methods on COM objects. Here is the original code, transacted 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][1], the relavent poritions 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 .NET2.0 method to invoke two methods on an object that, on some level, only supports late-bound IDispatch.

Are quick search of StackOverflow for using IDispatch from C# turns up [this post][2] saying what i want is not possible in .NET.

So is it possible to use COM from C# .NET2.0?


  [1]: http://pinvoke.net/default.aspx/Interfaces/IWebBrowser2.html
  [2]: http://stackoverflow.com/questions/403218/does-c-net-support-idispatch-late-binding
.NET2.0 C# Interop: How to call COM code from C#?
===

In my last development environment, i was able to easily interact with COM, and call methods on COM objects. Here is the original code, transacted 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][1], the relavent poritions 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 .NET2.0 method to invoke two methods on an object that, on some level, only supports late-bound IDispatch.

Are quick search of StackOverflow for using IDispatch from C# turns up [this post][2] saying what i want is not possible in .NET.

So is it possible to use COM from C# .NET2.0?


  [1]: http://pinvoke.net/default.aspx/Interfaces/IWebBrowser2.html
  [2]: http://stackoverflow.com/questions/403218/does-c-net-support-idispatch-late-binding