C# 为什么这种封送在Mono中工作,但在MS中失败?

C# 为什么这种封送在Mono中工作,但在MS中失败?,c#,pinvoke,marshalling,C#,Pinvoke,Marshalling,我把这归结为一个简单的例子。我正在编写一些C#和C之间的互操作代码,在非托管端我有以下非常简单的结构: typedef struct { bool create_if_missing; fdb_custom_cmp_variable custom_cmp; } fdb_kvs_config; typedef int (*fdb_custom_cmp_variable)(void *a, size_t len_a,

我把这归结为一个简单的例子。我正在编写一些C#和C之间的互操作代码,在非托管端我有以下非常简单的结构:

typedef struct {
    bool create_if_missing;
    fdb_custom_cmp_variable custom_cmp;
} fdb_kvs_config;

typedef int (*fdb_custom_cmp_variable)(void *a, size_t len_a,
                                   void *b, size_t len_b);
public struct fdb_kvs_config
{
    [MarshalAs(UnmanagedType.I1)]
    public bool create_if_missing;

    [MarshalAs(UnmanagedType.FunctionPtr)]
    public IntPtr custom_cmp;
}
因此,我在托管端创建了以下内容:

typedef struct {
    bool create_if_missing;
    fdb_custom_cmp_variable custom_cmp;
} fdb_kvs_config;

typedef int (*fdb_custom_cmp_variable)(void *a, size_t len_a,
                                   void *b, size_t len_b);
public struct fdb_kvs_config
{
    [MarshalAs(UnmanagedType.I1)]
    public bool create_if_missing;

    [MarshalAs(UnmanagedType.FunctionPtr)]
    public IntPtr custom_cmp;
}
我想使用这个非托管函数

extern __declspec(dllexport)
fdb_kvs_config fdb_get_default_kvs_config(void);
所以我有一个等价物:

[DllImport("forestdb", CallingConvention=CallingConvention.Cdecl)]
public static extern fdb_kvs_config fdb_get_default_kvs_config();
但是,这会引发一个异常:

ForestDB.Test.exe中发生类型为“System.Runtime.InteropServices.MarshalDirectiveException”的未处理异常

附加信息:方法的类型签名与PInvoke不兼容

注意:我尝试过各种
MarshalAs
组合,但运气不佳。还要注意的是,我只负责事情的C#方面,C API是由其他人开发的,我无法控制它


如果这个简单的签名与p/Invoke不兼容,那到底是什么?对于奖励积分,为什么它在Mono操作系统X上工作?

你知道什么。事实证明,
marshallas
实际上是问题所在,这使得属性在Windows上几乎毫无用处。托管端上的结构的所有成员都必须是。这个限制显然不存在于Mun.< /P>我不知道你的要求和限制,但是尝试了这两个,我更喜欢写一个有缺陷的C++包装而不是使用pNoKek。