G
Guest
I am porting an application from C++ to C#, and am having trouble finding a way to quickly and efficiently write structures to a binary file. In C++ this is trivial because all that is necessary is to pack the structure to 1 byte boundries, and then just write out the structure directly to the File IO function
pragma pack (1
typedef struct
char var1[4]
int var1
}MyStruc
fwrite(&myStructure,sizeof(MyStruct),1,filepointer);
Things are clearly different in C# however(and .NET in general). I am using a FileStream(with a 4k buffer for maximum file performance) and it only writes byte arrays. So I think the only issue is how to efficiently turn a structure into a byte array
I know one can marshall the data into a byte array like so
[StructLayout(LayoutKind.Sequential, Pack = 1)
public struct MyStruc
[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
public char[] var1;
public int var2
public class ByteManipulatio
/// <summary
/// Marshals data from an object to an array of bytes
/// </summary
/// <param name="obj">The object to be serialized</param
/// <returns>Array of bytes</returns
public static byte[] SerializeObjectToBytes(object obj
int size = Marshal.SizeOf( obj );
IntPtr buffer = Marshal.AllocHGlobal( size );
Marshal.StructureToPtr( obj, buffer, false );
byte[] data = new byte[ size ];
Marshal.Copy( buffer, data, 0, size );
Marshal.FreeHGlobal( buffer );
return data
but this has 2 problems for my application, 1 it is slow, and 2, it does not work because the Marshall.Sizeof() call fails because of the arrays in the structure (even with the MarshallAs attribute, it compiles fine but I get a runtime error
It would be preferable performance wise for me to use an unsafe block
unsaf
fixed(byte *pData = *(byte*)myStruct
but I keep getting an error that I cannot convert MyStruct to byte*.
Am I casting wrong in the unsafe block? There has to be a way to do this. All I want to do is get myStruct into a byte[]..
Also, I know one can use serialization to stuff objects into binary files, but I am unsure of the formatting effects of the serialization formatter. I need to control the format of the binary file - does the serialization add bytes to the file in any way that would confilct with a well defined file format
Thank you in advance for your help
pragma pack (1
typedef struct
char var1[4]
int var1
}MyStruc
fwrite(&myStructure,sizeof(MyStruct),1,filepointer);
Things are clearly different in C# however(and .NET in general). I am using a FileStream(with a 4k buffer for maximum file performance) and it only writes byte arrays. So I think the only issue is how to efficiently turn a structure into a byte array
I know one can marshall the data into a byte array like so
[StructLayout(LayoutKind.Sequential, Pack = 1)
public struct MyStruc
[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
public char[] var1;
public int var2
public class ByteManipulatio
/// <summary
/// Marshals data from an object to an array of bytes
/// </summary
/// <param name="obj">The object to be serialized</param
/// <returns>Array of bytes</returns
public static byte[] SerializeObjectToBytes(object obj
int size = Marshal.SizeOf( obj );
IntPtr buffer = Marshal.AllocHGlobal( size );
Marshal.StructureToPtr( obj, buffer, false );
byte[] data = new byte[ size ];
Marshal.Copy( buffer, data, 0, size );
Marshal.FreeHGlobal( buffer );
return data
but this has 2 problems for my application, 1 it is slow, and 2, it does not work because the Marshall.Sizeof() call fails because of the arrays in the structure (even with the MarshallAs attribute, it compiles fine but I get a runtime error
It would be preferable performance wise for me to use an unsafe block
unsaf
fixed(byte *pData = *(byte*)myStruct
but I keep getting an error that I cannot convert MyStruct to byte*.
Am I casting wrong in the unsafe block? There has to be a way to do this. All I want to do is get myStruct into a byte[]..
Also, I know one can use serialization to stuff objects into binary files, but I am unsure of the formatting effects of the serialization formatter. I need to control the format of the binary file - does the serialization add bytes to the file in any way that would confilct with a well defined file format
Thank you in advance for your help