CArchive issue?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: CArchive issue?

  1. #1
    Join Date
    Jul 2002
    Location
    Seattle Area, WA
    Posts
    241

    CArchive issue?

    I'm getting an exception thrown for CArchive. This is code that worked in a VS6 (COM) DLL that is being updated to VS8 (VS.NET 2005) and an MFC DLL (removed COM).

    Relevant Code:

    Code:
       CString strName = _T("");
       CSequence* pSequence = NULL;
       CFile file;
    
       CFileException fe;
       CString msg;
    
       
          // open file
       if (!file.Open(strFilePath, CFile::modeRead | CFile::shareDenyWrite, &fe))
       {
            // Error handling - no file open issues
       }
    
       CArchive ar(&file, CArchive::load);
    
    	ar >> pSequence; // **EXCEPTION
    
    	ar.Close();
    	file.Close();

    Exception happens at the ar >> pSequence line.

    For the CSequence class, the constructor with CArchive is a Serialize.
    Code:
    	virtual void Serialize(CArchive& ar);
    When it reads the files (created a long time ago, but still working on the older system), it throws an exception:
    <filename> contained an unexpected objet.

    Any ideas? Thoughts?

    Is CArchive compatible with older version of CArchive? I'm guessing that's how those files were created in the first place.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,309

    Re: CArchive issue?

    Quote Originally Posted by KingTermite View Post
    Is CArchive compatible with older version of CArchive? I'm guessing that's how those files were created in the first place.
    I'm not sure if it's compatible or not. Make sure that you have added any new fields to the 2008 program. If it is going to work, it must be an exact match of the old code. Even then it's doubtful that it will work.

    If it is mission critical that you convert the old data files to the new format, consider writing out the old files (using a modified version of the VC6 program) to another format like xml. Then add some temp code to the new VC8 project that takes the xml, creates mfc objects that you can serialize into the new format.
    Last edited by Arjay; May 13th, 2009 at 05:48 PM.

  3. #3
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: CArchive issue?

    You should also check your CSequence class to be sure none of the serialized members has changed in size.

    For instance, in VS6, a CTime object when serialized used 4 bytes, but under VS2005, a CTime object is 12 bytes. That change sure could mess things up.

    Good luck.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,569

    Re: CArchive issue?

    I don't use CArchive ... but this looks suspect to me:

    Code:
    CSequence* pSequence = NULL;
    
    ar >> pSequence;
    Did you just archive a memory location ? Or a CSequence object ?

  5. #5
    Join Date
    Jul 2002
    Location
    Seattle Area, WA
    Posts
    241

    Re: CArchive issue?

    Quote Originally Posted by Philip Nicoletti View Post
    I don't use CArchive ... but this looks suspect to me:

    Code:
    CSequence* pSequence = NULL;
    
    ar >> pSequence;
    Did you just archive a memory location ? Or a CSequence object ?
    It appears to be creating the archive from the file, then writing it out as a CSequence to that pSequence pointer. I looked in the old on (through debugger) and the >> operator appears to allocate the memory. I'm guessing the NULL pointer was what made you suspicious.

  6. #6
    Join Date
    Jul 2002
    Location
    Seattle Area, WA
    Posts
    241

    Re: CArchive issue?

    Quote Originally Posted by krmed View Post
    You should also check your CSequence class to be sure none of the serialized members has changed in size.

    For instance, in VS6, a CTime object when serialized used 4 bytes, but under VS2005, a CTime object is 12 bytes. That change sure could mess things up.

    Good luck.
    This to me seems the most likely. I've looked at where its serializing out and it appears to only be CStrings and some normal data types (longs, ints, doubles, etc...).

  7. #7
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: CArchive issue?

    One other thing to think about - did you migrate the project (through Visual Studio) or did you re-create it?

    In either case, check to make sure you're using the same character set - VS6 used MBCS by default whereas VS2005 uses Unicode. This would cause your CStrings to be changed. (requiring almost double the space).

    Good luck.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center