从C#复杂结构中调用C dll

时间:2017-07-07 22:21:26

标签: c# c pinvoke marshalling dllimport

我的c dll来自我们小组以外的地方。

#pragma pack(push, 8)
typedef struct
{
    int int1;
    int int2;
    int int3;
    int int4;
    int int5;
    int int6;

    int int_array1[250];
    int int_array2[250];    
    int int_array3[250];
    int int_array4[250];
    int int_array5[250];    
    int int_array6[250];
}my_structure
#pragma pack(pop)



extern "C" {  

 __declspec(dllexport) void set_my_structure(my_structure mystruct);
 __declspec(dllexport) my_structure get_my_structure();

}

我使用以下代码从C#调用dll。

[StructLayout(LayoutKind.Sequential, Pack = 8)]
public class MyStructure
{
    public int int1;
    public int int2;
    public int int3;
    public int int4;
    public int int5;
    public int int6;

    public int[] int_array1 = new int[250];
    public int[] int_array2  = new int[250];    
    public int[] int_array3 = new int[250];
    public int[] int_array4 = new int[250];
    public int[] int_array5 = new int[250]; 
    public int[] int_array6  = new int[250];
}

[DllImport(_dllName, EntryPoint = "set_my_structure", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern void SetMyStructure(MyStructure data);

[DllImport(_dllName, EntryPoint = "get_my_structure", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern MyStructure GetMyStructure();

然而,当我调用GetMyStructure方法时,代码抛出异常并停留在空档。怎么了?处理这个问题的最佳方法是什么?

0 个答案:

没有答案
豫ICP备18024241号-1