I am working on a project where I need to convert C++ code to C#.
I came across _bstr_t in the code I would like to know how the equivalent in C#.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
_bstr_tclass is a wrapper for theBSTRtype. This is a string in OLE Automation that is standardized across languages.That said, the equivalent type in .NET is a
System.String.The key is when interacting with it (or defining your interfaces for COM interop in .NET code), you’ll want to use the
MarshalAsAttributewith a value from theUnmanagedTypeenumeration ofUnmanagedType.BStr, like so:Note that if your class is actually exposing the
_bstr_tin a COM interface, then you should change it to expose aBSTR;_bstr_tis a helper class that isn’t meant to be exposed across interface boundaries. TheBSTRis for that and the methods on_bstr_tare for handling the allocation and use ofBSTRinstances.