将c#结构数组传递给c ++

时间:2015-01-25 11:07:02

标签: c# c++ arrays structure

我在c ++ dll上工作,有任何问题! 我的头文件是这样的

struct ST_DevInfo
{
    EN_DevType de_type;        
    int screen_width;          
    int screen_height;         
    char dev_name[256];        
    char id[14];               
    char sboox_version[16];    
    char fpga_version[16];     
};

extern "C" __declspec(dllexport) int CB_GetDeviceList(ST_DevInfo* buff,int length);

和c ++代码

int CB_GetDeviceList(ST_DevInfo* buff,int length)
{
    buff = (ST_DevInfo *)malloc(sizeof(ST_DevInfo) * length);

    return GetDeviceList(buff, length);
}

现在我在c#中使用此功能,如此

[StructLayout(LayoutKind.Sequential)]
        struct ST_DevInfo
        {
            [MarshalAs(UnmanagedType.I4)]
            public EN_DevType de_type;
            [MarshalAs(UnmanagedType.I4)]
            public int screen_width;
            [MarshalAs(UnmanagedType.I4)]
            public int screen_height;
            [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 256)]
            public char[] dev_name;
            [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 14)]
            public char[]  id;
            [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 16)]
            public char[]  sboox_version;
            [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 16)]
            public char[]  fpga_version;
        };

[DllImport(dllName, EntryPoint = "CB_GetDeviceList", SetLastError = true, ExactSpelling = true,
            CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
        public static extern
            int CB_GetDeviceList([MarshalAs(UnmanagedType.LPArray)] ref ST_DevInfo[] buff, 
                                    int length);

最后我在我的程序中使用此功能

ST_DevInfo[] buff = new ST_DevInfo[dev_length];
int ret = BBIA.CB_GetDeviceList( ref buff, dev_length);

但是从 CB_GetDeviceList 检索后,我的buff变量已分配,但没有任何值(包含0x00)。我用c ++测试它,它工作正常! 我觉得这行有问题

  

buff =(ST_DevInfo *)malloc(sizeof(ST_DevInfo)* length);

1 个答案:

答案 0 :(得分:0)

在您的C#代码中,您执行此操作:

ST_DevInfo[] buff = new ST_DevInfo[dev_length];
int ret = BBIA.CB_GetDeviceList( ref buff, dev_length);

分配数组,并将其(通过双指针,因为你有ref)传递给C ++代码。

在你正在做的C ++代码中:

int CB_GetDeviceList(ST_DevInfo* buff,int length)
{
    buff = (ST_DevInfo *)malloc(sizeof(ST_DevInfo) * length);
    return GetDeviceList(buff, length);
}

采用数组(而不是双指针)并将该(本地)指针更改为指向某个新内存。因此C ++代码永远不会触及C#中的原始数组。

首先,完全删除malloc来电。然后将你的pinvoke改为:

[DllImport( ... )]
public static extern int CB_GetDeviceList( [In, Out] ST_DevInfo[] buff, int length );

如前所述,但没有ref。需要In, Out告诉编组人员您希望pinvoke调用修改数据。它们并非在每种情况下都是必需的,但在您的情况下,我并非100%确定,所以我保留它们以防万一。