Search code examples
c++visual-studio-2015windows-10-universalwindows-10-mobilec++-cx

UWP C# app calling Windows Runtime Component written in C++


I am trying to create a Windows Runtime(WinRT) Component written in C++(c++-cx) which can be called from my UWP C# app. I followed this MSDN Tutorial

It all went fine. I am able to build & run the code/sample specified in the above link on both Windows-10 Desktop & Mobile
I then tried to create a even simpler WinRT Component. I followed the same steps as above tutorial. Created a Windows Runtime Component project (File->New->Project->Visual C++ ->Windows->Universal->Windows Runtime Component(Universal Windows))
Now my class has only one function, getNum which basically returns a uint16
This is my Class header file

#pragma once
#include <collection.h>

namespace WRT_sample
{
    public ref class Class1 sealed
    {
        public:
              Class1();
              uint16 getNum();
    };
}


This is the corresponding cpp file:

#include "Class1.h"


using namespace WRT_sample;
using namespace Platform;

Class1::Class1()
{
}

uint16 Class1::getNum()
{
     return 100;
}


I am using the same class names(Class1.h & Class1.cpp) which VS-2015 provides when I create a new WinRT project.
This is how I make a call to WinRT class from my C# code:

namespace CS_WRT_sample
{
    public sealed partial class MainPage : Page
    {
       public MainPage()
       {
            this.InitializeComponent();
            initWRT();
       }

       void initWRT()
       {
            try
            {
                var nativeObj = new Class1();
                var num = nativeObj.getNum();
                this.Result2.Text = "Num from Cpp: " + num.ToString();
                Debug.WriteLine("Num from Cpp: " + num);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                this.Result3.Text = ex.Message;
            }
        }
    }
}


I am able to build & run my code on Desktop(x86/x64 Local Machine), but when I try to run on Mobile(x86 Emulator or ARM device), it fails with the following error:

Exception thrown: 'System.IO.FileNotFoundException' in CS_WRT_sample.exe
The specified module could not be found. (Exception from HRESULT: 0x8007007E)**


This is the stack trace which I see:

System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)
at WRT_sample.Class1..ctor()
at CS_WRT_sample.MainPage.initWRT()


My VS-2015 details:

MS Visual Studio Professional 2015
Version 14.0.25123.00 Update 2
MS .NET Framework
Version 4.6.01038

I am not sure, why I am hitting the issue, as I followed the same steps mentioned in the MSDN tutorial. Any help will be highly appreciated.


Solution

  • You will run into this if you use a direct reference to a C++ winmd, instead of a P2P reference. This is easy to check in your .csproj:

    For example, your .csproj should not have this line: ..\Debug\Abc_def\Abc_Def.winmd

    Instead, consider using something like: {1ee77824-eaa8-40f1-9b56-6f8ffa44e727} Abc_Def