Struct and Byte [] Conversion

xiaoxiao2021-03-06  95

Static Byte [] StructTobytes (Object Structobj)

{

INT size = marshal.sizeof (structobj);

INTPTR BUFFER = Marshal.allochglobal (size);

Try

{

Marshal.StructureToptr (structobj, buffer, false);

Byte [] BYTES = New byte [size];

Marshal.copy (Buffer, Bytes, 0, size);

Return Bytes;

}

Finally

{

Marshal.Freehglobal (BUFFER);

}

}

Static Object Bytestostruct (Byte [] Bytes, Type Strcuttype

{

INT size = marshal.sizeof (strcuttype);

INTPTR BUFFER = Marshal.allochglobal (size);

Try

{

Marshal.copy (Bytes, 0, Buffer, Size);

Return Marshal.PTRTOStructure (Buffer, Strcuttype);

}

Finally

{

Marshal.Freehglobal (BUFFER);

}

}

转载请注明原文地址:https://www.9cbs.com/read-106036.html

New Post(0)