c++ - Linux 上的 .NET Core - 元帅结构
问题描述
我有一个 .NET Core 控制台应用程序,它调用 C++ 库中的一些函数。我试图调用 simpty 的函数,settings
并将结果输出到result
.
C++:
struct settings
{
char* input_path;
char* first_file;
char* second_file;
char* mask;
char* log_path;
int count_accepted;
double confidence;
char* device;
};
struct result
{
int count;
foo* foos;
bool is_preprocessed;
double duration;
};
bool process_input(const settings& settings, result* result);
C#:
[StructLayout(LayoutKind.Sequential)]
public struct Settings
{
[MarshalAs(UnmanagedType.LPStr)]
public string input_path;
[MarshalAs(UnmanagedType.LPStr)]
public string first_file;
[MarshalAs(UnmanagedType.LPStr)]
public string second_file;
[MarshalAs(UnmanagedType.LPStr)]
public string mask;
[MarshalAs(UnmanagedType.LPStr)]
public string log_path;
[MarshalAs(UnmanagedType.I4)]
public int count_accepted;
[MarshalAs(UnmanagedType.R8)]
public double confidence;
[MarshalAs(UnmanagedType.LPStr)]
public string device;
}
[StructLayout(LayoutKind.Sequential)]
public struct Result
{
[MarshalAs(UnmanagedType.I4)]
public int count;
[MarshalAs(UnmanagedType.SysInt)]
public IntPtr foos;
[MarshalAs(UnmanagedType.I1)]
public bool is_preprocessed;
[MarshalAs(UnmanagedType.R8)]
public double duration;
}
[DllImport("myLib", EntryPoint = "process_input", CallingConvention = CallingConvention.Cdecl)]
[return:MarshalAs(UnmanagedType.I1)]
public static extern bool ProcessInput(Settings settings, out Result result);
这一切在 Windows 上运行良好,但在 Linux 上不起作用。当我在 C++ 端(从process_input
)打印设置时,我在 int 和 double 属性中得到完全不同的值,并且在尝试访问 char* 属性时会出现分段错误。
我还尝试从 C++ 代码(Windows 和 Linux)调用这个库,它按预期工作。据我了解,这是一个编组问题,但我自己无法确定。我是一名 C# 开发人员,对 C++ 或 PInvoke 或 Linux 没有太多经验。
我使用 Windows 10 (x64) 和 Ubuntu 16.04 (x64)。
解决方案
正如David Heffernan在评论中 建议的那样,我添加了一个明确的ref
关键字并且它起作用了。
这是我之前函数的签名:
public static extern bool ProcessInput(Settings settings, out Result result);
之后:
public static extern bool ProcessInput(ref Settings settings, out Result result);
推荐阅读
- r - lapply over lapply(或其他清理时间序列数据的方式)
- sql - 根据条件在不同的列中选择
- vb.net - 我正在向 word 文档中添加文本并且需要包含下标
- android - 如何从 DAO 查询中返回单个 int 值?
- javascript - 从 React 组件中的道具设置初始状态
- bash - 使用“$@”时脚本无法识别 Bash“-n”参数
- c# - StringFormat.Trimming 改变文本的垂直位置
- javascript - 在角度服务构造函数中订阅会阻止我的拖放功能
- java - 无法在 Selenium Java 中找到图像 xpath
- regex - 正则表达式获取字符串的开始和结束匹配