473,786 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ 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_CS harp_binaryread er.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 12724
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*******@gmai l.com> wrote in message
news:7e******** *************** ***@posting.goo gle.com...
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_CS harp_binaryread er.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.Binar yReader
{
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 = ReadStringLengt h();
if (nNewLen == unchecked((UInt 32)(-1)))
{
nConvert = 1 - nConvert;
nNewLen = ReadStringLengt h();
if (nNewLen == unchecked((UInt 32)(-1)))
return str;
}

// set length of string to new length
UInt32 nByteLen = nNewLen;
nByteLen += (UInt32)(nByteL en * (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 ReadStringLengt h()
{
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((UInt 32)(-1));
}
else if (wLen == 0xffff)
{
// read DWORD of length
nNewLen = ReadUInt32();
return nNewLen;
}
else
return wLen;
}
}

"Airjoe" <ai****@newsgro up.nospam> wrote in message
news:Ou******** ******@TK2MSFTN GP10.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*******@gmai l.com> wrote in message
news:7e******** *************** ***@posting.goo gle.com...
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_CS harp_binaryread er.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
1668
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 getting called.
11
6639
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 transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
1
4800
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 how to do the same but read from standard input stream … like Console.In.
0
1606
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
1865
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 (an endless loop). i thought that BinaryReader.ReadByte advanced to the next byte? i had it time out after 1000 iterations, and keeps outputting the same byte. any help appreciated, my code is below: Imports System.io
11
1686
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 just fine, and I'm getting the correct number of bytes, but the data doesn't look right. :( I assume this is an encoding issue.
6
2180
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 ByteArray = brReader.ReadBytes(1) End While
1
2081
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++ -c CFileRw.cxx CArchive.o: CArchive.cxx HCommon.h g++ -c CArchive.cxx CMain.o: CMain.cxx HCommon.h
1
1593
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 { public: DECLARE_SERIAL(New);
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.