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
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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Required 2 58
CSV file copy field 1 to field 2 2 63
Java Inharitance subclass/child extends, reference variable from base/parent class 9 72
Java import explained 4 50
This article will show, step by step, how to integrate R code into a R Sweave document
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?
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

752 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