How do I save a class's Raw Data (without .NET object data) to a binary file?

M

mpreisdorf

I want to save the raw data of a class (without the .NET object
overhead) to a binary file. For example:
ref class Test
{
public:
String^ name;
Int32 number;
.....
}

I want to save the raw characters of the string and the raw integer
without the additional .NET Object data. I am trying to create an
array of Bytes and cast that array as my class, but the casting fails.
I was then going to write the Array out using a BinaryFormater object,
but I first need to cast the array as my class so I can easily stuff
the array. Can anyone help by providing an example of casting an array
of Bytes to a managed class, or of writing the actual raw data of a
class to a file?
 
W

William DePalo [MVP VC++]

I want to save the raw data of a class (without the .NET object
overhead) to a binary file. For example:
ref class Test
{
public:
String^ name;
Int32 number;
....
}

I want to save the raw characters of the string and the raw integer
without the additional .NET Object data. I am trying to create an
array of Bytes and cast that array as my class, but the casting fails.
I was then going to write the Array out using a BinaryFormater object,
but I first need to cast the array as my class so I can easily stuff
the array. Can anyone help by providing an example of casting an array
of Bytes to a managed class, or of writing the actual raw data of a
class to a file?

If there is some reason that you can not use the canonical method defined
here:

http://msdn.microsoft.com/library/d...BinaryBinaryFormatterClassSerializeTopic1.asp

and if you are proficient in C++, then you could use the PtrToStringChars()
function

http://msdn.microsoft.com/library/d...vcmxspec/html/vcManagedExtensionsSpec_7_8.asp

to get a pointer to the zero terminated array of characters that comprise
the string in your object. That done you could serialize them and the
integer however you like. Of course, then you will need to take
responsibility for your proprietary the deserialization as well.

Regards,
Will
 
B

Ben Voigt

I want to save the raw data of a class (without the .NET object
overhead) to a binary file. For example:
ref class Test
{
public:
String^ name;
Int32 number;
....
}

I want to save the raw characters of the string and the raw integer
without the additional .NET Object data. I am trying to create an
array of Bytes and cast that array as my class, but the casting fails.
I was then going to write the Array out using a BinaryFormater object,
but I first need to cast the array as my class so I can easily stuff
the array. Can anyone help by providing an example of casting an array
of Bytes to a managed class, or of writing the actual raw data of a
class to a file?

That's not going to happen. However you can treat your object as an array
of bytes. But you must allocate the buffer with the class type. You would
also get further with a value class than a ref class.

This is by nature not verifiably type-safe. Count on using unsafe code
(pointers). Furthermore, the string is not contained inside your object,
only a gc pointer (reference), which can be modified by the gc at any time,
unless you use pin_ptr.

However, something like this should get you the binary data for any field:
Test^ that = gcnew Test();
// fill in that
pin_ptr<Int32> pRaw = &that->number;
BYTE* pBytes = (BYTE*)pRaw;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top