C++/CLI Code Snippet - Load object from binary file

C++/CLI Code Snippet - Load object from binary file

C++/CLI Code Snippet - Load object from binary file

C++/CLI code snippet open an external binary data file and convert it to object. This function can be use to deserialize those byte array data (Serialized) from external file to their original objects.

Bookmark:

C++/CLI Code Snippet - Load object from binary file

This .Net C++/CLI code snippet open an external binary data file and convert it to object. This function can be use to deserialize those byte array data (Serialized) from external file to their original objects like image data ..etc. This function uses System.IO and System.Runtime.Serialization.Formatters.Binary name spaces to convert binary data file to object. This function useful to implement database cache control system for high traffic databases by storing cached query object data in external files and read them back to database objects. Modify the exception handling section as to your project requirements.

Note that it will be necessary to cast the returned generic Object from the deserialization method back into the original type of object you passed in to the serialization method originally.

System::Object ^FileToObject(System::String ^_FileName)
{
    try
    {
        // Open file for reading
        System::IO::FileStream ^_FileStream = gcnew System::IO::FileStream(_FileName, System::IO::FileMode::Open, System::IO::FileAccess::Read);

        // attach filestream to binary reader
        System::IO::BinaryReader ^_BinaryReader = gcnew System::IO::BinaryReader(_FileStream);

        // get total byte length of the file
        System::IO::FileInfo ^_FileInfo = gcnew System::IO::FileInfo(_FileName);
        System::Int64 _TotalBytes = _FileInfo->Length;

        // read entire file into buffer
        array<System::Byte> ^_ByteArray = _BinaryReader->ReadBytes(safe_cast<Int32>(_TotalBytes));

        // close file reader and do some cleanup
        _FileStream->Close();
        delete _FileStream;
        _FileStream = nullptr;
        _BinaryReader->Close();

        // convert byte array to memory stream
        System::IO::MemoryStream ^_MemoryStream = gcnew System::IO::MemoryStream(_ByteArray);

        // create new BinaryFormatter
        System::Runtime::Serialization::Formatters::Binary::BinaryFormatter ^_BinaryFormatter = gcnew System::Runtime::Serialization::Formatters::Binary::BinaryFormatter();

        // set memory stream position to starting point
        _MemoryStream->Position = 0;

        // Deserializes a stream into an object graph and return as a object.
        return _BinaryFormatter->Deserialize(_MemoryStream);
    }
    catch (Exception ^_Exception)
    {
        // Error
        Console::WriteLine("Exception caught in process: {0}", _Exception->ToString());
    }

    // Error occured, return null
    return nullptr;
}


Here is a simple example showing how to use above function (FileToObject). In this example we read binary data from external file (image-object.dat) and convert byte array into object, in this case it's an image object, and show the image in picturebox.

pictureBox1->Image = safe_cast<Image^>(FileToObject("c:\\image-object.dat"));


C++/CLI Keywords Used:

  • FileStream
  • BinaryReader
  • FileInfo
  • FileMode
  • FileAccess
  • MemoryStream
  • BinaryFormatter
  • Deserialize
  • byte
  • Exception

Code Snippet Information:

  • Applies To: Visual Studio, .Net, C++, CLI, Byte Array, File to object, FileStream, BinaryReader, FileMode, BinaryFormatter, Deserialize, Cache control, Load objects from file
  • Programming Language : C++/CLI

External Resources:

Leave a comment