且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中等效于_bstr_t

更新时间:2023-01-29 11:17:29

_bstr_t BSTR 类型。这是 OLE自动化中的字符串,该字符串已跨语言标准化。

The _bstr_t class is a wrapper for the BSTR type. This is a string in OLE Automation that is standardized across languages.

也就是说,.NET中的等效类型是 System.String

That said, the equivalent type in .NET is a System.String.

关键是与之交互(或定义接口用于.NET代码中的COM互操作),则需要使用 MarshalAsAttribute ,其值来自 UnmanagedType.BStr UnmanagedType 枚举,例如所以:

The key is when interacting with it (or defining your interfaces for COM interop in .NET code), you'll want to use the MarshalAsAttribute with a value from the UnmanagedType enumeration of UnmanagedType.BStr, like so:

// This is on an interface that is in unmanaged code.
public void DoSomething([MarshalAs(UnmanagedType.BStr] string myString);

如果您的课程实际上是在COM接口中公​​开 _bstr_t ,则应更改它以公开 BSTR ; _bstr_t 是一个帮助程序类,不能跨越接口边界公开。 BSTR 用于该类和方法在 _bstr_t 上用于处理 BSTR 实例的分配和使用。

Note that if your class is actually exposing the _bstr_t in a COM interface, then you should change it to expose a BSTR; _bstr_t is a helper class that isn't meant to be exposed across interface boundaries. The BSTR is for that and the methods on _bstr_t are for handling the allocation and use of BSTR instances.