Desktop duplication screen capturing - DuplicateOutput returns E_ACCESSDENIED error

1.7k views Asked by At

I'm capturing the screens using desktop duplication APIs (DirectX11). The DuplicateOutput API returns the access denied error and that too happens very rare(may be 10% of the time) on a windows 8.1 machine on logon screen though my application is running with SYSTEM level privileges and the SetThreadDesktop being called properly. I used to reset and call SetThreadDesktop after every error I got but the application couldn't recover from the error after that even after multiple device resets and inits. I had to fallback to GDI (application works fine after being switched from directx to GDI) based approach after multiple retries or restart the application but that idea seems terrible.

Note: I did come across the same problem on Windows 10/ Windows 8 machines but not too often compared to that particular windows 8.1 machine.

here is the description of the E_ACCESSDENIED error that tells only possible case (not having system level privileges or the SetThreadDesktop not being called properly) for this error. I tried all the possible ways to find out the problem but couldn't.

Any help would be appreciated, Thanks in advance.

Here is the code to init the device:

    //
// Initialize duplication interfaces
//
HRESULT cDuplicationManager::InitDupl(_In_ ID3D11Device* Device, _In_ IDXGIAdapter *_pAdapter, _In_ IDXGIOutput *_pOutput, _In_ UINT Output)
{
    HRESULT hr = E_FAIL;

    if(!_pOutput || !_pAdapter || !Device)
    {
        return hr;
    }

    m_OutputNumber = Output;

    // Take a reference on the device
    m_Device = Device;
    m_Device->AddRef();

    _pOutput->GetDesc(&m_OutputDesc);
     // QI for Output 1
    IDXGIOutput1* DxgiOutput1 = nullptr;
    hr = _pOutput->QueryInterface(__uuidof(DxgiOutput1), reinterpret_cast<void**>(&DxgiOutput1));

    if (FAILED(hr))
    {
        return ProcessFailure(nullptr, _T("Failed to QI for DxgiOutput1 in DUPLICATIONMANAGER"), _T("Error"), hr);
    }

    // Create desktop duplication
    hr = DxgiOutput1->DuplicateOutput(m_Device, &m_DeskDupl);

    DxgiOutput1->Release();
    DxgiOutput1 = nullptr;


    if (FAILED(hr) || !m_DeskDupl)
    {
        if (hr == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE)
        {
            return ProcessFailure(nullptr, _T("Maximum number of applications using Desktop Duplication API"), _T("Error"), hr);
        }

        return ProcessFailure(m_Device, _T("Failed to get duplicate output in DUPLICATIONMANAGER"), _T("Error"), hr);//, CreateDuplicationExpectedErrors);
    }

    return S_OK;
}

The code to set the desktop to current thread:

    DWORD setCurrentInputDesktop()
{
    DWORD errorCode = ERROR_ACCESS_DENIED;


    HDESK currentInputDesktop = OpenInputDesktop(0, false, GENERIC_ALL);

    if(currentInputDesktop != NULL)
    {
        if(!SetThreadDesktop(currentInputDesktop))
        {
            errorCode = GetLastError();
            cout << ("setCurrentInputDesktop: SetThreadDesktop failed. Error code: ") << errorCode;
        }
        else
        {
            errorCode = ERROR_SUCCESS;

            cout << ("setCurrentInputDesktop: SetThreadDesktop succeeded.");
        }

        CloseDesktop(currentInputDesktop);
    }
    else
    {
        errorCode = GetLastError();

        cout << "setCurrentInputDesktop: OpenInputDesktop failed. Error code: " << errorCode;
    }

    return errorCode;
}

Here is the error message returned after processing the error code:

Id3d11DuplicationManager::ProcessFailure - Error: Failed to get duplicate output in DUPLICATIONMANAGER, Detail: Access is denied.

0

There are 0 answers