locked
Serialization problem RRS feed

  • Question

  •  

    Here a code with a Managed Class CMot

    Then an object of CMot is serialized and deserialized.

    The deserilization sentence throw an exception.

    With a String rather than a CMot object, it works

    Thanks for your help

    Christian from France

     

    Runtime :

    Srialization test
    sauve
    Binary stream '246' does not contain a valid BinaryHeader. Possible causes are i
    nvalid stream or object version change between serialization and deserialization
    .

       at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
       at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(He
    aderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAp
    pDomain, IMethodCallMessage methodCallMessage)
       at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
    (Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCr
    ossAppDomain, IMethodCallMessage methodCallMessage)
       at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
    (Stream serializationStream)
       at restore(String path) in c:\documents and settings\christian\mes documents\
    visual studio 2008\projects\lexiquebilinguesolution\testserialization\testserial
    ization.cpp:line 79
    Erreur lors de la restauration
    Appuyez sur la touche entrée pour continuer ...

     

     

    // TestSerialization.cpp : CODE.

    #include "stdafx.h"

    using namespace System;

    using namespace System::IO;

    using namespace System::Runtime::Serialization::Formatters::Binary;

    using namespace System::Runtime::Serialization;



    //=================================================================

    [Serializable]

    ref class CMot

    {

    public:

    //CMot(void) {}

    CMot(String ^fr) {strFr = fr; dtCreation=System::DateTime::Now;}

    void addInterroA2F(bool succes) {

    nbInterroA2F++;

    if (succes) nbSuccesA2F++;

    }

    String ^getFr() { return strFr; }

    int getNbInterroA2F() {return nbInterroA2F;}

    int getNbSuccesA2F() {return nbSuccesA2F;}

    DateTime^ getDateCreation() {return dtCreation;}

    virtual String^ ToString(void) override { return strFr; }

    private:

    String ^strFr;

    DateTime ^dtCreation;

    int nbInterroA2F;

    int nbSuccesA2F;

    };

     

    //=================================================================

    bool sauve(String ^path)

    {

    bool ret=true;

    FileStream ^file;

    try

    {

    BinaryFormatter^ formatter = gcnew BinaryFormatter;

    file = gcnew FileStream(path, FileMode::Create);

    CMot ^m= gcnew CMot("Bonjour");

    //String ^m="Test";

    formatter->Serialize(file, m);

    }

    catch( SerializationException^ e )

    {

    Console::WriteLine(e->Message + "\n\n" + e->StackTrace);

    ret=false;

    }

    catch(Exception^ e)

    {

    Console::WriteLine(e->Message + "\n\n" + e->StackTrace);

    ret=false;

    }

    finally

    {

    file->Close();

    }

    return ret;

    }



    //=================================================================

    bool restore(String ^path)

    {

    bool ret=true;

    FileStream^ file;

    try

    {

    BinaryFormatter^ formatter = gcnew BinaryFormatter;

    file = gcnew FileStream( path,FileMode::Open );

    //Stream^ file = File::Open(path, FileMode::Open);

    CMot ^m=nullptr ; //gcnew CMot();

    //String ^m;

    Object ^o = formatter->Deserialize(file);

    //m=dynamic_cast<String^>(o);

    m=dynamic_cast<CMot^>(o);

    Console::WriteLine("Mot restaure' : " + m);

    }

    catch ( SerializationException^ e )

    {

    Console::WriteLine(e->Message + "\n\n" + e->StackTrace);

    ret=false;

    }

    catch(Exception^ e)

    {

    Console::WriteLine(e->Message + "\n\n" + e->StackTrace);

    ret=false;

    }

    finally

    {

    file->Close();

    }

    return ret;

    }



    //=================================================================

    int main(array<System::String ^> ^args)

    {

    Console::WriteLine("Srialization test");

    if (sauve("sauve.lxq"))

    {

    Console::WriteLine("sauve");

    if (restore("sauve.lxq"))

    {

    Console::WriteLine("restaure");

    }

    else

    Console::WriteLine("Erreur lors de la restauration");

    }

    else

    Console::WriteLine("Erreur lors de la sauvegarde");

    Console::WriteLine(L"Appuyez sur la touche entrée pour continuer ...");

    Console::ReadLine();

    return 0;

    }

    Saturday, May 31, 2008 9:34 AM

Answers

  • Your declaration for the dtCreation member is incorrect.  DateTime is a value type, not a reference type.  Drop the "hat" and it will work.  Same for the getDateCreation() return type.

    My compliments on your repro, you made it easy to help you.

    Hans Passant.
    • Marked as answer by Yan-Fei Wei Thursday, June 5, 2008 5:17 AM
    Saturday, May 31, 2008 2:49 PM

All replies

  • Your declaration for the dtCreation member is incorrect.  DateTime is a value type, not a reference type.  Drop the "hat" and it will work.  Same for the getDateCreation() return type.

    My compliments on your repro, you made it easy to help you.

    Hans Passant.
    • Marked as answer by Yan-Fei Wei Thursday, June 5, 2008 5:17 AM
    Saturday, May 31, 2008 2:49 PM
  • Thanks a lot,

    Christian
    Sunday, June 1, 2008 8:32 AM