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

久未见 提交于 2019-12-11 02:07:09

问题


I'm attempting to use the SimpleCommunication C++ DLL that Microsoft in one of their UWP sample examples inside a Win32 C# WPF application.

https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/SimpleCommunication/common/MediaExtensions/Microsoft.Samples.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:

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'.

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!


回答1:


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.

(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).



来源:https://stackoverflow.com/questions/51786164/using-a-uwp-c-dll-in-win32-c-sharp-wpf-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!