views:

51

answers:

1
HRESULT EnumerateVideoInputFilters(void** gottaFilter)
{
    // Once again, code stolen from the DX9 SDK.
    // Create the System Device Enumerator.
    ICreateDevEnum *pSysDevEnum = NULL;
    HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, 
                                  CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, 
                                  (void **)&pSysDevEnum);
    if (FAILED(hr))
    {
        return hr;
    }

    // Obtain a class enumerator for the audio input category.
    IEnumMoniker *pEnumCat = NULL;
    hr = pSysDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, 
                                            &pEnumCat, 0);

    if (hr == S_OK) 
    {
        // Enumerate the monikers.
        IMoniker *pMoniker = NULL;
        ULONG cFetched;
        if (pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK)
        {
            // Bind the first moniker to an object.
            IPropertyBag *pPropBag;
            hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, 
                (void **)&pPropBag);
            if (SUCCEEDED(hr))
            {
                // To retrieve the filter's friendly name, 
                // do the following:
                VARIANT varName;
                VariantInit(&varName);
                hr = pPropBag->Read(L"FriendlyName", &varName, 0);
                if (SUCCEEDED(hr))
                {
                    wprintf(L"Selecting Audio Input Device: %s\n", 
                            varName.bstrVal);
                }
                VariantClear(&varName);

                // To create an instance of the filter, 
                // do the following:
                // Remember to release gottaFilter later.
                hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter, 
                                            gottaFilter);
                pPropBag->Release();
            }
            pMoniker->Release();
        }
        pEnumCat->Release();
    }
    pSysDevEnum->Release();
    return hr;
}
+4  A: 

General idea is that you use CComPtr and other similar things like CComBSTR, CComVariant, CComQIPtr:

ISomeInterface* pointer = 0;
HRESULT hr = whatever->Obtain( &pointer );
if( SUCCEDED( hr ) ) {
    hr = pointer-DoSomething();
    pointer->Release();
}
return hr;

becomes:

CComPtr<ISomeInterface> pointer;
HRESULT hr = whatever->Obtain( &pointer );
if( FAILED( hr ) ) {
    return hr;
}
r = pointer-DoSomething();
return hr;

This makes your code smaller and cleaner - you don't need to call Release() for each pointer and thus you can reduce nesting by returning early.

sharptooth
Do I need to call `CoInitialize` and `CoUninitialize` then?
Alan
@Alan: You need to call them if and only if you needed them before - using the classes above doesn't change that.
sharptooth