Reading structures from binary streams

U

Usenet User

I am trying to read (and then save) a binary file which has certain
data structures in it. (The file is in propritetary format produced by
a 3rd party MFC application.)

I know that those data structures would correspond to the structures
similar to the one below:

[StructLayout(LayoutKind.Sequential)]
private struct MyStruct
{
Int64 Signature;
Int16 VersionNumber;
Int16 Key;
Int32 NodeID;
byte[] Signature2; <--- byte array of 24 bytes
Int64 ContentLength;
...
}

I implemented the structures in my C# code, but how would I read and
populate them from the file's content? I know I can parse the binary
stream explicitly using, for example, BinaryReader, but it means I
would have to read the stream field by field.

Is there an easier way?

As far as I remember, the above could be easily done in C/C++: one
would just printf/scanf data structures with no additional effort.

TIA!
 
N

Nicholas Paldino [.NET/C# MVP]

The easiest way to do this would be to use the ReadFile API function,
combined with unsafe code. You could declare your byte array like this:

[StructLayout(LayoutKind.Sequential)]
private struct MyStruct
{
Int64 Signature;
Int16 VersionNumber;
Int16 Key;
Int32 NodeID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=24)]
byte[] Signature2; <--- byte array of 24 bytes
Int64 ContentLength;
}

Then, in unsafe code, create an instance of the structure, and pass the
pointer to that to the ReadFile method, passing the appropriate number of
bytes. It should write the contents to unmanaged memory (the memory that
was allocated by the marshaller) and then marshal it back across the
managed/unmanaged boundary correctly.

Hope this helps.
 
U

Usenet User

Guys,

Thank you both for the suggestions!

-UU


Also have a look at:
http://csharp.codenewbie.com/articles/csharp/1431/C_and_advanced_binary_files-Page_1.html

Ab.
http://joehacker.blogspot.com

Usenet User said:
I am trying to read (and then save) a binary file which has certain
data structures in it. (The file is in propritetary format produced by
a 3rd party MFC application.)

I know that those data structures would correspond to the structures
similar to the one below:

[StructLayout(LayoutKind.Sequential)]
private struct MyStruct
{
Int64 Signature;
Int16 VersionNumber;
Int16 Key;
Int32 NodeID;
byte[] Signature2; <--- byte array of 24 bytes
Int64 ContentLength;
...
}

I implemented the structures in my C# code, but how would I read and
populate them from the file's content? I know I can parse the binary
stream explicitly using, for example, BinaryReader, but it means I
would have to read the stream field by field.

Is there an easier way?

As far as I remember, the above could be easily done in C/C++: one
would just printf/scanf data structures with no additional effort.

TIA!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top