在C#中等效于_bstr_t [英] What is the equivalent of _bstr_t in C#

查看:209
本文介绍了在C#中等效于_bstr_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个需要将C ++代码转换为C#的项目中。

I am working on a project where I need to convert C++ code to C#.

我遇到了 _bstr_t 在代码中,我想知道C#中的等效方式。

I came across _bstr_t in the code I would like to know how the equivalent in C#.

推荐答案

_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.

这篇关于在C#中等效于_bstr_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆