extravi
extravi

Reputation: 1

Error 8001010E: "The application called an interface that was marshalled for a different thread" in Graphics Capture API

I'm getting the following error when trying to create a screen capture session. I'm using winrt::init_apartment() and co_await, but it’s still failing.

GraphicsCapture.dll!00007FFA84F4FD63: ReturnHr(1) tid(3f04) 8001010E The application called an interface that was marshalled for a different thread.
    Msg:[winrt::hresult_error: The application called an interface that was marshalled for a different thread.]

The error seems to have something to do with this line:

auto captureItem = co_await capturePicker.PickSingleItemAsync();
    winrt::IAsyncOperation<winrt::GraphicsCaptureItem> CreateCaptureSession()
    {
        winrt::init_apartment();

        auto isCaptureSupported = winrt::Windows::Graphics::Capture::GraphicsCaptureSession::IsSupported();
        if (!isCaptureSupported)
        {
            MessageBoxW(NULL, L"screen capture is not supported on this device for this release of windows!", L"Error", MB_OK | MB_ICONERROR);
            throw std::runtime_error("screen capture is not supported on this device for this release of windows!");
        }

        RECT windowRect;
        GetWindowRect(targetHwnd, &windowRect);

        printf("window rectangle: left=%d, top=%d, right=%d, bottom=%d\n",
            windowRect.left, windowRect.top, windowRect.right, windowRect.bottom);

        printf("native: %d x %d\n", screenWidth, screenHeight);

        winrt::apartment_context context;

        auto capturePicker = winrt::Windows::Graphics::Capture::GraphicsCapturePicker();
        auto captureItem = co_await capturePicker.PickSingleItemAsync();

        if (!captureItem)
        {
            MessageBoxW(NULL, L"failed to select a capture item!", L"Error", MB_OK | MB_ICONERROR);
            throw std::runtime_error("failed to select a capture item!!");
        }

        winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice device = CreateD3DDevice();
        auto d3dDevice = GetDXGIInterfaceFromObject<ID3D11Device>(device);
        winrt::com_ptr<ID3D11DeviceContext> d3dContext;
        d3dDevice->GetImmediateContext(d3dContext.put());

        if (d3dDevice)
        {
            std::printf("successfully retrieved ID3D11Device\n");
        }

        auto pixelFormat = winrt::Windows::Graphics::DirectX::DirectXPixelFormat::B8G8R8A8UIntNormalized;

        auto framePool = winrt::Direct3D11CaptureFramePool::CreateFreeThreaded(
            device,
            pixelFormat,
            1,
            captureItem.Size());
        auto session = framePool.CreateCaptureSession(captureItem);

        printf("CreateCaptureSession success\n");
    }

Upvotes: 0

Views: 46

Answers (0)

Related Questions