473,462 Members | 1,055 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Interesting problem: serializing in with MFC CArchive, reading out using C# BinaryReader

I've serialized in various variables of various types (mainly CString,
int, double) into an instantiated MFC CArchive class and saved it as a
binary file.

I am able to open the binary file and read out the individual
variables fine with an MFC application. However I cannot read the
binary file correctly with a C#.NET application, where I'm using the
BinaryReader Class. Doing some debugging I noticed that the problem
occured at this C# line of code:

my_variable_of_type_CString =
instantiated_CSharp_binaryreader.ReadString();

When this line of code execute, the binary reads out the ENTIRE
remainder of the file. So I thought is this one of those hairy, good
old string termination issues, but I've checked the byte representing
the string termination and it's there.

Another strange thing that I noticed is that the first ASCII character
that is suppose to be there as part of the string being read out, is
not being interpreted.

Two other things I want to point out that is specific to my situation
and it is that the string being read out from the binary file has two
consecutive carriage returns and line feeds ("\r\n\r\n"). Also I'm
trying to read out two string consecutively. Could these two aspects
attribute to the problem?

Example, when I use the Studio.NET debugger to see what is contained
in my_variable_of_type_CString, I get something like this:

my_variable_of_type_CString = "ew York City\vNext String to be read
out\0\0\0 . . . (garbage until the end of file"

Any help will be GREATLY appreciated. Thanks in advance.

-RP
Nov 16 '05 #1
2 12686
Hello,

I am planning to write a C# application that needs to read binary files
created with MFC serialization.

Have you made any progress on solving your issues? If so any information
would be appreciated

Thanks,

J
"RP2001" <ra*******@gmail.com> wrote in message
news:7e**************************@posting.google.c om...
I've serialized in various variables of various types (mainly CString,
int, double) into an instantiated MFC CArchive class and saved it as a
binary file.

I am able to open the binary file and read out the individual
variables fine with an MFC application. However I cannot read the
binary file correctly with a C#.NET application, where I'm using the
BinaryReader Class. Doing some debugging I noticed that the problem
occured at this C# line of code:

my_variable_of_type_CString =
instantiated_CSharp_binaryreader.ReadString();

When this line of code execute, the binary reads out the ENTIRE
remainder of the file. So I thought is this one of those hairy, good
old string termination issues, but I've checked the byte representing
the string termination and it's there.

Another strange thing that I noticed is that the first ASCII character
that is suppose to be there as part of the string being read out, is
not being interpreted.

Two other things I want to point out that is specific to my situation
and it is that the string being read out from the binary file has two
consecutive carriage returns and line feeds ("\r\n\r\n"). Also I'm
trying to read out two string consecutively. Could these two aspects
attribute to the problem?

Example, when I use the Studio.NET debugger to see what is contained
in my_variable_of_type_CString, I get something like this:

my_variable_of_type_CString = "ew York City\vNext String to be read
out\0\0\0 . . . (garbage until the end of file"

Any help will be GREATLY appreciated. Thanks in advance.

-RP

Nov 16 '05 #2
A few months ago I had the same problem. I solved it by looking into the MFC
source code and converting the needed code to C#:

public class MFCStringReader : System.IO.BinaryReader
{
public MFCStringReader(Stream s) : base(s)
{
}

public MFCStringReader(Stream s, Encoding e) : base(s, e)
{
}

public string ReadCString()
{
string str = "";
int nConvert = 1; // if we get ANSI, convert

UInt32 nNewLen = ReadStringLength();
if (nNewLen == unchecked((UInt32)(-1)))
{
nConvert = 1 - nConvert;
nNewLen = ReadStringLength();
if (nNewLen == unchecked((UInt32)(-1)))
return str;
}

// set length of string to new length
UInt32 nByteLen = nNewLen;
nByteLen += (UInt32)(nByteLen * (1 - nConvert)); // bytes to read

// read in the characters
if (nNewLen != 0)
{
// read new data
byte[] byteBuf = ReadBytes((int)nByteLen);

// convert the data if as necessary
StringBuilder sb = new StringBuilder();
if (nConvert != 0)
{
for (int i = 0; i < nNewLen; i++)
sb.Append((char)byteBuf[i]);
}
else
{
for (int i = 0; i < nNewLen; i++)
sb.Append((char)(byteBuf[i*2] + byteBuf[i*2+1] * 256));
}

str = sb.ToString();
}

return str;
}

private UInt32 ReadStringLength()
{
UInt32 nNewLen;

// attempt BYTE length first
byte bLen = ReadByte();

if (bLen < 0xff)
return bLen;

// attempt WORD length
UInt16 wLen = ReadUInt16();
if (wLen == 0xfffe)
{
// UNICODE string prefix (length will follow)
return unchecked((UInt32)(-1));
}
else if (wLen == 0xffff)
{
// read DWORD of length
nNewLen = ReadUInt32();
return nNewLen;
}
else
return wLen;
}
}

"Airjoe" <ai****@newsgroup.nospam> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
Hello,

I am planning to write a C# application that needs to read binary files
created with MFC serialization.

Have you made any progress on solving your issues? If so any information
would be appreciated

Thanks,

J
"RP2001" <ra*******@gmail.com> wrote in message
news:7e**************************@posting.google.c om...
I've serialized in various variables of various types (mainly CString,
int, double) into an instantiated MFC CArchive class and saved it as a
binary file.

I am able to open the binary file and read out the individual
variables fine with an MFC application. However I cannot read the
binary file correctly with a C#.NET application, where I'm using the
BinaryReader Class. Doing some debugging I noticed that the problem
occured at this C# line of code:

my_variable_of_type_CString =
instantiated_CSharp_binaryreader.ReadString();

When this line of code execute, the binary reads out the ENTIRE
remainder of the file. So I thought is this one of those hairy, good
old string termination issues, but I've checked the byte representing
the string termination and it's there.

Another strange thing that I noticed is that the first ASCII character
that is suppose to be there as part of the string being read out, is
not being interpreted.

Two other things I want to point out that is specific to my situation
and it is that the string being read out from the binary file has two
consecutive carriage returns and line feeds ("\r\n\r\n"). Also I'm
trying to read out two string consecutively. Could these two aspects
attribute to the problem?

Example, when I use the Studio.NET debugger to see what is contained
in my_variable_of_type_CString, I get something like this:

my_variable_of_type_CString = "ew York City\vNext String to be read
out\0\0\0 . . . (garbage until the end of file"

Any help will be GREATLY appreciated. Thanks in advance.

-RP


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark | last post by:
Which MFC runtime dll is the CArchive object in? I have a situation where the CArchive constructor gpf's when using a certain set of MFC*.dll and msvc*.dll. Trying to narrow down which file is...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
1
by: Vitaly | last post by:
// Open input file and create the BinaryReader. br = new BinaryReader(new FileStream("Test.dat", FileMode.Open, FileAccess.Read)); // Read binary data. d = br.ReadDouble(); A question is...
0
by: Shayne South | last post by:
I have Code From VC++ Version 6 that works fine but when I compile it in Version 7 with MCF support the Client socket stopps receiving FD_READ notifications messages. Any Ideas?
2
by: Mad Scientist Jr | last post by:
i'm trying to read a file byte by byte (and later alter the data and write it to a 2nd file byte by byte) and running into a problem where it seems to keep reading the same byte over and over again...
11
by: Andrew | last post by:
I'm trying to pass a byte array from C# to Java. I used a BinaryReader in C# to read the byte array. I'm using a DataInputStream in Java to read the byte array. The data is passing through...
6
by: Anil Gupte | last post by:
Here is my code: Dim fsReadStream As New FileStream(L3FileName, FileMode.Open, FileAccess.Read) Dim brReader As New BinaryReader(fsReadStream) Dim ByteArray() As Byte While brReader.PeekChar()...
1
by: nu2007 | last post by:
Hi, i have a simple makefile which looks like this: CArchive: CFileRw.o CArchive.o CMain.o g++ -o CArchive CFileRw.o CArchive.o CMain.o 3rdparty_1.a CFileRw.o: CFileRw.cxx HCommon.h g++...
1
by: raja11112222 | last post by:
Hi I am new to vc++, I created the class derived from CObject, I want to implement serialize meyhod in that class the code is: The Class name New and declaration class New : public CObject ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.