如何将C++ .dll集成到C# Tizen NUI应用中?
问题描述
我正在用Visual Studio开发常规C# Tizen NUI应用,基于模板的初始代码在TV模拟器中运行正常。我制作了一个C++ DLL(包含头文件Header.h和实现逻辑的.cpp文件),生成路径为...\TizenDotNetApp\x64\Debug\CppLibrary.dll。将该DLL添加到Tizen应用解决方案并设置属性为“始终复制”后,修改C#代码通过[DllImport]调用其中的add函数,但运行时提示找不到DLL文件。不过该DLL在普通C#控制台应用中可正常调用并执行函数。
初始C#代码
//Main.cs using System; using Tizen.NUI; using Tizen.NUI.BaseComponents; namespace TizenDotNetApp { class Program : NUIApplication { protected override void OnCreate() { base.OnCreate(); Initialize(); } void Initialize() { Window.Instance.KeyEvent += OnKeyEvent; TextLabel text = new TextLabel("Hello Tizen NUI World"); text.HorizontalAlignment = HorizontalAlignment.Center; text.VerticalAlignment = VerticalAlignment.Center; text.TextColor = Color.Blue; text.PointSize = 12.0f; text.HeightResizePolicy = ResizePolicyType.FillToParent; text.WidthResizePolicy = ResizePolicyType.FillToParent; Window.Instance.GetDefaultLayer().Add(text); Animation animation = new Animation(2000); animation.AnimateTo(text, "Orientation", new Rotation(new Radian(new Degree(180.0f)), PositionAxis.X), 0, 500); animation.AnimateTo(text, "Orientation", new Rotation(new Radian(new Degree(0.0f)), PositionAxis.X), 500, 1000); animation.Looping = true; animation.Play(); } public void OnKeyEvent(object sender, Window.KeyEventArgs e) { if (e.Key.State == Key.StateType.Down && (e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "Escape")) { Exit(); } } static void Main(string[] args) { var app = new Program(); app.Run(args); } } }
C++头文件代码
//Header.h #pragma once #ifdef CPPPROGRAM_EXPORTS #define CPPPROGRAM_API _declspec(dllexport) #else #define CPPPROGRAM_API _declspec(dllimport) #endif // 简单的问候函数 extern "C" CPPPROGRAM_API void message(); // 两数相加 extern "C" CPPPROGRAM_API int add(int a, int b); // 向传入的参数打招呼 extern "C" CPPPROGRAM_API void greet(char* name);
C++实现代码
#include "pch.h" #include "Header.h" #include <stdio.h> // 测试基础printf函数 void message() { printf("你好,来自C++!\n"); } // 测试基础参数传递 int add(int a, int b) { return a + b; } // 测试稍复杂的数组传递(暂不考虑结构体和对象) void greet(char* name) { printf("你好,%s!", name); }
修改后的C#代码
using System; using Tizen.NUI; using Tizen.NUI.BaseComponents; using System.Runtime.InteropServices; namespace TizenDotNetApp { class Program : NUIApplication { [DllImport("CppLibrary.dll")] static extern int add(int a, int b); // <-- 调用该函数 protected override void OnCreate() { base.OnCreate(); Initialize(); } void Initialize() { Window.Instance.KeyEvent += OnKeyEvent; int result = add(5, 8); // <-- 此处执行简单加法 TextLabel text = new TextLabel("Hello Tizen NUI World"); text.HorizontalAlignment = HorizontalAlignment.Center; text.VerticalAlignment = VerticalAlignment.Center; text.TextColor = Color.Blue; text.PointSize = 12.0f; text.HeightResizePolicy = ResizePolicyType.FillToParent; text.WidthResizePolicy = ResizePolicyType.FillToParent; Window.Instance.GetDefaultLayer().Add(text); Animation animation = new Animation(2000); animation.AnimateTo(text, "Orientation", new Rotation(new Radian(new Degree(180.0f)), PositionAxis.X), 0, 500); animation.AnimateTo(text, "Orientation", new Rotation(new Radian(new Degree(0.0f)), PositionAxis.X), 500, 1000); animation.Looping = true; animation.Play(); } public void OnKeyEvent(object sender, Window.KeyEventArgs e) { if (e.Key.State == Key.StateType.Down && (e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "Escape")) { Exit(); } } static void Main(string[] args) { var app = new Program(); app.Run(args); } } }
解决方案
- 确认DLL复制路径正确:Tizen应用的输出目录和普通控制台不同,右键C#项目→属性→生成→输出路径,确保DLL被复制到该路径下(比如
bin/Tizen/x64/Debug)。可以手动将CppLibrary.dll复制到这个目录,或者在C++项目属性里设置输出目录为Tizen应用的x64 Debug目录。 - 匹配架构:确保C++ DLL编译为x64架构(和TV模拟器一致),不要用x86。在C++项目属性→配置管理器→平台选择x64。
- 修改DllImport路径:如果自动复制失效,可以在
[DllImport]里指定DLL的绝对路径,或者在代码启动时添加DLL搜索路径:[DllImport("kernel32.dll", SetLastError = true)] static extern bool SetDllDirectory(string lpPathName); // 在OnCreate开头添加 SetDllDirectory(@"你的DLL所在的绝对路径"); - 检查Tizen打包配置:打开Tizen项目的
.tizenproj文件,确认是否包含了DLL的复制规则,添加以下内容(如果没有):<ItemGroup> <Content Include="CppLibrary.dll"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content> </ItemGroup>
内容的提问来源于stack exchange,提问作者enricosebastian
相关产品推荐
相关产品推荐

