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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 95
Rubik's Cube Code for Effective Presentation 3 52
Basic Java Case or If-Else statement... 3 50
some keys in my laptop is not working. any suggestion 6 25
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 …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

786 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