Advertisement
CorrM

Untitled

Aug 21st, 2019
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.26 KB | None | 0 0
  1. public static class HeapHelper
  2.     {
  3.         public class StructAllocer<TStruct> : IDisposable
  4.         {
  5.             public IntPtr Ptr { get; private set; }
  6.             public TStruct ManagedStruct { get; private set; }
  7.  
  8.             public StructAllocer()
  9.             {
  10.                 Ptr = Marshal.AllocHGlobal(Marshal.SizeOf<TStruct>());
  11.             }
  12.  
  13.             ~StructAllocer()
  14.             {
  15.                 if (Ptr == IntPtr.Zero) return;
  16.  
  17.                 Marshal.FreeHGlobal(Ptr);
  18.                 Ptr = IntPtr.Zero;
  19.             }
  20.  
  21.             /// <summary>
  22.             /// Update unmanaged data from <see cref="Ptr"/> to managed struct
  23.             /// </summary>
  24.             public bool Update()
  25.             {
  26.                 if (Ptr == IntPtr.Zero)
  27.                     return false;
  28.  
  29.                 ManagedStruct = Marshal.PtrToStructure<TStruct>(Ptr);
  30.                 return true;
  31.             }
  32.  
  33.             public void Dispose()
  34.             {
  35.                 Marshal.FreeHGlobal(Ptr);
  36.                 Ptr = IntPtr.Zero;
  37.                 GC.SuppressFinalize(this);
  38.             }
  39.  
  40.             public static implicit operator IntPtr(StructAllocer<TStruct> w)
  41.             {
  42.                 return w.Ptr;
  43.             }
  44.         }
  45.         public class StringAllocer : IDisposable
  46.         {
  47.             public enum StringType
  48.             {
  49.                 Ansi,
  50.                 Unicode
  51.             }
  52.  
  53.             public IntPtr Ptr { get; private set; }
  54.             public int Length { get; set; }
  55.             public StringType StrType { get; }
  56.             public string ManageString { get; private set; }
  57.  
  58.             public StringAllocer(int len, StringType stringType)
  59.             {
  60.                 StrType = stringType;
  61.                 Length = len;
  62.                 Ptr = Marshal.AllocHGlobal(Length);
  63.             }
  64.  
  65.             ~StringAllocer()
  66.             {
  67.                 if (Ptr == IntPtr.Zero) return;
  68.  
  69.                 Marshal.FreeHGlobal(Ptr);
  70.                 Ptr = IntPtr.Zero;
  71.             }
  72.  
  73.             /// <summary>
  74.             /// Change size of allocated string.
  75.             /// </summary>
  76.             /// <param name="len">New size of string</param>
  77.             public void ReSize(int len)
  78.             {
  79.                 Length = len;
  80.                 Ptr = Marshal.ReAllocHGlobal(Ptr, (IntPtr)len);
  81.                 Update();
  82.             }
  83.  
  84.             /// <summary>
  85.             /// Update unmanaged data from <see cref="Ptr"/> to managed struct
  86.             /// </summary>
  87.             public bool Update()
  88.             {
  89.                 if (Ptr == IntPtr.Zero)
  90.                     return false;
  91.  
  92.                 switch (StrType)
  93.                 {
  94.                     case StringType.Ansi:
  95.                         ManageString = Marshal.PtrToStringAnsi(Ptr);
  96.                         break;
  97.                     case StringType.Unicode:
  98.                         ManageString = Marshal.PtrToStringUni(Ptr);
  99.                         break;
  100.                 }
  101.  
  102.                 return true;
  103.             }
  104.  
  105.             public void Dispose()
  106.             {
  107.                 Marshal.FreeHGlobal(Ptr);
  108.                 Ptr = IntPtr.Zero;
  109.                 GC.SuppressFinalize(this);
  110.             }
  111.  
  112.             public static implicit operator IntPtr(StringAllocer w)
  113.             {
  114.                 return w.Ptr;
  115.             }
  116.  
  117.             public static implicit operator string(StringAllocer w)
  118.             {
  119.                 return w.ManageString;
  120.             }
  121.         }
  122.  
  123.         public static object ToStructure(this byte[] bytes, Type structType)
  124.         {
  125.             object stuff;
  126.             GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  127.             try
  128.             {
  129.                 stuff = Marshal.PtrToStructure(handle.AddrOfPinnedObject(), structType);
  130.             }
  131.             finally
  132.             {
  133.                 handle.Free();
  134.             }
  135.             return stuff;
  136.         }
  137.         public static T ToStructure<T>(this byte[] bytes) where T : struct
  138.         {
  139.             return (T)ToStructure(bytes, typeof(T));
  140.         }
  141.         public static T ToClass<T>(this byte[] bytes) where T : class
  142.         {
  143.             T stuff;
  144.             GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  145.             try
  146.             {
  147.                 stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
  148.             }
  149.             finally
  150.             {
  151.                 handle.Free();
  152.             }
  153.             return stuff;
  154.         }
  155.  
  156.         public static byte[] ToByteArray<T>(this T obj)
  157.         {
  158.             if (obj == null)
  159.                 return null;
  160.  
  161.             BinaryFormatter bf = new BinaryFormatter();
  162.             using (MemoryStream ms = new MemoryStream())
  163.             {
  164.                 bf.Serialize(ms, obj);
  165.                 return ms.ToArray();
  166.             }
  167.         }
  168.         public static T FromByteArray<T>(this byte[] data)
  169.         {
  170.             if (data == null)
  171.                 return default(T);
  172.             BinaryFormatter bf = new BinaryFormatter();
  173.             using (MemoryStream ms = new MemoryStream(data))
  174.             {
  175.                 object obj = bf.Deserialize(ms);
  176.                 return (T)obj;
  177.             }
  178.         }
  179.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement