【问题标题】:C++/WinRT: MCVC doesn't linking event callbackC++/WinRT:MCVC 不链接事件回调
【发布时间】:2022-01-16 16:56:06
【问题描述】:

我正在尝试在 c++ 项目中使用 Desktop Capture API。

这里是帧池的初始化:

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

这里是ScreenGrabWinCaptureApi::OnFrameArrived 定义:

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

我正在尝试构建它,编译器似乎可以使用代码,但在 framePool.FrameArrived(&amp;ScreenGrabWinCaptureApi::OnFrameArrived); 调用时链接失败

错误 LNK2019:未解析的外部符号“公共:__cdecl winrt::Windows::Foundation::TypedEventHandler::TypedEventHandler)(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@P 8ScreenGrabWinCaptureApi@@EAAXAEBUDirect3D11CaptureFramePool@Capture@Graphics@23@AEBUIInspectable@123@@Z@Z) 在函数“public: __cdecl ScreenGrabWinCaptureApi::ScreenGrabWinCaptureApi(void)”中引用 (??0ScreenGrabWinCaptureApi@@QEAA@XZ)

我已经尝试了重新解释/静态转换的所有方式,用方法引用引入变量,用 clojure 替换方法,但没有任何效果。任何人都知道什么是原因以及如何使其运行?

【问题讨论】:

  • 这可能是由于缺少标头包含引起的:devblogs.microsoft.com/oldnewthing/20190529-00/?p=102527
  • 没有类实例就不能调用成员函数指针。您将不得不将类实例传递给 TypedEventHandler 的 c'tor,例如framePool.FrameArrived({ this, &amp;ScreenGrabWinCaptureApi::OnFrameArrived });。不过,使用弱指针通常是个好主意,因此将this 替换为get_weak(),并尝试从事件处理程序中使用get_strong()(请参阅Strong and weak references in C++/WinRT)。
  • 谢谢@SimonMourier,这是一个原因。

标签: linker windows-runtime c++-winrt


【解决方案1】:

正如Simon Mourier 在 cmets 中所说,我忘记将标题包含到TypedEventHandler。我的代码在插入相应的包括后工作:

#include "winrt/Windows.Foundation.h"

【讨论】:

  • 如果您已经解决了您的问题,请mark它被接受
猜你喜欢
  • 2020-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 2010-10-31
  • 1970-01-01
  • 2012-04-14
相关资源
最近更新 更多