问题描述
我正尝试在Win32 C#WPF应用程序中的一个UWP示例示例中使用Microsoft的SimpleCommunication C ++ DLL.
I'm attempting to use the SimpleCommunication C++ DLL that Microsoft in one of their UWP sample examples inside a Win32 C# WPF application.
我试图添加对在Win32 C#WPF应用程序中构建SimpleCommunication .dll时生成的.dll和.winmd文件的引用,但是,每当我尝试从SimpleCommunication调用任何函数时,我都会得到以下信息异常消息:
I've tried to add a reference to the both the .dll and .winmd files that are generated when building the SimpleCommunication .dll into my Win32 C# WPF application however whenever I attempt to call any function from SimpleCommunication I get the following exception message:
据我所知,这个C ++ dll是作为C ++/CX构建的(因为它在编译器中使用/ZW标志),因此它包含非托管代码,所以我假设我需要一些可以从我的托管wpf应用程序调用这些函数的包装程序,但是我不确定如何实现此目标的最佳方法是什么,真的很感谢任何帮助!
As far as I know this C++ dll is being built as a C++/CX (because its using the /ZW flag in the compiler) and because of this it contains unmanaged code, so I'm assuming I'll need some sort of wrapper to be able to call these functions from my managed wpf application but I'm not really sure what the best way to approach this would be, any help would be really appreciated!
推荐答案
尽管WPF应用程序可以使用系统提供的 WinRT类型(在Windows
名称空间中的那些类型),但不能使用自定义WinRT类型因为Windows不知道如何找到它们.解决方案是使用桌面桥将您的WPF应用程序添加到AppX包中,此时Windows能够找到并激活您的类型.
Although WPF applications can use system-provided WinRT types (those in the Windows
namespace), they cannot use custom WinRT types because Windows doesn't know how to locate them. The solution is to use the Desktop Bridge to add your WPF application to an AppX package, at which point Windows will be able to locate and activate your types.
(注意:您不必将应用提交到商店才能使用Desktop Bridge;这只是打包应用的一种机制).
(Note: you do not have to submit your app to the Store to use the Desktop Bridge; it's just a mechanism to package your app).
这篇关于在Win32 C#WPF应用程序中使用UWP C ++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!