我正在尝试将结构数组传递给C ++ DLL并遇到问题。我一直试图弄清楚好几天但没有用。我可以从C ++中获得良好的数据,当我尝试使用.NET获取struct数组时,我遇到了问题。
C ++原型是:
static __declspec(dllexport) int SocketAPI::api_get_data(int iSize, buffer_node *data);
在我的C#代码中,我将函数定义为:
[DllImport("SocketAPI.dll")]
static extern int api_get_data(int iSize, buffer_node[] data);
我的Struct是buffer_node,定义为:
[StructLayout(LayoutKind.Sequential, Size = 23), Serializable]
public struct header
{
// HEADER
public UInt16 h_type;
public UInt32 frame_num;
public UInt16 count_1pps;
public byte data_options;
public byte project_type;
public byte tile_num;
public byte tile_set;
public byte total_rows;
public byte total_cols;
public byte num_rows;
public byte num_cols;
public byte first_row;
public byte first_col;
public UInt16 num_sensors;
public UInt16 num_data_bytes;
public byte h_checksum;
}
[StructLayout(LayoutKind.Sequential, Size = 25), Serializable]
public struct footer
{
// FOOTER
public UInt16 f_type;
public byte ts_len;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public byte[] ts_array;
public byte frame_status;
public byte f_checksum;
}
[StructLayout(LayoutKind.Sequential, Size = 51), Serializable]
public struct buffer_node
{
// HEADER
public header data_header;
// DATA
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] data;
// FOOTER
public footer data_footer;
}
如果尝试以下导入:
// See buffer, but everything is 0 - ie. not being populated
unsafe static extern int api_get_data(int iSize, buffer_node[] data);
// fails somewhere in the API
static extern int api_get_data(int iSize, out buffer_node[] data);
static extern int api_get_data(int iSize, ref buffer_node[] data);
我的C#GetData程序目前看起来像这样:
// Get current data size
int iSize = api_is_data_available();
// Create buffer to hold the data
buffer_node[] buf_data = new buffer_node[iSize];
for (int i = 0; i < iSize; i++)
{
buf_data[i].data = new byte[3];
buf_data[i].data_footer.ts_array = new byte[20];
}
// Get the data
//int iStructSize = Marshal.SizeOf(buf_data[0]);
//IntPtr bufNodePtr = IntPtr.Zero;
//IntPtr buffer = Marshal.AllocHGlobal(iStructSize * iSize);
//api_get_data(iSize, buffer);
//for (int i = 0; i < iSize; i++)
//{
// IntPtr ptr = new IntPtr(buffer.ToInt64() + iStructSize * i);
// buf_data[i] = (buffer_node)Marshal.PtrToStructure(ptr, typeof(buffer_node));
//}
//api_get_data(iSize, buf_data); // See buffer, but everything is 0 - ie. not being populated
// api_get_data(iSize, out buf_data); // fails no error
api_get_data(iSize, ref buf_data); // fails no error
// api_get_data(iSize, ref buf_data);
// Print the data
for (int i = 0; i < iSize; i++)
{
StringBuilder sb = new StringBuilder();
sb.Append("Tile Number: " + Convert.ToString(buf_data[i].data_header.tile_num));
AppendTextBox(sb.ToString());
}
再次感谢你。任何帮助都将非常感激,因为我认为这将是一个简单的任务,真的让我陷入困境!
答案 0 :(得分:4)
您必须在[DllImport]属性中使用CallingConvention属性。默认是StdCall,你需要Cdecl,因为C ++声明没有使用__stdcall。
答案 1 :(得分:1)
如果int iSize
是元素中数组的大小(例如data.Length),请尝试使用MarshallAs.SizeParamIndex。这将告诉编组人员数据中应该有多少元素。
[DllImport("SocketAPI.dll")]
static extern int api_get_data(int iSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] buffer_node[] data);
有关数组如何mashalled at MSDN的更多信息。
答案 2 :(得分:0)
具有ref
和out
的那些不起作用,因为它们传递指向引用的指针,而不是指向第一个元素的指针。
编辑1:我刚注意到,你不能像现在这样做传递数组 - 结构体内的托管数组通常不会按照你想要的方式进行编组。当我想到一个时,我会写一个解决方案,但我认为你将不得不手工编组。
编辑2:如果您能够使用不安全的代码,那么这应解决问题:更改从ByValArray
到fixed byte[]
的所有内容,然后使用此代码:
[StructLayout(LayoutKind.Sequential, Size = 23), Serializable]
public struct header
{
// HEADER
public UInt16 h_type;
public UInt32 frame_num;
public UInt16 count_1pps;
public byte data_options;
public byte project_type;
public byte tile_num;
public byte tile_set;
public byte total_rows;
public byte total_cols;
public byte num_rows;
public byte num_cols;
public byte first_row;
public byte first_col;
public UInt16 num_sensors;
public UInt16 num_data_bytes;
public byte h_checksum;
}
[StructLayout(LayoutKind.Sequential, Size = 25), Serializable]
public struct footer
{
// FOOTER
public UInt16 f_type;
public byte ts_len;
public unsafe fixed byte ts_array[20];
public byte frame_status;
public byte f_checksum;
}
[StructLayout(LayoutKind.Sequential, Size = 51), Serializable]
public struct buffer_node
{
// HEADER
public header data_header;
// DATA
public unsafe fixed byte data[3];
// FOOTER
public footer data_footer;
}
unsafe static extern int api_get_data(int iSize, buffer_node* pData);
//...
// Get current data size
int iSize = api_is_data_available();
// Create buffer to hold the data
buffer_node[] buf_data = new buffer_node[iSize];
unsafe
{
fixed (buffer_node* pBufData = buf_data)
{
api_get_data(iSize, pBufData); // fails no error
}
}
(您必须将声明更改为指向元素的指针。)
编辑3:我刚注意到......你试过像这样说[Out]
吗?
[DllImport("SocketAPI.dll")]
static extern int api_get_data(int iSize, [Out] buffer_node[] data);
这可能只是起作用,没有做我上面所做的痛苦。
旁注:除非您也更改对齐方式,否则说Size = 23
将不会执行任何操作,因为结构将被填充以达到默认对齐方式。
答案 3 :(得分:0)
我遇到了同样的问题,必须将一个空数组从C#传递给一个dll中的C函数。然后该函数将返回指向填充结构的数组的第一个元素的指针。
这是我声明外部函数的方式:
[DllImport(LIB_NAME, CallingConvention = CallingConvention.StdCall, EntryPoint = "getData")]
unsafe extern void getData(IntPtr data, ref UInt32 dataLen);
有问题的结构:
[StructLayout(LayoutKind.Sequential)]
internal struct DataC
{
internal UInt16 xRes, yRes;
internal fixed float rot[9];
}
这就是我调用函数以及如何将IntPtr转换为结构的方法:
unsafe
{
UInt32 dataLen = 10;
IntPtr dataPtr = Marshal.AllocHGlobal((int)dataLen * Marshal.SizeOf(typeof(DataC)));
getData(dataPtr, ref dataLen);
// check here for null, obviously
DataC* dataArr = (DataC*)dataPtr;
for (int i = 0; i < dataLen; i++)
{
DataC data = dataArr[i];
// I fill a managed class/struct with the unmanaged data and add it to a List or whatever
result.Add(new Data(data->xRes, data->yRes, data->rot[0], ...));
}
// As we have the data in managed memory now, we free the allocated space
Marshal.FreeHGlobal(dataPtr);
}
答案 4 :(得分:0)
对buffer_node []数据参数使用[In,Out]属性:
[DllImport("SocketAPI.dll")]
static extern int api_get_data(int iSize, [In, Out] buffer_node[] data);