Search code examples
c#comsolidworks

Why is BStrWrapper only required for C# application?


In the SOLIDWORKS API documentation, it is explicitly stated that in order to marshal an array of .NET strings back SOLIDWORKS, you must use the BStrWrapper class. It also states this for C# applications without the mention of VB.NET? Is there a reason for that? Thank you.

Source

enter image description here


Solution

  • I'm not very familiar with VB.net but I think this is because of different casting rules in C# and VB.NET. Underneath, the method you referenced looks (in C++):

    virtual HRESULT __stdcall raw_RemoveExternalDocuments (
            /*[in]*/ VARIANT DocumentNames,
            /*[out,retval]*/ VARIANT_BOOL * Retval ) = 0;
    

    Or better looking:

    VARIANT_BOOL RemoveExternalDocuments (const _variant_t & DocumentNames );
    

    C# requires using BStrWrapper because it marshals string as VARIANT, see MS doc. And it looks VB.NET can do it implicit.