c# - 如何将带有字符串的结构从 C++ 传递到 C#
问题描述
我有以下 C++ 代码
#ifdef EXPORT_DLL
#define CfgAPI __declspec(dllexport)
#else
#define CfgAPI
#endif
struct WrkPaths
{
public:
char *WrkDir;
char *URL1;
char *URL2;
char *URL3;
};
CfgAPI int getURLFromDir(WrkPaths * pathCfg){
pathCfg->WrkDir = (char*)malloc( 5 * sizeof(char) );
strcpy( pathCfg->WrkDir , "Test");
return 0;
}
对应的C#代码
using System;
using System.Runtime.InteropServices;
using System.Text;
// namespace declaration
namespace HelloWorldApp {
// Class declaration
class Geeks {
[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct WrkPaths {
[MarshalAs (UnmanagedType.LPStr, SizeConst = 255)]
public string WrkDir;
[MarshalAs (UnmanagedType.LPStr, SizeConst = 255)]
public string URL1;
[MarshalAs (UnmanagedType.LPStr, SizeConst = 255)]
public string URL2;
[MarshalAs (UnmanagedType.LPStr, SizeConst = 255)]
public string URL3;
}
[DllImport ("CfgAPI.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int getRepoURLFromDir (
ref WrkPaths wPath
);
// Main Method
static void Main (string[] args) {
// statement
// printing Hello World!
Console.WriteLine ("Hello World!");
WrkPaths wPath = new WrkPaths();
int a = getRepoURLFromDir(ref wPath);
Console.WriteLine (wPath.WrkDir.ToString ());
// To prevents the screen from
// running and closing quickly
Console.ReadKey ();
}
}
}
仅在屏幕上Hello world
出现。如何将数据从 C/C++ DLL 传输到 C#?
解决方案
推荐阅读
- c++ - OpenGL围绕自身而不是原点(0,0,0)旋转对象(一组立方体)
- c++ - c++模板结果类型
- r - 如何使用 tmap 为每个坐标赋予另一种颜色
- java - 在 Java8 中清理数据列表
- reactjs - 通用类型'异步
' 需要使用 React Select 的 AsyncSelect 的 1 个类型参数 - java - 为什么我的 firestore 函数返回 null 给我的应用程序?
- python - 如何在 Python 中自动读取多个测试用例,其中测试用例存储在记事本文件中?
- mysql - MySQL:将字符串与查询结果进行比较
- vue.js - 无法将选中的值从自定义复选框组件传递给父组件
- javascript - 如何在 Owl Carousel 中只显示单个项目?