我正在尝试创建一个用(c++-cx)编写的WinRT Windows(WinRT)组件,它可以从我的UWP C#应用程序中调用。我跟踪了这个MSDN教程
一切都很顺利。我能够在Windows-10桌面和移动平台上构建并运行上述链接中指定的代码/示例
然后,我尝试创建一个更简单的WinRT组件。我遵循了与上述教程相同的步骤。创建了一个Windows组件项目(File->New-> project ->VisualC++->Windows->Windows组件(Universal))
现在我的类只有一个函数,getNum,它基本上返回一个uint16。
这是我的类头文件
#pragma once
#include <collection.h>
namespace WRT_sample
{
public ref class Class1 sealed
{
public:
Class1();
uint16 getNum();
};
}这是对应的cpp文件:
#include "Class1.h"
using namespace WRT_sample;
using namespace Platform;
Class1::Class1()
{
}
uint16 Class1::getNum()
{
return 100;
}我使用的类名(Class1.h& Class1.cpp)与创建新的WinRT项目时提供的VS-2015类名相同。
下面是我如何从我的WinRT代码中调用C#类:
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;
}
}
}
}我能够在桌面(x86/x64本地机器)上构建和运行我的代码,但是当我尝试在移动(x86仿真程序或ARM设备)上运行时,它会失败,但会出现以下错误:
引发的异常: CS_WRT_sample.exe中的“System.IO.FileNotFoundException” 无法找到指定的模块。(HRESULT的例外: 0x8007007E)**
这是我看到的堆栈跟踪:
哈佛大学( System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD) at WRT_sample.Class1..ctor() at CS_WRT_sample.MainPage.initWRT()
我的VS-2015详细信息:
MS Visual Studio Professional 2015版本14.0.25123.00更新2 MS .NET框架版本4.6.01038
我不知道为什么我会碰到这个问题,因为我遵循了MSDN教程中提到的相同步骤。任何帮助都将不胜感激。
发布于 2016-08-03 01:50:34
如果使用对C++ winmd的直接引用而不是P2P引用,您将遇到这种情况。这很容易在您的.csproj中签入:
例如,您的.csproj不应该有以下一行:..\Debug\Abc_def\Abc_Def.winmd
相反,考虑使用类似于:{1ee77824-ea8-40f1-9b56-6f8ffa44e727} Abc_Def。
https://stackoverflow.com/questions/38626173
复制相似问题