如何将c++的struct表示为C#,并把 bytes? Marshal的方式仅支持简单类型,不支持char A[20]的形式。
tyzyx 2007-08-11 11:49:16 C++的结构体如下,需要用C#表示这个结构体并转为byte数组用TcpClient发送。
struct test
{
unsigned long ID;
char A[20];
char B[20];
};
表示为C#为,表示的对否?如果不对,应如何表示?
public struct test
{
public uint ID;
public char[] A;
public char[] B;
}
在网上找到将上面结构体转为byte的方法如下
test temp ;
temp.ID =100;
temp.A = new char[20];
temp.B = new char[20];
int rawsize = 45;// Marshal.SizeOf(test);
IntPtr buffer = Marshal.AllocHGlobal(1024);
Marshal.StructureToPtr(temp, buffer, false);//这句会崩溃~~~
byte[] rawdatas = new byte[rawsize];
Marshal.Copy(buffer, rawdatas, 0, rawsize);
这个方法用的对吗?还有其它方法转换吗?