如何用ATL重写下面的函数

如何用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)
        {
            // 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;
}

最佳答案

通常的想法是您使用CComPtr和其他类似的东西,例如CComBSTRCComVariantCComQIPtr

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


变成:

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


这使您的代码更小,更清晰-无需为每个指针调用Release(),因此可以通过提早返回来减少嵌套。

关于c++ - 如何用ATL重写下面的函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3700634/

10-11 16:12