COM 中 CComPtr 相对于 CComQIPtr 有何用途?
任何人都可以解释一下,COM 中 CComPtr 相对于 CComQIPtr 有什么用处?
CComPtr<ISampleInterface> Sample1;
CComQIPtr<ISampleInterface> Sample2;
Can any one explain, What is the use of CComPtr over CComQIPtr in COM?
CComPtr<ISampleInterface> Sample1;
CComQIPtr<ISampleInterface> Sample2;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
CComQIPtr
适用于当您想要以方便的方式调用QueryInterface()
来了解是否支持接口的情况:通过这种方式,您可以从指向任何对象的指针请求接口。 (不相关)COM 接口并检查该请求是否成功。
CComPtr
用于管理肯定支持某些接口的对象。您可以将其用作具有引用计数的普通智能指针。它类似于 CComQIPtr,但不允许上述用例,这为您提供了更好的类型安全性。此代码:
如果
unknown
绑定到未实现IDispatch
的对象,则编译并可能生成空指针。现在,您必须在运行时检查这一点,如果您首先想要运行时检查,那么这很好,但如果您更喜欢编译时类型检查,则不好。这段代码:
根本无法编译 - 它会产生
这提供了更好的编译时类型安全性。
CComQIPtr
is for cases when you want to callQueryInterface()
in a convenient manner to know whether an interface is supported:This way you can request an interface from a pointer to any (unrelated) COM interface and check whether that request succeeded.
CComPtr
is used for managing objects that surely support some interface. You use it as a usual smart pointer with reference counting. It is likeCComQIPtr
, but doesn't allow the usecase described above and this gives you better type safety.This code:
compiles and maybe yields a null pointer if
unknown
is bound to an object that doesn't implementIDispatch
. You now have to check for that in runtime which is good if you wanted a runtime check in the first place but bad if you'd prefer a compile time type check.This code:
will simply not compile - it yields
which provides for better compile time type safety.
前者通过默认模板参数自动推导出给定类型的 UUID。
Former deduces the UUID of given type automatically, via default template argument.
以下 MSDN 文章解释了其中的差异,并建议使用 CComPtr 而不是 CComQIPtr
如何:创建和使用 CComPtr 和 CComQIPtr 实例
The following MSDN article explains the difference and recommends using CComPtr instead of CComQIPtr
How to: Create and Use CComPtr and CComQIPtr Instances
评论sharptooth的答案。只是试图编译某事。喜欢
却失败了。相反,作业有效:
不幸的是我没有时间进一步分析这个......
Remark on the answer of sharptooth. Just tried to compile sth. like
and failed. Assignment instead worked:
Unfortunately I have no time to analyse this further...
“ATL 使用 CComQIPtr 和 CComPtr 来管理 COM 接口指针。这两个类都通过调用 AddRef 和 Release 来执行自动引用计数。重载运算符处理指针操作。CComQIPtr 另外还支持通过 QueryInterface 自动查询接口。”
当您不想“手动”调用“QueryInterface()”时,请使用“CComQIPtr”:
如果传递从 T 派生的指针类型,构造函数会将 p 设置为 T* 参数并调用 AddRef。如果传递的指针类型不是从 T 派生的,则构造函数会调用 QueryInterface 将 p 设置为与 piid 对应的接口指针。
"ATL uses CComQIPtr and CComPtr to manage COM interface pointers. Both classes perform automatic reference counting through calls to AddRef and Release. Overloaded operators handle pointer operations. CComQIPtr additionally supports automatic querying of interfaces though QueryInterface."
When you do not want to call 'QueryInterface()' 'manually', use 'CComQIPtr':
If you pass a pointer type derived from T, the constructor sets p to the T* parameter and calls AddRef. If you pass a pointer type not derived from T, the constructor calls QueryInterface to set p to an interface pointer corresponding to piid.