CComVariant vs. _variant_t,CComBSTR和_bstr_t [英] CComVariant vs. _variant_t, CComBSTR vs. _bstr_t

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

问题描述

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


类似地,有两个 BSTR 包装器 - CComBSTR _bstr_t

解决方案

_variant_t和_bstr_t由编译器提供 COM支持类,并在使用如#import的构造时使用。您可以使用它们。



CComVariant和CComBSTR由 ATL库



无论您使用COM支持类还是ATL类你决定。如果你经常需要做附加到原始BSTRs或VARIANTs的操作,COM支持类可能是一个更安全的打赌。



有一些行为差异docs),其中最重要的似乎是COM支持类将抛出一个_com_error&异常时发生故障。如果你不想做异常处理,可以使用ATL类。


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?

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

解决方案

_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 and CComBSTR are provided by the ATL libraries.

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.

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 vs. _variant_t,CComBSTR和_bstr_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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