How do I call a COM-function with a _variant_t parameter (type "long")?

1

I want to port a certain function call to C#. The two lines are as follows:

    m_pBrowserApp->get_Document(&pVoid);
    m_pLayoutAnalyzer->Analyze4(pVoid, _variant_t(5L));

m_pBrowserApp is the ActiveX browser object and pVoid is its document property. I can get that by calling WebBrowserBase.ActiveXInstance.Document. However, I have no idea how to create a _variant_t(5L) in C#. Since the call is not a VT_BYREF, it "should just work" by calling it like this:

    ILayoutAnalyzer2 vips = new LayoutAnalyzer2();
    vips.Initialize(0);
    SHDocVw.WebBrowser_V1 axBrowser = (SHDocVw.WebBrowser_V1)this.webBrowser1.ActiveXInstance;
    var doc = axBrowser.Document as mshtml.HTMLDocument;

    vips.Analyze4(doc, (Object)5L); // fails with HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH)

But it doesn't. It fails with a DISP_E_TYPEMISMATCH error. I'm pretty sure the Document property is valid. So the question remains: How to I properly pass a long wrapped in a variant via interop?

c#
com
interop
com-interop
asked on Stack Overflow Apr 2, 2013 by Jonny • edited Apr 2, 2013 by bash.d

1 Answer

2

Variants go back to the mid 1990s, a time when longs were consider long for having 32 bits. This is just a few years after the first 32-bit operating systems became available, an integer was still 16 bits in VB6 for example. Not so in C# and .NET in general, a 32-bit programming environment by design that never had to deal with 16-bit back-compat. So use a C# int, not a long.

Drop the L from the literal.

answered on Stack Overflow Apr 2, 2013 by Hans Passant

User contributions licensed under CC BY-SA 3.0