c# - 在 Win32 C# WPF 应用程序中使用 UWP C++ dll

标签 c# c++ uwp interop

我正在尝试使用 Microsoft 在 Win32 C# WPF 应用程序内的一个 UWP 示例示例中使用的 SimpleCommunication C++ DLL。

https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/SimpleCommunication/common/MediaExtensions/Microsoft.Samples.SimpleCommunication

我尝试添加对在将 SimpleCommunication .dll 构建到我的 Win32 C# WPF 应用程序中时生成的 .dll 和 .winmd 文件的引用,但是每当我尝试从 SimpleCommunication 调用任何函数时,我都会得到以下信息异常消息:

Exception thrown: 'System.TypeLoadException' in mscorlib.dll An unhandled exception of type 'System.TypeLoadException' occurred in mscorlib.dll Could not find Windows Runtime type 'Microsoft.Samples.SimpleCommunication.StspMediaSinkProxy'.

据我所知,这个 C++ dll 是作为 C++/CX 构建的(因为它在编译器中使用/ZW 标志),因此它包含非托管代码,所以我假设我需要一些某种包装器能够从我的托管 wpf 应用程序调用这些函数,但我不太确定解决此问题的最佳方法是什么,任何帮助将不胜感激!

enter image description here

最佳答案

尽管 WPF 应用程序可以使用系统提供的 WinRT 类型(Windows 命名空间中的类型),但它们无法使用自定义 WinRT 类型,因为 Windows 不知道如何查找他们。解决办法是use the Desktop Bridge将您的 WPF 应用程序添加到 AppX 包中,此时 Windows 将能够找到并激活您的类型。

(注意:您不必将应用程序提交到应用商店即可使用 Desktop Bridge;它只是一种打包应用程序的机制)。

关于c# - 在 Win32 C# WPF 应用程序中使用 UWP C++ dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51786164/

相关文章:

c# - WPF 中部分透明的 .png

C# 字符串连接和字符串驻留

c# - 评估 Lambda 表达式作为表达式树的一部分

c++ - 字符串类实现的差异

c++ - boost::make_zip_iterator 的 decltype?

c# - 使用 Newtonsoft Json 从流中反序列化多个 json 对象

c++ - 使用 ffmpeg 无法解析的外部符号

c# - 将事件绑定(bind)到方法,为什么在UWP中有效?

c# - 如何为 UAP 拍摄媒体元素的当前帧(Windows 10)

windows-10 - 在 UWP 中隐藏状态栏