struct - How to convert a structure to a byte array in C#? -


How do I convert a structure into a byte array in C #?

I have defined a structure:

public architecture CIFSPacket {public UIT protocol identifier; // value should be "0xFF + 'SMB. Public byte command; public byte error class; public byte reserved; public server error; public byte flag; // Here are 14 bytes of data that are different between different dialects Used. // I want 2 flags though, so I will try to parse them. Public Use Flags 2; Public User TreeID; Public Use Process id; public user user id; public user multiplex id // trans request public byte wordCount; // calculates the parameter words defined by the data portion of the packet // Here it can be unreliable ... public int parameter startx; public Utility bytecount; // buffer length public INFF & amp; end; public string buffer;}

My main address In practice, I make an example of this and it provides value:

  CIFSPacket packet = new CIFSPacket (); Packet.protocolIdentifier = 0xff; Packet.command = (byte) commandtaps SMBCM_NGOT; Packet.errorClass = 0xff; Packet.error = 0; Packet.flags = 0x00; Packet.flags2 = 0x0001; Packet.multiplexId = 22; Packet.wordCount = 0; Packet.byteCount = 119; a packet. Buffer = "NT LM 0.12";  

Now I want to send this packet socket. For this, I have to convert the structure to a byte array. How can I do this?

My entire code is as follows.

  Fixed zero main (string [] args) {socket myping = new socket (address Family.InterNetwork, SocketType.Stream, protocol type. Unspecified); MyPing.Connect ("172.24.18.240", 139); // fake IP address so that I can send SendTo IPAddress IP = new IPADress (new byte [] {172,24,18,240}); Ipdpoint ippe = new ipdpoint (ip, 139); Local IP to receive / IPEndPoint local = new IPPpoint (IPadation Any, 0); Endpoint EP = (Endpoint) Local; CIFsacket packet = new CIFsKet (); Packet.protocolIdentifier = 0xff; Packet.command = (byte) commandtaps. SMBCM_NGOT; Packet.errorClass = 0xff; Packet.error = 0; Packet.flags = 0x00; Packet.flags2 = 0x0001; Packet.multiplexId = 22; Packet.wordCount = 0; Packet.byteCount = 119; a packet. Buffer = "NT LM 0.12"; MyPing.SendTo (this takes the byte array as parameter); }  

What will be a code snippet?

is

file's top

  Use the system. Runtime.InteropServices  

function

  byte [] getBytes (CIFS sketch strait) {int size = Marshal.SizeOf (str ); Byte [] arr = new byte [shape]; IntPtr ptr = Marshall AllocHGlobal (size); Martial Structure oct print (str, PTR, true); Marshall.Copy (PTR, ARM, 0, size); Marshal.FreeHGlobal (PTR); Return arr; }  

and convert it back to:

  Biotease (byte [] arr) from CIF sketch {CIF SPKKet SR = new CIFSKette (); Int size = Marshal.SizeOf (str); IntPtr ptr = Marshall AllocHGlobal (size); Marshall.Copy (ARM, 0, PTR, size); Str = (CIF sketch) martial.tterstroke (PTR, straight type) ()); Marshal.FreeHGlobal (PTR); Return str; }  

In your structure, you must put it before the string

  [Marshall AS (UnmanagedType.ByValTStr, SizeConst = 100)] Public string buffer;  

And make sure that SizeConst is the equivalent of your biggest possible string.

And you should probably read it:


Comments