智能指针_com_ptr_t

对于COM调用,大量的AddRef/Release调用无疑是例行公事和让人生厌。

_com_ptr_t 是vc运行库中提供的固有的封装COM接口的智能指针,相比CComPtr/CComQIPtr缺少移植性,

但是支持不是ATL实现部分的的异常和操作。

以下是DirectShow中播放一个文件的代码:

    IGraphBuilder *pGraph;
    IMediaControl *pMediaControl;
    IMediaEvent   *pEvent;
    
    // Create the filter graph manager and query for interfaces.
    CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
                        IID_IGraphBuilder, (void **)&pGraph);
    pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl);
    pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

    // Build the graph. IMPORTANT: Change string to a file on your system.
    pGraph->RenderFile(L"C://Example.avi", NULL);

    // Run the graph.
    pMediaControl->Run();

    // Wait for completion. 
    long evCode;
    pEvent->WaitForCompletion(INFINITE, &evCode);

    // Clean up.
    pMediaControl->Release();
    pEvent->Release();
    pGraph->Release();


使用com_ptr_t后简单了很多:

//这部分可以放到头文件中

#include 
_COM_SMARTPTR_TYPEDEF( IGraphBuilder , __uuidof(IGraphBuilder) );
_COM_SMARTPTR_TYPEDEF( IMediaControl , __uuidof(IMediaControl) );
_COM_SMARTPTR_TYPEDEF( IMediaEvent , __uuidof(IMediaEvent) );

...

    IGraphBuilderPtr pGraph( CLSID_FilterGraph ) ;//调用CreateInstance
    IMediaControlPtr pMediaControl = pGraph;//这里会自动调用QueryInterface  
    IMediaEventPtr pEvent = pGraph;//这里会自动调用QueryInterface
  
  pGraph->RenderFile( L"L"C://Example.avi", NULL); 
  

    // Run the graph.
    pMediaControl->Run();

    // Wait for completion. 
    long evCode;
    pEvent->WaitForCompletion(INFINITE, &evCode);
//不需要调用Release()

假如我们要复制一个接口,常常要

pG1 = pG2;

pG1->AddRef();

...

pG1->Release();

对于_com_ptr_t , 只要pG1 = pG2;就可以了


注意以下的代码:

IGraphBuilderPtr pGraph ;

pGraph.CreateInstance( CLSID_FilterGraph );

...

CoUninitialize();

因为 pGraph 会在超出作用域时析构,也就是在CoUninitialize()之后调用 IUnkown::Release() ,这样

会引起问题,可以这样解决:

pGraph.Release();  CoUninitialize();

或者

pGraph = NULL;  CoUninitialize(); //这样会隐含的调用_com_ptr_t::Release()

绝对不可以调用pGraph->Release();  因为这是调用IUnkown::Release()然后再调用一次

pGraph.Release();  将会两次释放接口指针。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ljfxmf/archive/2008/06/04/2511029.aspx

0

你可能感兴趣的:(智能指针_com_ptr_t)