如何用ATL重写下面的函数?

发布于 2024-09-18 12:19:53 字数 2108 浏览 6 评论 0原文

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;
}
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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风轻花落早 2024-09-25 12:19:53

一般想法是,您使用 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() ,因此您可以通过提前返回来减少嵌套。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文