我想创建一个Windows运行时(WinRT)用C++(C++ - cx)编写的组件,可以从我的UWP C#应用程序调用。我跟着这个MSDN Tutorial
这一切都很顺利。我能够构建&在Windows-10桌面上运行上述链接中指定的代码/示例& Mobile
然后我尝试创建一个更简单的WinRT组件。我遵循了上述教程中的相同步骤。创建一个Windows运行时组件项目(文件 - >新建 - >项目 - > Visual C++ - > Windows->通用 - > Windows运行时组件(通用Windows))
现在我的班级只有一个功能,getNum基本上返回一个UINT16
这是我的类的头文件UWP C#应用程序调用使用C++编写的Windows运行时组件
#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;
}
我使用VS-2015在创建新的WinRT项目时提供的相同类名(Class1.h & Class1.cpp)。
这是我作出的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装置),它失败,错误如下:
Exception thrown: 'System.IO.FileNotFoundException' in CS_WRT_sample.exe
The specified module could not be found. (Exception from 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
Version 14.0.25123.00 Update 2
MS .NET Framework
Version 4.6.01038
我不知道,为什么我打的问题,因为我也跟着在MSDN教程中提到的相同步骤。 任何帮助将不胜感激。
我在我的机器上试了一下效果很好本地机和x86模拟器,是有你没有任何特殊的步骤? – 2power10
Hi @ 2power10,谢谢你的回应。这很奇怪。我将我的项目复制到我朋友的机器上,并在他的机器上运行。他也看到了同样的行为。不,我没有做任何特别的事情。正如我所提到的,我遵循了与MSDN教程中列出的步骤相同的步骤。我将我的C/C++和链接器命令行参数与MSDN教程的参数相比较。基本上我只是使用Windows运行时组件项目模板,而不是修改任何设置/标志。我不知道现在如何调试这个问题。 –
我做了一个演示,但无法重现您的问题。你能分享一个可以重现这个问题的基本样本吗? –