Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2004-09-30
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Simple Linear Regression
Introduction to Processes

661 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