Loading

C++ 结构体数组回调C#代码,c#数组只有一条

C# 方法

[UnmanagedFunctionPointerAttribute(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public delegate void RecvSwitchStatusCallback(string no, int noLength, IntPtr cpointer, int dataLength);

public static void RequestRecvSwitchStatusCallback(string no, int noLength, IntPtr cpointer, int dataLength) {
IntPtr pointer = cpointer;
MyStruct[] listMyStruct = new MyStruct[dataLength];
int structSize = Marshal.SizeOf(typeof(MyStruct));
for (int i = 0; i < dataLength; ++i) {
var data = new IntPtr(pointer.ToInt64() + structSize * i);
MyStruct currentStruct = (MyStruct) Marshal.PtrToStructure(data, typeof(MyStruct));
listCommonLightStatus[i] = currentStruct;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct MyStruct {
public byte id;
public int code;
};

[UnmanagedFunctionPointerAttribute(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public delegate void RecvSwitchStatusCallback(string no, int noLength, IntPtr cpointer, int dataLength);

posted @ 2016-12-13 16:05  yuefengkai  阅读(531)  评论(0编辑  收藏  举报
>