CComVariant 与 _variant_t、CComBSTR 与 _bstr_t [英] CComVariant vs. _variant_t, CComBSTR vs. _bstr_t

查看:28
本文介绍了CComVariant 与 _variant_t、CComBSTR 与 _bstr_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ATL(VS2008,所以 ATL9 IIRC)来创建 COM 对象,并且一直使用 CComVariant 类(在 atlcomcli.h 中定义)来管理 VARIANT 类型.但是,还有另一个 VARIANT 包装器称为 _variant_t.CComVariant_variant_t 之间有什么区别,我应该使用哪一个?

I am using ATL (VS2008, so ATL9 IIRC) to create COM objects and have been using the CComVariant class (defined in atlcomcli.h) to manage VARIANT types. However, there is also another VARIANT wrapper called _variant_t. Is there any difference between CComVariant and _variant_t and which one should I be using?

同样,有两个 BSTR 包装器可用 - CComBSTR_bstr_t.同样,我应该更喜欢哪个,为什么?

Similarly, there are two BSTR wrappers available - CComBSTR and _bstr_t. Again, which should I prefer and why?

推荐答案

_variant_t 和 _bstr_t 由编译器作为 COM 支持类 并在您使用 #import 等构造时使用.如果您愿意,可以使用它们.

_variant_t and _bstr_t are provided by the compiler as COM support classes and get used when you use constructs like #import . You can use them if you like.

CComVariant 和 CComBSTR 由 ATL 库 提供一>.

CComVariant and CComBSTR are provided by the ATL libraries.

是使用 COM 支持类还是 ATL 类取决于您.如果您经常需要执行诸如附加到原始"BSTR 或 VARIANT 之类的操作,则 COM 支持类可能是一个更安全的选择.

Whether you use the COM Support classes or the ATL classes is up to you. If you often need to do operations like attaching to 'raw' BSTRs or VARIANTs, the COM Support classes may be a safer bet.

存在一些行为差异(查看文档),其中最重要的似乎是 COM 支持类会抛出 _com_error&出现故障时的异常.如果您不想进行异常处理,请使用 ATL 类.

There are some behavioural differences (check the docs), the most important of which seems to be that the COM Support classes will throw a _com_error& exception when something fails. If you don't want to do exception-handling, go with the ATL classes.

这篇关于CComVariant 与 _variant_t、CComBSTR 与 _bstr_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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