COM,包含 BSTR 的 VARIANT。 谁分配?

发布于 2024-07-09 08:03:22 字数 856 浏览 8 评论 0原文

好吧,所以我真的想不出一个合适的标题来总结这一点。

IPrintPipelinePropertyBag 接口具有 AddProperty 方法,该方法非常恰当地“将属性添加到属性包”。

http://msdn.microsoft.com/en -us/library/aa506384.aspx

AddProperty( [输入,字符串] const wchar_t *pszName, [in] const 变体 *pVar );

我们使用以下代码将字符串添加到属性包中。

CComVariant varProperty = CComBSTR(someString);
pPrintPropertyBag->AddProperty(L"SOME_PROPERTY", &varFilename);

不过,很明显,创建的 CComBSTR 和 CComVariant 一段时间后就会超出范围。 我不确定 PropertyBag 是否处理该字符串并制作自己的副本。 由于我们可以在 VARIANT 中存储各种内容,因此情况不应该如此。

假设未处理该字符串,我的问题是,在 COM 中执行此操作的模式是什么? 我应该如何传递包含已分配字符串的 VARIANT,使该字符串可用于其他线程,即使调用 AddProperty 的线程首先死亡,并正确取消分配该字符串?

OK, so I couldn't really think of an apropos title that summarizes this.

The IPrintPipelinePropertyBag interface has the method AddProperty which aptly enough "adds a property to a property bag."

http://msdn.microsoft.com/en-us/library/aa506384.aspx

AddProperty( [in, string] const
wchar_t *pszName, [in] const
VARIANT *pVar );

We use the following code to add a string to the property bag.

CComVariant varProperty = CComBSTR(someString);
pPrintPropertyBag->AddProperty(L"SOME_PROPERTY", &varFilename);

It's pretty obvious, though, that the CComBSTR and CComVariant that is created goes out of scope after a while. I'm not sure if the PropertyBag handles the string and makes its own copy. Since we can store all kinds of stuff inside a VARIANT, this shouldn't be the case.

Assuming the string isn't handled, my question is, what is the pattern for doing this in COM? How should I pass a VARIANT that contains an allocated string, make that string available for other threads even if the thread that called AddProperty died first, and de-allocate the string properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2024-07-16 08:03:22

在大多数情况下,当您使用字符串或 VARIANT 调用 COM 函数时,唯一需要保证的是这些对象在整个调用过程中可用。 调用后,对象本身负责复制数据。 例如,VARIANT 最有可能使用 VariantCopy 函数来复制字符串、复制 COM 对象(增加引用计数)。
您唯一应该担心的是当您在 VARIANT 中传递实际接口时:在这种情况下,应该对接口进行正确的引用计数,并且 QueryInterface、AddRef 和 Release 都应该相应地实现。 并且在引用计数达到 0 之前永远不要释放该对象;)

编辑:哦,如果您想了解有关 COM 编程的更多信息,请务必获取“Don Box”的“Essential COM”。 Don Box 是 COM 大师,那本书将教您几乎所有您想了解的有关 COM 的知识以及更多知识;)

When you call a COM function with strings or VARIANTs in most cases the only garantuee needed is that those objects are available throughout the call itself. After the call, the object itself is responsible for making copies of the data. For example VARIANT's will most likely use the VariantCopy function that will copy strings, copy COM objects (increases reference count).
The only thing you should worry about is when you pass an actual interface in the VARIANT: in that case the interface should be properly reference counted, and QueryInterface, AddRef, and Release should all be implemented accordingly. And don't ever deallocate that object before the reference count reaches 0 ;)

EDIT: Oh, and if you want to learn more about COM programming, be sure to get "Essential COM" by "Don Box". Don Box is the COM guru, and that book will teach you almost anything you would ever want to know about COM and more ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文