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

Using a UWP C++ dll in Win32 C# WPF application

我正在尝试在 Win32 C# WPF 应用程序中的一个 UWP 示例示例中使用 Microsoft 的 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 应用程序调用这些函数,但我不确定最好的方法是什么,任何帮助将不胜感激!

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

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