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


how convert structure byte array in c#?

i have defined structure this:

public struct cifspacket {     public uint protocolidentifier; //the value must "0xff+'smb'".     public byte command;      public byte errorclass;     public byte reserved;     public ushort error;      public byte flags;      //here there 14 bytes of data used differently among different dialects.     //i want flags2. however, i'll try parsing them.     public ushort flags2;      public ushort treeid;     public ushort processid;     public ushort userid;     public ushort multiplexid;      //trans request     public byte wordcount;//count of parameter words defining data portion of packet.     //from here might undefined...      public int parametersstartindex;      public ushort bytecount; //buffer length     public int bufferstartindex;      public string buffer; } 

in main method, create instance of , assign values it:

cifspacket packet = new cifspacket(); packet.protocolidentifier = 0xff; packet.command = (byte)commandtypes.smb_com_negotiate; packet.errorclass = 0xff; packet.error = 0; packet.flags = 0x00; packet.flags2 = 0x0001; packet.multiplexid = 22; packet.wordcount = 0; packet.bytecount = 119;  packet.buffer = "nt lm 0.12"; 

now want send packet socket. that, need convert structure byte array. how can it?

my full code follows.

static void main(string[] args) {    socket myping = new socket(addressfamily.internetwork,   sockettype.stream , protocoltype.unspecified ) ;     myping.connect("172.24.18.240", 139);      //fake ip address can send sendto     ipaddress ip = new ipaddress(new byte[] { 172,24,18,240 });     ipendpoint ipep = new ipendpoint(ip, 139);      //local ip receiving     ipendpoint local = new ipendpoint(ipaddress.any, 0);     endpoint ep = (endpoint)local;      cifspacket packet = new cifspacket();     packet.protocolidentifier = 0xff;     packet.command = (byte)commandtypes.smb_com_negotiate;     packet.errorclass = 0xff;     packet.error = 0;     packet.flags = 0x00;     packet.flags2 = 0x0001;     packet.multiplexid = 22;     packet.wordcount = 0;     packet.bytecount = 119;      packet.buffer = "nt lm 0.12";      myping.sendto(it takes byte array parameter); } 

what code snippet be?

this easy, using marshalling.

top of file

using system.runtime.interopservices 

function

byte[] getbytes(cifspacket str) {     int size = marshal.sizeof(str);     byte[] arr = new byte[size];      intptr ptr = marshal.allochglobal(size);     marshal.structuretoptr(str, ptr, true);     marshal.copy(ptr, arr, 0, size);     marshal.freehglobal(ptr);     return arr; } 

and convert back:

cifspacket frombytes(byte[] arr) {     cifspacket str = new cifspacket();      int size = marshal.sizeof(str);     intptr ptr = marshal.allochglobal(size);      marshal.copy(arr, 0, ptr, size);      str = (cifspacket)marshal.ptrtostructure(ptr, str.gettype());     marshal.freehglobal(ptr);      return str; } 

in structure, need put before string

[marshalas(unmanagedtype.byvaltstr, sizeconst = 100)] public string buffer; 

and make sure sizeconst big biggest possible string.

and should read this: http://msdn.microsoft.com/en-us/library/4ca6d5z7.aspx


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -