lundi 12 septembre 2016

How to deserialize a Memory Mapped File View into an object?

I'm looking for a way to parse a memory mapped file view and save its contents in an object. Basically what I'm trying to do is read a binary and save it in an object. In C++ I would simply create a file mapping with CreateFileMapping and map a file view with MapViewOfFile and then dump the view in a struct (eg. IMAGE_DOS_HEADER). Is there any way to do this in C#?

        try
        {
            using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(path, FileMode.Open))
            {
                _mva = mmf.CreateViewAccessor(0, PeOffset.DosHeader);
                _mva.Read<MyType>(myTypeInstance);
                action?.Invoke(imageHeaders);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error opening file");
            _logger.Error(ex);
        }

Thanks in advance!





Aucun commentaire:

Enregistrer un commentaire