C++ 双指针函数参数和CComPtr
我不确定在具有双指针表示的参数的函数中使用CComPtr的这种方式:C++ 双指针函数参数和CComPtr,c++,atl,smart-pointers,C++,Atl,Smart Pointers,我不确定在具有双指针表示的参数的函数中使用CComPtr的这种方式: HRESULT D3DPresentEngine::CreateD3DSample( IDirect3DSwapChain9 *pSwapChain, IMFSample **ppVideoSample ) { // Caller holds the object lock. D3DCOLOR clrBlack = D3DCOLOR_ARGB(0xFF, 0x00, 0x00, 0x
HRESULT D3DPresentEngine::CreateD3DSample(
IDirect3DSwapChain9 *pSwapChain,
IMFSample **ppVideoSample
)
{
// Caller holds the object lock.
D3DCOLOR clrBlack = D3DCOLOR_ARGB(0xFF, 0x00, 0x00, 0x00);
CComPtr< IDirect3DSurface9 > pSurface;
CComPtr< IMFSample > pSample;
// Get the back buffer surface.
ReturnIfFail( pSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pSurface ) );
// Fill it with black.
ReturnIfFail( m_pDevice->ColorFill(pSurface, NULL, clrBlack));
// Create the sample.
ReturnIfFail( MFCreateVideoSampleFromSurface(pSurface, &pSample));
// Return the pointer to the caller.
*ppVideoSample = pSample;
(*ppVideoSample)->AddRef();
return S_OK;
}
HRESULT D3DPresentEngine::CreateD3DSample(
IDirect3DSwapChain9*pSwapChain,
IMFSample**ppVideoSample
)
{
//调用者持有对象锁。
D3DCOLOR CLRBLOCK=D3DCOLOR_ARGB(0xFF,0x00,0x00,0x00);
CComPtrpSurface;
CComPtrpSample;
//获取后缓冲区表面。
returniFail(pSwapChain->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO和pSurface));
//把它装满黑色的。
returnifail(m_pDevice->ColorFill(pSurface,NULL,clrback));
//创建示例。
ReturniFail(MFCreateVideoSampleFromSurface(pSurface和pSample));
//返回指向调用者的指针。
*ppVideoSample=pSample;
(*ppVideoSample)->AddRef();
返回S_OK;
}
我对最后一次分配+AddRef电话有疑问
它们适合你吗
提前感谢没关系,但可以简化:
HRESULT D3DPresentEngine::CreateD3DSample(
IDirect3DSwapChain9 *pSwapChain,
IMFSample **ppVideoSample
)
{
// Caller holds the object lock.
D3DCOLOR clrBlack = D3DCOLOR_ARGB(0xFF, 0x00, 0x00, 0x00);
CComPtr< IDirect3DSurface9 > pSurface;
// Get the back buffer surface.
ReturnIfFail( pSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pSurface ) );
// Fill it with black.
ReturnIfFail( m_pDevice->ColorFill(pSurface, NULL, clrBlack));
// Create the sample.
ReturnIfFail( MFCreateVideoSampleFromSurface(pSurface, ppVideoSample));
return S_OK;
}
HRESULT D3DPresentEngine::CreateD3DSample(
IDirect3DSwapChain9*pSwapChain,
IMFSample**ppVideoSample
)
{
//调用者持有对象锁。
D3DCOLOR CLRBLOCK=D3DCOLOR_ARGB(0xFF,0x00,0x00,0x00);
CComPtrpSurface;
//获取后缓冲区表面。
returniFail(pSwapChain->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO和pSurface));
//把它装满黑色的。
returnifail(m_pDevice->ColorFill(pSurface,NULL,clrback));
//创建示例。
ReturniFail(MFCreateVideoSampleFromSurface(pSurface,ppVideoSample));
返回S_OK;
}
在您的代码中,
AddRef
是必需的,因为pSample
在超出范围时将释放。AddRef()
的赋值和取消引用是正确的
调用MFCreateVideoSampleFromSurface()
时,它的第二个参数是指向接口的指针应存储的位置。您可以使用&pSample
获取要传递给函数的地址。这与所需的IMFSample**
类型匹配。请注意,CComPtr
上的和运算符通过CComPtrBase
返回正确的类型
ppVideoSample
也是类型IMFSample**
,它要求*
操作符取消对接口指针的引用。这将生成一个类型为IMFSample*
的指针,您可以使用->
操作符调用它来访问AddRef()
和界面上的其他函数。更惯用的版本是
// Transfer the pointer to our caller.
*ppVideoSample = pSample.Detach();
如果您想要复制语义而不是传输,那么可以使用
pSample.CopyTo(ppVideoSample);