Click here to Skip to main content
15,891,721 members
Articles / Programming Languages / C#

Reading Unmanaged Data Into Structures

Rate me:
Please Sign up or sign in to vote.
4.93/5 (34 votes)
8 May 2008CPOL9 min read 196.2K   1.1K   58  
In this article, we will look into reading data from an unmanaged array of bytes into a managed data structure. We will use multiple approaches to optimize the process.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ReadingStructureData
{
    struct Packet
    {
        public short Source;
        public short Destination;
        public int Checksum;
        public int Identifier;
        public int SecondaryIdentifier;
    }

    class Program
    {
        static Packet ReadUsingBinaryReader(byte[] data)
        {
            Packet packet;
            using (BinaryReader reader = new BinaryReader(new MemoryStream(data, false)))
            {
                packet.Source = reader.ReadInt16();
                packet.Destination = reader.ReadInt16();
                packet.Checksum = reader.ReadInt32();
                packet.Identifier = reader.ReadInt32();
                packet.SecondaryIdentifier = reader.ReadInt32();
            }
            return packet;
        }

        static Packet ReadUsingPointer(byte[] data)
        {
            unsafe
            {
                fixed (byte* packet = &data[0])
                {
                    return *(Packet*)packet;
                }
            }
        }

        static T ReadUsingMarshalSafe<T>(byte[] data) where T : struct
        {
            GCHandle gch = GCHandle.Alloc(data, GCHandleType.Pinned);
            try
            {
                return (T)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(T));
            }
            finally
            {
                gch.Free();
            }
        }

        static T ReadUsingMarshalUnsafe<T>(byte[] data) where T : struct
        {
            unsafe
            {
                fixed (byte* p = &data[0])
                {
                    return (T) Marshal.PtrToStructure(new IntPtr(p), typeof(T));
                }
            }
        }

        static T ReadUsingCppCli<T>(byte[] data) where T : struct
        {
            return GenericRead.Reader.Read<T>(data);
        }

        static void Main(string[] args)
        {
            byte[] data = { 1, 0, 2, 0, 5, 0, 0, 0, 6, 0, 0, 0, 8, 0, 0, 0 };
            Stopwatch stopwatch;

            Console.WriteLine("Non-Generic Solutions:" + Environment.NewLine);
            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < 1000000; ++i)
            {
                ReadUsingBinaryReader(data);
            }
            Console.WriteLine("BinaryReader: " + stopwatch.ElapsedMilliseconds);

            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < 1000000; ++i)
            {
                ReadUsingPointer(data);
            }
            Console.WriteLine("Pointer: " + stopwatch.ElapsedMilliseconds);

            Console.WriteLine(Environment.NewLine + Environment.NewLine +
                "Generic Solutions:" + Environment.NewLine);
            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < 1000000; ++i)
            {
                ReadUsingMarshalSafe<Packet>(data);
            }
            Console.WriteLine("MarshalSafe: " + stopwatch.ElapsedMilliseconds);

            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < 1000000; ++i)
            {
                ReadUsingMarshalUnsafe<Packet>(data);
            }
            Console.WriteLine("MarshalUnsafe: " + stopwatch.ElapsedMilliseconds);

            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < 1000000; ++i)
            {
                ReadUsingCppCli<Packet>(data);
            }
            Console.WriteLine("C++/CLI: " + stopwatch.ElapsedMilliseconds);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer SELA Group
Israel Israel
Sasha Goldshtein is the CTO of SELA Group, an Israeli company specializing in training, consulting and outsourcing to local and international customers.

Sasha's work is divided across these three primary disciplines. He consults for clients on architecture, development, debugging and performance issues; he actively develops code using the latest bits of technology from Microsoft; and he conducts training classes on a variety of topics, from Windows Internals to .NET Performance.

You can read more about Sasha's work and his latest ventures at his blog: http://blogs.microsoft.co.il/blogs/sasha

Sasha writes from Jerusalem, Israel.

Comments and Discussions