I worked., Thanx a lot...
I have used below functions for "Converting from structure to byte array"
and "byte array to structure"
static byte [] StructureToByteArray(object obj)
{
int len = Marshal.SizeOf(obj);
byte [] arr = new byte[len];
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.StructureToPtr(obj, ptr, true);
Marshal.Copy(ptr, arr, 0, len);
Marshal.FreeHGlobal(ptr);
return arr;
}
static void ByteArrayToStructure(byte [] bytearray, ref object obj)
{
int len = Marshal.SizeOf(obj);
IntPtr i = Marshal.AllocHGlobal(len);
Marshal.Copy(bytearray,0, i,len);
obj = Marshal.PtrToStructure(i, obj.GetType());
Marshal.FreeHGlobal(i);
}
Thanks and Regards
Prakash Prabhu
"William Stacey" <st***********@mvps.org> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
If struct or contains only native types (i.e. int, byte, fixed length
array) you can use the Marshal class and StructToPtr.
--
William Stacey, MVP
"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:#6**************@TK2MSFTNGP11.phx.gbl... Hi,
I have to send a structure through TCPClient socket. we can send
only byte array through the socket,
So please any one can help me by telling How to convert a struct object
into an byte array..
Thanks & Regards
Prakash Prabhu