如何使用显式对齐(非托管dll)

时间:2018-05-22 15:49:30

标签: 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);

在调用此函数接口()时,在运行时,异常正在上升。类似于“访问冲突以写入......地址......”或 “访问违规以阅读......地址......”

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

1 个答案:

答案 0 :(得分:1)

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

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文件中,没有标题)

#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