开发者

How to rewrite the function below with ATL?

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)
        {
            //开发者_Python百科 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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜