Variant in C# ?

I’m coding in C# and I am working with a COM object that I earlier in C++ made this call:

COleVariant varBeforeEnd(DISP_E_MEMBERNOTFOUND, VT_ERROR);

hresult = pTemp->Add(knownObject, &varBeforeEnd );

How do I make the call in C#

The IDE in VisualStudio say that the Add(…) function should have parameters like this:

Add(KownObject, ref object);

Any tip what to do with the second parameter I’ve tried to block some primitive types like int and bool to an object, but I only get exceptions…

All tips are welcomed!




Answer this question

Variant in C# ?

  • ROBSR

    Anders,
    "ref object" should be the correct translation.

    To generate an error object (i.e. a variant containing an error), you can use the ErrorWrapper class that is found in System.Runtime.InteropServices. You will either need the exception that maps to DISP_E_MEMBERNOTFOUND, or the value of the DISP_E_MEMBERNOTFOUND (should be 0x80020003).

    I could not test it, but I expect you should be able to use your method as:

    ErrorWrapper varBeforeEnd = new ErrorWrapper (0x80020003);
    Add (KnownObject, ref varBeforeEnd);

    HTH
    --mc

     


  • Jorge Coelho

    Have you tried the IntPtr type it has been designed specifically for interop.

  • Variant in C# ?