首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linux上的.NET核心--编组结构

Linux上的.NET核心--编组结构
EN

Stack Overflow用户
提问于 2019-08-14 16:31:39
回答 1查看 831关注 0票数 1

我有一个从C++库调用一些函数的.NET核心控制台应用程序。我尝试调用的函数simpty在一些settings中执行,并将结果输出到result中。

C++:

代码语言:javascript
复制
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#:

代码语言:javascript
复制
[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)打印设置时,我在整型和双精度属性中得到了完全不同的值,并且在尝试访问char*属性时得到了一个分割错误。

我也试着从C++代码(Windows和Linux)调用这个库,它可以正常工作。据我所知,这是一个编组问题,但我自己不能准确指出它。我是一名C#开发人员,没有太多使用C++、PInvoke或Linux的经验。

我使用Windows10 (x64)和Ubuntu16.04 (x64)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-16 23:36:19

作为评论中的David Heffernan suggested,我添加了一个显式的ref关键字,它起作用了。

下面是之前我的函数的签名:

代码语言:javascript
复制
public static extern bool ProcessInput(Settings settings, out Result result);

在此之后:

代码语言:javascript
复制
public static extern bool ProcessInput(ref Settings settings, out Result result);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57490976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档