Microsoft Media Foundation和COM [英] Microsoft Media Foundation and COM

查看:85
本文介绍了Microsoft Media Foundation和COM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All。



我正在尝试使用Microsoft Media Foundation来实现一个自定义媒体播放器,该播放器具有我在其他播放器中找不到的功能。我是MF和COM的新手,我正在使用Microsoft Press的书开发Microsoft Media Foundation Applications 作为指南。



我明白MF与COM交互。本书中的代码示例使用CComPtr类来处理引用计数等。例如:

Hello All.

I am trying to use Microsoft Media Foundation to implement a customized media player that would have some functionality that I have not found in other players. I am a newbie when it comes to MF and COM, and am using the book Developing Microsoft Media Foundation Applications from Microsoft Press as a guide.

I understand that MF interacts with COM. The code examples in the book use the CComPtr class to handle reference counting, etc. For example:

CComPtr<IMFTopology> pTopology = NULL;





话虽如此,但并非书中MF对象的每次初始化都使用这种方法。例如,IMFMediaSession。所以我的问题是:在什么情况下CComPtr(或AddRef / Release)需要在我的代码中使用?我如何知道哪些接口需要这个?严格来说,从我的课程中使用COM时,引用计数是强制性的吗?这本书并没有让我满意,我也无法通过MSDN找到足够的答案。



希望我的问题很清楚。最好的问候。



That being said, not every initialization of a MF object in the book uses this approach. For example, IMFMediaSession. So my question is this: under what circumstances would CComPtr (or AddRef/Release) need to be used in my code? How do I know which interfaces would require this? Strictly speaking, is reference counting mandatory when utilizing COM from my classes? The book does not cover this question to my satisfaction, and I have not been able to find a sufficient answer via MSDN either.

Hopefully my question is clear. Best regards.

推荐答案

问题的关键在于CComPtr< t>在很大程度上管理它,维护自己的引用并在创建时调用AddRef()和销毁时调用Release()(使用RAII习语() RAII - 维基百科 [ ^ ])。每当你举行指向COM对象的指针,通常将其包装在CComPtr< t>中,C ++将为您正确管理AddRef / Release。



偶尔,您可以使用指针直接到接口。例如,如果你编写的函数接收IMFMediaSession *作为参数,你使用参数的唯一方法是直接调用方法(使用 - >),那么你不需要使用AddRef / Release(确保检查为null)。



主要考虑因素是参考的生命周期。例如,总是使用CComPtr< t>:



1.会员一个类或结构体,当对象被销毁时子对象将被破坏(因此被释放)。

2.在一个方法中,当你调用另一个可能复制引用的函数时(如果你不确定 - 使用CComPtr< t>安全。



希望这会有所帮助。



(更新)

COM概述在这里组件对象模型 [ ^ ]

管理引用计数的规则如下:参考计数 [ ^ ]
The crux if the issue is that CComPtr<t> manages this to a large extent, maintaining its own reference and calling AddRef() when created and Release() when destroyed (using the RAII idiom (RAII - Wikipedia[^]). Whenever you hold a pointer to a COM object, generally wrap it in a CComPtr<t> and C++ will correctly managed AddRef/Release for you.

Occasionally, you can use a pointer to the interface directly. For example, if a function you write receives an IMFMediaSession* as a parameter, and the only way you use the parameter is to directly call methods (using -> ), then you don''t need to use AddRef/Release (be sure to check for null though).

The main consideration is lifetime of the reference. Always, for example, use CComPtr<t> for:

1. Members of a class or struct, where the sub-object will get destructed (hence Released) when the object is destroyed.
2. In a method, when you invoke another function that may possibly copy the reference (if you''re not sure - use CComPtr<t> for safety.

Hope this helps.

(update)
The COM overview is here Component Object Model[^]
Rules for managing reference counts are here: Reference Counts[^]


这篇关于Microsoft Media Foundation和COM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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