none
COM Interop - Error marshaling wchar_t** from unmanaged C++ to .NET RRS feed

  • Question

  • Hello,
           I get the following error when calling a COM (VC++ 6.0) function from C# using COM Interop.

     

    ---------------------------------------------------------------------------------------------------------------------------------------------------------

    An invalid VARIANT was detected during a conversion from an unmanaged VARIANT to a managed object.

    ---------------------------------------------------------------------------------------------------------------------------------------------------------

     


    The body of the VC++ function looks like this:

    short Func1(BSTR FAR* pwUnicodeResults)
    {
       //The signature of the function below is: int Process(wchar_t **)
       //Process writes Unicode data to the pwUnicodeResults parameter
       obj1->Process(pwUnicodeResults);
       return 0;
    }

    Unicode data is being written to pwUnicodeResults correctly. However it seems
    that the RCW is unable to marshal the data(wchar_t **) to the managed environment.
    I used tlbimp to create the RCW. This COM function is called from the following C# code.

    void Func2()
    {
       String labelResults = "";

       COMObj obj = new COMObj();
       obj.Func1(ref labelResults);

    }


    I haven't worked with COM interop before. I would really appreciate it if anybody has any suggestions regarding potential solutions to this issue.

     

    Thanks,

    Navin

    Friday, April 18, 2008 3:47 AM

Answers

  • The C++ code doesn't look valid if Process() takes a wchar_t** argument.  Does it actually call SysAllocString() to allocate a BSTR?  The error you're getting says it doesn't.  This might have worked before without errors because clients just simply ignored the HRESULT return value from SysFreeString().  .NET surely doesn't.
    Friday, April 18, 2008 6:14 PM
    Moderator