C++Winrt winmd visible in C# UWP project but not C++Winrt Project

704 Views Asked by At

I have some structs and enums in IDL file inside c++/winrt WRC project.

This is so the data structures can be shared between C++ and C#.

Add Winmd to UWP C# project and types are visible ok

Then on trying winrt projects

  1. Winrt CoreApp Project builds fine after adding the winmd but never sees the types.

  2. Winrt Blank Project fails to compile in some xaml related files due to my types , simply as result of adding winmd.

But no such issues with C# UWP project. In C# uwp i can see my type and use it no issues.

cheers

1

There are 1 best solutions below

0
On

So if C# can consume the types implemented in the WinRT component, it sounds like you may not have included a header file. Did you include the header file for the Windows Rutime Component in your pch.h file?

Let's say you have an windows runtime projected type called Cat that's in a Windows Runtime Component called MyComponent, if the name of your .winmd file is "MyComponent.winmd", do the following:

  1. In your consuming project (your .exe, for example), add a reference to the .winmd file.

  2. Build the project (must do this!)

  3. Include a header for it in your pch.h file:

    #include "winrt/MyComponent.h" <-- don't forget!

Once that's done, you should be able to find Cat (don't forget the namespace scope resolution, etc.)

// some consuming .cpp file

#include "pch.h"

using namespace winrt;
using namespace Windows::Foundation;
using namespace MyComponent;    // <-- Add for convenience ...or not

App::App()
{
    Cat tabby();        // <-- Should work like a charm ;)
}