Writing to unmanaged memory in C#

Results 1 to 1 of 1
  1. #1
    Account Upgraded | Title Enabled! AngraMainyu is offline
    MemberRank
    May 2011 Join Date
    445Posts

    Writing to unmanaged memory in C#

    My line of work involves a great deal of interop, and although I've written a number of solutions for manipulating unmanaged memory in C#, this is probably the most elegant. Real programmers don't write safe C#.

    PHP Code:
    using System;
    using System.IO;
    using System.Net;
    using System.Runtime.InteropServices;
    using System.Text;

    namespace 
    AngraMainyu.IO
    {
        public static class 
    UnmanagedMemoryStreamExtension
        
    {
            
    unsafe public static T Read<T>(this UnmanagedMemoryStream streamwhere T struct
            
    {
                var 
    = (T)Marshal.PtrToStructure((IntPtr)(stream.PositionPointer), typeof(T));
                
    stream.Position += Marshal.SizeOf(t);
                return 
    t;
            }
            
    unsafe public static String ReadString(this UnmanagedMemoryStream stream)
            {
                var 
    = new String((sbyte*)stream.PositionPointer);
                
    stream.Position += s.Length;
                return 
    s;
            }
            
    unsafe public static String ReadString(this UnmanagedMemoryStream streamint length)
            {
                var 
    = new String((sbyte*)stream.PositionPointer0length);
                
    stream.Position += s.Length;
                return 
    s;
            }
            public static 
    byte ReadByte(this UnmanagedMemoryStream stream)
            {
                return 
    Read<byte>(stream);
            }
            public static 
    sbyte ReadSByte(this UnmanagedMemoryStream stream)
            {
                return 
    Read<sbyte>(stream);
            }
            public static 
    short ReadInt16(this UnmanagedMemoryStream stream)
            {
                return 
    Read<Int16>(stream);
            }
            public static 
    ushort ReadUInt16(this UnmanagedMemoryStream stream)
            {
                return 
    Read<UInt16>(stream);
            }
            public static 
    int ReadInt32(this UnmanagedMemoryStream stream)
            {
                return 
    Read<Int32>(stream);
            }
            public static 
    uint ReadUInt32(this UnmanagedMemoryStream stream)
            {
                return 
    Read<UInt32>(stream);
            }
            public static 
    long ReadInt64(this UnmanagedMemoryStream stream)
            {
                return 
    Read<Int64>(stream);
            }
            public static 
    ulong ReadUInt64(this UnmanagedMemoryStream stream)
            {
                return 
    Read<UInt64>(stream);
            }
            public static 
    Single ReadSingle(this UnmanagedMemoryStream stream)
            {
                return 
    Read<Single>(stream);
            }
            public static 
    Double ReadDouble(this UnmanagedMemoryStream stream)
            {
                return 
    Read<Double>(stream);
            }
            public static 
    Decimal ReadDecimal(this UnmanagedMemoryStream stream)
            {
                return 
    Read<Decimal>(stream);
            }
            
    unsafe public static void Write<T>(this UnmanagedMemoryStream streamT valuewhere T struct
            
    {
                var 
    length Marshal.SizeOf(value);

                if (
    stream.Position length stream.Length) {
                    throw new 
    NotSupportedException();
                } else {
                    var 
    handle GCHandle.Alloc(value);
                    for (
    int i 0lengthi++) {
                        *(
    stream.PositionPointer i) = ((byte*)handle.AddrOfPinnedObject())[i];
                    }
                    
    stream.Position += length;
                    
    handle.Free();
                }
            }
            public static 
    void WriteByte(this UnmanagedMemoryStream streambyte value)
            {
                
    Write<byte>(streamvalue);
            }
            public static 
    void WriteSByte(this UnmanagedMemoryStream streamsbyte value)
            {
                
    Write<sbyte>(streamvalue);
            }
            public static 
    void WriteInt16(this UnmanagedMemoryStream streamshort value)
            {
                
    Write<Int16>(streamvalue);
            }
            public static 
    void WriteUInt16(this UnmanagedMemoryStream streamushort value)
            {
                
    Write<UInt16>(streamvalue);
            }
            public static 
    void WriteInt32(this UnmanagedMemoryStream streamint value)
            {
                
    Write<Int32>(streamvalue);
            }
            public static 
    void WriteUInt32(this UnmanagedMemoryStream streamuint value)
            {
                
    Write<UInt32>(streamvalue);
            }
            public static 
    void WriteInt64(this UnmanagedMemoryStream streamlong value)
            {
                
    Write<Int64>(streamvalue);
            }
            public static 
    void WriteUInt64(this UnmanagedMemoryStream streamulong value)
            {
                
    Write<UInt64>(streamvalue);
            }
            public static 
    void WriteSingle(this UnmanagedMemoryStream streamfloat value)
            {
                
    Write<Single>(streamvalue);
            }
            public static 
    void WriteDouble(this UnmanagedMemoryStream streamdouble value)
            {
                
    Write<Double>(streamvalue);
            }
            public static 
    void WriteDecimal(this UnmanagedMemoryStream streamdecimal value)
            {
                
    Write<Decimal>(streamvalue);
            }
        }





Advertisement