c# - 如何使用显式对齐(非托管 dll)将结构从 C 编码到 C#

标签 c# c dll alignment unmanaged

我必须在我的 C# 应用程序中使用非托管 dll(用 C 编写)。 在这个 dll 中我有一个结构:

typedef struct {
void (*Func1)(void*, int*);
void (*Func2)(void*, int*);
} myStructure;

并且,我有一个使用这个结构作为 [in/out] 参数的函数:

void functionInterface(myStructure* pToStruct);

在 C# 中,我将该结构翻译为:

[StructLayout(LayoutKind.Sequential)]
public unsafe struct myStructure
{
    //defining delegate instances        
    public func1 Func1;
    public func2 Func2;
              ...
    //defining delegate types
    public delegate void Func1(void* l,int* data);
    public delegate void Func2(void* l,int* data);
              ...
}

和 C# 中的函数:

[DllImport("lib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void functionInterface(ref myStructure pToStruct);

当调用这个 functionInterface() 时,在运行时,异常会上升。比如“在...地址...写入的访问冲突”或 “在……地址……阅读的访问冲突”

我找不到原因,我认为问题可能在于翻译结构中成员的正确对齐,或堆栈对齐,但是,我不知道如何正确地做到这一点。或者问题可能不同,性质不同,我无法检测到,希望你们能帮助我。

最佳答案

这非常有效...正如我喜欢说的:你打破它,你修复它...

C# 端:

[StructLayout(LayoutKind.Sequential)]
public struct myStructure
{
    public Fn1 Func1;
    public Fn2 Func2;

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void Fn1(IntPtr p1, ref int p2);

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void Fn2(IntPtr p1, ref int p2);
}

[DllImport("lib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void functionInterface(out myStructure pToStruct);

然后:

myStructure myStructure;
functionInterface(out myStructure);

int num1 = 100, num2 = 200;
myStructure.Func1((IntPtr)0x1000, ref num1);
Console.WriteLine("C#-side: out Func1: {0}", num1);
myStructure.Func2((IntPtr)0x2000, ref num2);
Console.WriteLine("C#-side: out Func2: {0}", num2);

C端:(我把所有的代码都放在一个.c/.cpp文件里,没有header)

#ifdef __cplusplus
extern "C"
{
#endif
    typedef struct 
    {
        void (*Func1)(void*, int*);
        void (*Func2)(void*, int*);
    } myStructure;

    void Fn1(void* p1, int* p2)
    {
        printf("C-side: Fn1: %p, %d - ", p1, *p2);
        *p2 += 1;
    }

    void Fn2(void* p1, int* p2)
    {
        printf("C-side: Fn2: %p, %d - ", p1, *p2);
        *p2 += 1;
    }

    __declspec(dllexport) void functionInterface(myStructure* pToStruct)
    {
        pToStruct->Func1 = Fn1;
        pToStruct->Func2 = Fn2;
    }
#ifdef __cplusplus
}
#endif

关于c# - 如何使用显式对齐(非托管 dll)将结构从 C 编码到 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50471810/

相关文章:

C#:简单的TCP服务器问题

不同布局的数组可以别名吗?

C:需要将二维数组的某些部分写入新的二维数组

c++ - 来自 C++ 源的 C-DLL

delphi - Delphi 应用程序的插件系统 - bpl 与 dll?

c# - 执行长任务而不影响 Unity 帧速率的正确方法

c# - 在单个项目中使用多个平台

c# - ViewModel 自动具有与通用模型相同的属性

c - 方括号在 C 中如何工作?

visual-c++ - 查询 : Integrating libsndfile with Visual Studio 2010 C++. 错误 : libsndfile. dll 找不到