双指针函数参数和CComPtr
Double pointer function argument and CComPtr
我不确定在函数内部使用CComPtr的这种方式,该函数的参数表示为双指针:
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;
}
我对最后一个赋值+ AddRef调用有疑问。
它们适合你吗?
Thanks in advance
可以,但可以简化:
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;
}
在你的代码中,AddRef
是必要的,因为当pSample
超出作用域时,它会替换Release
。
更习惯的版本是
// Transfer the pointer to our caller.
*ppVideoSample = pSample.Detach();
如果你想复制语义而不是传输,你可以使用
pSample.CopyTo(ppVideoSample);
AddRef()
的赋值解引用是正确的
当调用MFCreateVideoSampleFromSurface()
时,它的第二个参数是指向接口的指针应该存储的位置。使用&pSample
获取要传递给函数的地址。这与所需的IMFSample **
类型匹配。注意,通过CComPtrBase<>
对CComPtr<>
执行&
运算符返回正确的类型。
CComPtrBase::,operator @ MSDN
ppVideoSample
也是类型IMFSample **
,这需要*
操作符对接口指针解引用。这将产生一个类型为IMFSample *
的指针,您可以使用->
操作符调用它来访问AddRef()
和接口上的其他函数。
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 双指针函数参数和CComPtr