Search code examples
linkerwindows-runtimec++-winrt

C++/WinRT: MCVC doesn't linking event callback


I'm trying to use Desktop Capture API in c++ project.

Here is initialisation of frame pool:

ScreenGrabWinCaptureApi::ScreenGrabWinCaptureApi() {
    //
    // <neccessary stuff for d3d device and capture item creation>
    //

    framePool = winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::CreateFreeThreaded(d3dDevice2,
                                                                                                  winrt::Windows::Graphics::DirectX::DirectXPixelFormat::B8G8R8A8UIntNormalized,
                                                                                                  2, item.Size());
    framePool.FrameArrived(&ScreenGrabWinCaptureApi::OnFrameArrived);
    captureSession = framePool.CreateCaptureSession(item);
    captureSession.StartCapture();
}

and here is ScreenGrabWinCaptureApi::OnFrameArrived definition:

void
ScreenGrabWinCaptureApi::OnFrameArrived(const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool &sender,
                                        const winrt::Windows::Foundation::IInspectable &) {
    // <some buisness logic>
}

I'm trying to build this, and code seems OK to compiler, but linkage fails on framePool.FrameArrived(&ScreenGrabWinCaptureApi::OnFrameArrived); call with

error LNK2019: unresolved external symbol "public: __cdecl winrt::Windows::Foundation::TypedEventHandler<struct winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool,struct winrt::Windows::Foundation::IInspectable>::TypedEventHandler<struct winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool,struct winrt::Windows::Foundation::IInspectable><void (__cdecl ScreenGrabWinCaptureApi::)(struct winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool const &,struct winrt::Windows::Foundation::IInspectable const &)>(void (__cdecl ScreenGrabWinCaptureApi::)(struct winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool const &,struct winrt::Windows::Foundation::IInspectable const &))" (??$?0P8ScreenGrabWinCaptureApi@@EAAXAEBUDirect3D11CaptureFramePool@Capture@Graphics@Windows@winrt@@AEBUIInspectable@Foundation@45@@Z@?$TypedEventHandler@UDirect3D11CaptureFramePool@Capture@Graphics@Windows@winrt@@UIInspectable@Foundation@45@@Foundation@Windows@winrt@@QEAA@P8ScreenGrabWinCaptureApi@@EAAXAEBUDirect3D11CaptureFramePool@Capture@Graphics@23@AEBUIInspectable@123@@Z@Z) referenced in function "public: __cdecl ScreenGrabWinCaptureApi::ScreenGrabWinCaptureApi(void)" (??0ScreenGrabWinCaptureApi@@QEAA@XZ)

I've tried all the way of reinterpret/static casts, introducing variable with method reference, replacing method with clojure, but nothing works. Anybody knows what is reason and how to make this running?


Solution

  • As Simon Mourier said in the comments, I've forgotten to include header to TypedEventHandler. My code works after insertion of the corresponding include:

    #include "winrt/Windows.Foundation.h"