Repository: microsoft/PowerToys
File: src/common/utils/elevation.h (C/C++)

Code before:

        CComPtr<IShellView> spView;
        result = spBrowser->QueryActiveShellView(&spView);
        if (result != S_OK)
        {
            Logger::warn(L"Failed to query active shell window. {}", GetErrorString(result));
            return false;
        }

        result = spView->QueryInterface(riid, ppv);
        if (result != S_OK)
        {
            Logger::warn(L"Failed to query interface. {}", GetErrorString(result));
            return false;
        }

        return true;
    }

    inline bool GetDesktopAutomationObject(REFIID riid, void** ppv)
    {
        CComPtr<IShellView> spsv;
        
        // Desktop may not be available on startup
        auto maxAttempts = 5;
        for (auto i = 1; !FindDesktopFolderView(IID_PPV_ARGS(&spsv)) && i <= maxAttempts; i++)
        {
            Logger::warn(L"FindDesktopFolderView() failed attempt {}", i);

            if (i == maxAttempts)
            {
                Logger::warn(L"FindDesktopFolderView() max attempts reached");
                return false;
            }

            Sleep(3000);
        }

        CComPtr<IDispatch> spdispView;
        auto result = spsv->GetItemObject(SVGIO_BACKGROUND, IID_PPV_ARGS(&spdispView));
        if (result != S_OK)
        {
            Logger::warn(L"GetItemObject() failed. {}", GetErrorString(result));
            return false;
        }

        result = spdispView->QueryInterface(riid, ppv);
        if (result != S_OK)
        {
            Logger::warn(L"QueryInterface() failed. {}", GetErrorString(result));
            return false;

Review comment (suggestion):
let's move `!FindDesktopFolderView(IID_PPV_ARGS(&spsv))` to the body of `for` loop to improve clarity 🙂 

Code after:

        CComPtr<IShellView> spView;
        result = spBrowser->QueryActiveShellView(&spView);
        if (result != S_OK)
        {
            Logger::warn(L"Failed to query active shell window. {}", GetErrorString(result));
            return false;
        }

        result = spView->QueryInterface(riid, ppv);
        if (result != S_OK)
        {
            Logger::warn(L"Failed to query interface. {}", GetErrorString(result));
            return false;
        }

        return true;
    }

    inline bool GetDesktopAutomationObject(REFIID riid, void** ppv)
    {
        CComPtr<IShellView> spsv;
        
        // Desktop may not be available on startup
        auto attempts = 5;
        for (auto i = 1; i <= attempts; i++)
        {
            if (FindDesktopFolderView(IID_PPV_ARGS(&spsv)))
            {
                break;
            }

            Logger::warn(L"FindDesktopFolderView() failed attempt {}", i);

            if (i == attempts)
            {
                Logger::warn(L"FindDesktopFolderView() max attempts reached");
                return false;
            }

            Sleep(3000);
        }

        CComPtr<IDispatch> spdispView;
        auto result = spsv->GetItemObject(SVGIO_BACKGROUND, IID_PPV_ARGS(&spdispView));
        if (result != S_OK)
        {
            Logger::warn(L"GetItemObject() failed. {}", GetErrorString(result));
            return false;
        }