gpt4 book ai didi

c++ - UWP C# 应用程序调用用 C++ 编写的 Windows 运行时组件

转载 作者:行者123 更新时间:2023-11-30 05:24:35 25 4
gpt4 key购买 nike

我正在尝试创建一个用 C++(c++-cx) 编写的 Windows 运行时 (WinRT) 组件,它可以从我的 UWP C# 应用程序中调用。我跟着这个MSDN Tutorial

一切顺利。我能够在 Windows-10 桌面和移动设备上构建和运行上述链接中指定的代码/示例
然后我尝试创建一个更简单的 WinRT 组件。我按照与上述教程相同的步骤操作。创建了一个 Windows 运行时组件项目 (File->New->Project->Visual C++ ->Windows->Universal->Windows Runtime Component(Universal Windows))
现在我的类只有一个函数,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;
}


我在创建新的 WinRT 项目时使用 VS-2015 提供的相同类名(Class1.h 和 Class1.cpp)。
这是我从 C# 代码调用 WinRT 类的方式:

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 教程中提到的相同步骤。任何帮助将不胜感激。

最佳答案

如果您使用对 C++ winmd 的直接引用而不是 P2P 引用,就会遇到这种情况。这很容易检查您的 .csproj:

例如,您的 .csproj 不应包含此行: ..\Debug\Abc_def\Abc_Def.winmd

相反,请考虑使用如下内容: {1ee77824-eaa8-40f1-9b56-6f8ffa44e727} Abc_Def

关于c++ - UWP C# 应用程序调用用 C++ 编写的 Windows 运行时组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38626173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com