以下函数创建一个具有指定类标识符(CLSID)的过滤器,并将其添加到过滤器图形中:
// Create a filter by CLSID and add it to the graph.
HRESULT AddFilterByCLSID(
IGraphBuilder *pGraph, // Pointer to the Filter Graph Manager.
REFGUID clsid, // CLSID of the filter to create.
IBaseFilter **ppF, // Receives a pointer to the filter.
LPCWSTR wszName // A name for the filter (can be NULL).
)
{
*ppF = 0;
IBaseFilter *pFilter = NULL;
HRESULT hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pFilter));
if (FAILED(hr))
{
goto done;
}
hr = pGraph->AddFilter(pFilter, wszName);
if (FAILED(hr))
{
goto done;
}
*ppF = pFilter;
(*ppF)->AddRef();
done:
SafeRelease(&pFilter);
return hr;
}
该函数调用CoCreateInstance创建过滤器,然后调用IFilterGraph :: AddFilter将过滤器添加到图形中。 以下代码示例使用此功能将AVI Mux过滤器添加到图形中:
IBaseFilter *pMux;
hr = AddFilterByCLSID(pGraph, CLSID_AviDest, L"AVI Mux", &pMux, NULL);
if (SUCCEEDED(hr))
{
/* ... */
pMux->Release();
}
注意,有些过滤器不能用CoCreateInstance创建。 管理其他软件组件的过滤器通常就是这种情况。 例如,AVI Compressor过滤器是视频编解码器的包装,WDM视频捕获过滤器是WDM捕获驱动程序的包装。 这些过滤器必须使用System Device Enumerator或Filter Mapper来创建。
参考:
https://www.yuque.com/docs/share/a239f7a9-5a46-4e90-997e-d89d38ac6667