Cotta
Cotta

Reputation: 160

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

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!

enter image description here

Upvotes: 2

Views: 1020

Answers (1)

Peter Torr
Peter Torr

Reputation: 12019

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

Upvotes: 3

Related Questions