1
votes

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.

1
I tried in my machine it works well in both local machine and x86 Emulator, is there any special steps you did? - 2power10
Hi @2power10, Thanks for the response. That is strange. I copied my project to my friend's machine & ran it on his machine. He too was seeing the same behavior. No, I am not doing anything special. As I mentioned, I followed the same steps as listed in MSDN tutorial. I compared my C/C++ & Linker Command Line args with the MSDN tutorial's & both are the same. Basically I am just using Windows Runtime Component project template, & not modifying any settings/flags. I am not sure how to debug this issue now. - Ganesh kudva
I made a demo, but couldn't reproduce your problem. Could you share a basic sample that can reproduce this problem? - Elvis Xia - MSFT
Hi @ElvisXia-MSFT, Thanks for the response. I have uploaded my entire sample here: (dropbox.com/sh/dhvjyzkkixproro/AAD2GLeXYOAZ39w8kIqiBNHLa?dl=0). You can download and run it. You can see that if you try to run on either Debug/Release x86 mode on emulator, I get the error. Again I tried this sample on my friend's machine, he has VS Community Edition & he is seeing the same error as well. 2power10, you are welcome to try this sample as well. - Ganesh kudva
could you provide a more complete stack trace? - Yuchen

1 Answers

1
votes

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