How to reference a native WinRT component from a managed project?
Asked Answered
I

3

10

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.

Infusive answered 20/9, 2011 at 21:31 Comment(0)
I
8

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.

Infusive answered 21/9, 2011 at 14:41 Comment(0)
R
2

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.

Ringnecked answered 20/9, 2011 at 22:14 Comment(4)
What C++ assembly? Supposedly, all the core assemblies for WinRT are already referenced automatically. There are no additional standard assemblies in the reference dialog to select.Infusive
@Jeremy It looks like you need to add a reference to Microsoft.VCLibs to .NET app for it to resolve Platform.IDisposable (on a side note, I would have expected it to be projected as System.IDisposable, but it doesn't happen...). However, adding it as reference seems to cause problems when deploying. Probably a Dev Preview quirk.Apothegm
Yeah, I noticed Microsoft.VCLibs and tried it, and it looks like that's where it's defined, but I also am getting errors when deploying. Will have to research the packaging stuff - perhaps VCLibs has to be included in the package to work? Anyone able to get this to work?Infusive
Maybe you should post a new question because you will probably get a reply as the original question kind of changed now.Ringnecked
S
2

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.

Sulfur answered 25/9, 2011 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.