Jump to content
  • Advertisement
Sign in to follow this  

[.net] Porting custom binary file loader from c++ to c#

This topic is 2774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I want to port a loader for a custom binary file format from c++ to c# and have run into some questions I didn't think about before, because my experience with c# in that area is lacking.
In the cpp code I often use things like this

stream.read((char*)&struct, sizeof(Struct));
stream.read((char*)structs, numStructs * sizeof(Struct));

This seems to be problematic in c#, at least I haven't found a solution yet that works similar.
Looking around I found some potential variants.

  1. Use the BinaryReader to read one built-in type at a time, loop for arrays.

  2. Use the StructLayout attribute and write your own method to read a struct from a stream, loop for arrays.

  3. Use the MemoryMappedViewAccessor, which apparently can read structs and arrays thereof. Do I need to annotate the structs with StructLayout anyway?

I understand that the layout of .net structs can be changed by the compiler, which is why the cpp way is not working. Is this correct?
How would you go about this?

Share this post

Link to post
Share on other sites
The BinaryReader method is good when your format is complicated, variably sized, or not totally compatible with native .NET data types (RIFF headers for example). You can intersperse logic and loops, convert unusual data types and read the bytes directly if you need to. In my experience it is also a whole lot of work.

Marshaling structures directly from binary is fast, simple and (mostly) easy. It is also much harder to deal with oddities and may require you to use unsafe code.

I tend to lean toward the second if I can (because I am lazy). If you correctly set up the StructLayout attribute you can query its size, do pointer operations directly on it (for example cast an array of bytes to it) and use it in a number of Marshal operations. (see: Marshal.PtrToStructure, Marshal.SizeOf, Marshal.Copy etc.)

In general: If I am reading a single struct or an array of structs that are made up of compatible (fixed size) primitives than I use marshaling otherwise I use binary readers.

The memory mapped file stuff is new (for me and for .NET) and not something I have had time to play with yet, but I suspect it uses the marshaling calls internally when dealing with structs.

Hope this helps.

Share this post

Link to post
Share on other sites
Thanks for the answer. For now I went ahead and used the BinaryReader, but I'll consider the marshall method for later comparison!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!