使用哪些 COM 智能指针类?

2022-01-14 00:00:00 com c++ c++builder

我对 C++ 编程的 COM 智能指针类的选择感到困惑:

I'm confused by the choices for COM smart pointers classes for C++ programming:

我知道有三个四个:

  • CCOMPtr 来自 ATL
  • _com_ptr_t 来自 MS Com 支持类一个>
  • TComInterface(因为我使用的是 C++Builder 2009)
  • CCOMQIPtr,(我之前忘记了)
  • CCOMPtr from ATL
  • _com_ptr_t from the MS Com Support Classes
  • TComInterface (because I'm using C++Builder 2009)
  • CCOMQIPtr, (which I'd previously forgotten)

我已经阅读了前两者的错误与异常处理差异,但 TComInterface 似乎完全没有记录.据我所知,前两个似乎都有问题或意外"行为.

I've read about the error vs. exception handling differences of the first two, but TComInterface seems totally undocumented. Both the first two seem to have gotchas or 'unexpected' behaviour, from what I can find.

理想情况下,我想要一些干净且现代的 C++,但据我所知,boost::com 并不存在...

Ideally, I'd like something that's clean and modern C++, but boost::com doesn't exist as far as I know...

我需要控制来自其他供应商的应用程序.它们通过 TLB 文件提供 COM 接口.

I need to control an application from another vendor. They provide a COM interface via a TLB file.

推荐答案

如果您使用类型库导入的东西,它将根据 _com_ptr_t 和相关的 COM 支持类生成代码.如果您只是使用这些库中的内容,请继续使用它们.

If you are using the type-library import stuff, it will generate code based upon _com_ptr_t and the related COM Support Classes. Go ahead and use those if you are just using stuff from those libraries.

如果您正在编写基于 ATL 的代码,请使用 CCOMPtr 和其他基于 ATL 的类.

If you are writing ATL-based code, then use CCOMPtr and the other ATL-based classes.

如果您需要混合搭配这两种类型,事情可能会变得很糟糕,但您可能应该对这两种类型都感到满意,并使用对您当时所做的任何事情最有意义的那个.

Things may get ugly if you need to mix-and-match both types, but you should probably just get comfortable with both and use whichever makes the most sense for whatever you are doing at the time.

好消息是您拥有所有这些东西的源代码,因此您不必只依赖文档(自从 .NET 出现以来,文档并没有得到太多关注).

The nice thing is that you have the source for all of these things, so you don't have to rely only on the documentation (which has not been getting much care since .NET appeared).

相关文章