How to write unmanaged binary data in a managed C++ app?

Posted on 2004-09-30
Last Modified: 2013-12-03
I want to write the contents of a struct out to disk retaining the exact binary data without any additional header information, and of course be able to later read it back in. What's the best way to accomplish this? Here's what I came up with. Assuming we have a simple struct as follows:

      } X;

whose members have been initialized in some manner, then the following code
can be used to dump the bytes of this structure:

      FileStream* fs = new FileStream("C:\\data.bin", FileMode::Create,
      BinaryWriter* bw = new BinaryWriter(fs);
      char* bytes = (char*)&X;
      char gcbytes __gc[] = new char __gc[sizeof X];
      for (int i = 0; i < sizeof X; i++) gcbytes[i] = bytes[i];

What I don't like about this solution is that it duplicates the entire
structure. Can the data be output directly without this duplication?
Question by:Peter_Steele
  • 2
  • 2
LVL 22

Expert Comment

by:earth man2
ID: 12198489
Does using class instead of struct make things easier ?

class c_X   {

 c_X X;
 FileStream* fs = new FileStream("C:\\data.bin", FileMode::Create,FileAccess::Write);
 BinaryWriter* bw = new BinaryWriter(fs);
 bw->Write( X );

Author Comment

ID: 12199847
Well, theoretically a struct is just a class in C++ with a default access of "public" instead of "private." Beyond that though the code in your example won't even compile. There is no version of the Write method of BinaryWriter that allows a class/struct object as an argument. Must be simple types or a character array.
LVL 22

Expert Comment

by:earth man2
ID: 12201840
Add method to class c_X that casts c_X to an array of char.

cast X to char[]

Author Comment

ID: 12202119
But you cannot cast a class to a char[]. You *can* do this:

      c_X x;
      char* bytes = (char*)&x;

but you cannot say

      c_X x;
      char bytes[] = (char[])x;

or even

     char bytes[] = (char[])&x;

Even if I could do this though it wouldn't solve the problem. The Write method needs a __gc char[] array. I can get a non gc character pointer defined but I need to copy that to a gc array to output it, and that's what I'm trying to avoid.
LVL 11

Accepted Solution

KurtVon earned 125 total points
ID: 12203914
I'm pretty sure you have no choice in the matter simply because the entire purpose of managed extensions on C++ is to prevent this sort of thing from happening.

Let's say there was a way to convert an arbitrary pointer into a managed array without copying the data.  Now we have a managed array, which is supposed to be pointing at type-safe memory, instead pointing to an arbitrary location.  Sure, we know there is the correct size of memory there, but the problem is that unless the compiler is much smarter than I'm willing to give it credit for at this point, it doesn't know.  All it knows is that you told it to manage an array at a pointer.

In other words, how would it distingush a

c_X x;
char bytes[] = (char[sizeof(x)])&x


c_X x;
char bytes[] = (char[sizeof(x) * 2])&x;

without a special parser to look at the number and specifically block arrays larger than the object?  It may be a neat extension, but not very practical.

Actually, I'm surprised your code works without a __pin to lock the pointer.

Hope this helps.

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now