我正在尝试使用 Microsoft 在 Win32 C# WPF 应用程序内的一个 UWP 示例示例中使用的 SimpleCommunication C++ DLL。
我尝试添加对在将 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 将能够找到并激活您的类型。
(注意:您不必将应用程序提交到应用商店即可使用 Desktop Bridge;它只是一种打包应用程序的机制)。
关于c# - 在 Win32 C# WPF 应用程序中使用 UWP C++ dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51786164/