共用方式為


依 CLSID 新增篩選

[與此頁面相關的功能 DirectShow是舊版功能。 它已被 MediaPlayerIMFMediaEngineMedia Foundation 中的音訊/視訊擷取取代。 這些功能已針對Windows 10和Windows 11進行優化。 Microsoft 強烈建議新程式碼盡可能使用 MediaPlayerIMFMediaEngine音訊/視訊擷取 ,而不是 DirectShow。 Microsoft 建議使用舊版 API 的現有程式碼盡可能重寫為使用新的 API。

下列函式會建立具有指定類別識別碼的篩選, (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;
}

注意

此範例會使用 SafeRelease 函式來釋放 IBaseFilter 指標。

 

函式會呼叫 CoCreateInstance 來建立篩選,然後呼叫 IFilterGraph::AddFilter 將篩選新增至圖形。 下列程式碼範例會使用此函式將 AVI Mux 篩選新增至圖表:

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

請注意,某些篩選無法使用 CoCreateInstance建立。 這通常是管理其他軟體元件的篩選準則。 例如, AVI 篩選 條件是視訊編解碼器的包裝函式, 而 WDM 視訊擷取篩選器則是 WDM 擷取 驅動程式的包裝函式。 您必須使用 系統裝置列舉值篩選對應器來建立這些篩選。 如需詳細資訊,請參閱 列舉裝置和篩選

一般Graph-Building技術