C# 通过结构体二维数组与C++通信

C# 通过结构体二维数组与C++通信
C#:
定义结构体:

public struct SendMsg
{
    public int nHeadId;
    public int nLen;
    public int nCmdId;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50, ArraySubType = UnmanagedType.Struct)]
    public UDPMsg[] szUDPMsg;
}

public struct UDPMsg
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 200)]
    public string szUDPcmd;
}

C++ 定义结构体:

typedef struct
{
	int		nHeadId;			
	int		nLen;				
	int		nCmdId;				
	char	szUdpMsg[50][200];  
}*UdpSendMsg;

C#通过赋值:

            SendMsg sMsg = new SendMsg();
            sMsg.nCmdId = 0xd1;
            sMsg.nHeadId = 0xEF;
            sMsg.nLen = 4;
            sMsg.szUDPMsg = new UDPMsg[2];
            sMsg.szUDPMsg[0] = new UDPMsg();
            sMsg.szUDPMsg[0].szUDPcmd = "测试1";
            sMsg.szUDPMsg[1] = new UDPMsg();
            sMsg.szUDPMsg[1].szUDPcmd = "测试2";

C++ 接收数据
char* recv_buffer = new char[32768];

		UdpSendMsg sendMsg;
		sendMsg = (UdpSendMsg)&recv_buffer[0];

你可能感兴趣的:(C++,MFC编程)