Add a Filter by CLSID

 
Microsoft DirectShow 9.0

Add a Filter by CLSID

The following function creates a filter with a specified class identifier (CLSID) and adds it to the filter graph:

HRESULT AddFilterByCLSID(
    IGraphBuilder *pGraph,  // Pointer to the Filter Graph Manager.
    const GUID& clsid,      // CLSID of the filter to create.
    LPCWSTR wszName,        // A name for the filter.
    IBaseFilter **ppF)      // Receives a pointer to the filter.
{
    if (!pGraph || ! ppF) return E_POINTER;
    *ppF = 0;
    IBaseFilter *pF = 0;
    HRESULT hr = CoCreateInstance(clsid, 0, CLSCTX_INPROC_SERVER,
        IID_IBaseFilter, reinterpret_cast<void**>(&pF));
    if (SUCCEEDED(hr))
    {
        hr = pGraph->AddFilter(pF, wszName);
        if (SUCCEEDED(hr))
            *ppF = pF;
        else
            pF->Release();
    }
    return hr;
}

The function calls CoCreateInstance to create the filter, and then calls IFilterGraph::AddFilter to add the filter to the graph. The following code example uses this function to add the AVI Mux filter to the graph:

IBaseFilter *pMux;
hr = AddFilterByCLSID(pGraph, CLSID_AviDest, L"AVI Mux", &pMux); 
if (SUCCEEDED(hr))
{
    /* ... */
   pMux->Release();
}

Note that some filters cannot be created with CoCreateInstance. This is often the case with filters that manage other software components. For example, the AVI Compressor filter is a wrapper for video codecs, and the WDM Video Capture filter is a wrapper for WDM capture drivers. These filters must be created using either the System Device Enumerator or the Filter Mapper. For more information, see Enumerating Devices and Filters.