Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a native WinRT component from a managed project?

It's been said that you can create a C++ WinRT component that you can consume in C#. I have a simple test project - a C# XAML app, and a C++ project using just the basic WinRT Component project template. When I try to add a project reference in the C# XAML project, it says it cannot add the reference (not sure why). I can build the native WinRT component, and a .winmd file is generated, and I can add a reference to this .winmd file manually using the browse button in the add reference dialog. This allows me to access the methods in the native library, but when I build, it says it is using Platform.IDisposable, but that type is defined in an assembly that isn't referenced. I haven't seen any of the sessions demonstrate this scenario.

like image 904
Jeremy Bell Avatar asked Sep 20 '11 21:09

Jeremy Bell


3 Answers

Found the answer! Looks like it's a glitch, but it has a work-around: http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/1900bd21-8693-4127-800a-f35cfd5daac2

Basically, first you clean the WinRT component project, then you add the reference, then add a reference manually to platform.winmd located in \VC\bin then you should be good to go.

like image 125
Jeremy Bell Avatar answered Nov 06 '22 20:11

Jeremy Bell


The C++ WinRT assembly you are referencing probably uses objects that are implementing the IDisposable interface so you also have to reference the C++ assembly containing the interface.

like image 28
Ucodia Avatar answered Nov 06 '22 19:11

Ucodia


So I run into similar issues, after adding a reference to Microsoft.VCLibs, i wasn't able to deploy. However I noticed, that by default WinRT component is compiled to x86 code whereas the C# Metro style app targets ANY cpu, and this was a root cause in my case. Switching C# app to x86 resolved the problem.

like image 42
pwlodek Avatar answered Nov 06 '22 19:11

pwlodek