"raza" <ra**@uraan.com > wrote in message
news:d8******** *************** **@posting.goog le.com...
In C++ we can do things like (Header is an instance of C++ structure)
Send(m_Sock,(ch ar*)&Header,siz eof(Header),0)
in C# we have Socket.Send(byt e[] , int offset, int size, flags)
how do i convert the instance of a C# class or strucute to byte array
efficiently rathen then creating function myself todo that ?
Regards
[StructLayout(La youtKind.Sequen tial)]
public struct Header
{
public int i;
...
...
}
// create header and setup
Header header = new Header();
....
// determine struct size
int size = Marshal.SizeOf( typeof(Header) );
// create byte-array
byte[] buffer = new byte[size];
// pin the byte array
GCHandle hBuffer = GCHandle.Alloc( buffer, GCHandleType.Pi nned );
// marshal struct into the byte array
Marshal.StructT oPtr( header, hBuffer.AddrOfP innedObject(), true );
// free pinning
hBuffer.Free();
// now you can send the buffer
Send( buffer, 0, size, flags );
HTH,
greetings