Solved

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

Posted on 2004-09-30
5
390 Views
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:

      struct
      {
        ...
      } 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,
FileAccess::Write);
      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];
      bw->Write(gcbytes);
      bw->Flush();
      bw->Close();

What I don't like about this solution is that it duplicates the entire
structure. Can the data be output directly without this duplication?
0
Comment
Question by:Peter_Steele
[X]
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
5 Comments
 
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 );
 bw->Flush();
 bw->Close();
0
 

Author Comment

by:Peter_Steele
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.
0
 
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[]
0
 

Author Comment

by:Peter_Steele
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.
0
 
LVL 11

Accepted Solution

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

from

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

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

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

740 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