xxeell

string_hack

Nov 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Runtime.InteropServices;
  7.  
  8. namespace ConsoleApp1
  9. {
  10.     [StructLayout(LayoutKind.Explicit)]
  11.     struct Writer
  12.     {
  13.         private class Helper<T>
  14.         {
  15.             public T value;
  16.         }
  17.  
  18.         [FieldOffset(0)]
  19.         private Helper<object> obj;
  20.         [FieldOffset(0)]
  21.         private Helper<IntPtr> ptr;
  22.  
  23.         private static Writer w;
  24.  
  25.         public static IntPtr ToPointer(object obj)
  26.         {
  27.             if (w.obj == null)
  28.                 w.obj = new Helper<object>();
  29.              
  30.             w.obj.value = obj;
  31.             return w.ptr.value;
  32.         }
  33.  
  34.         public static object ToObject(IntPtr ptr)
  35.         {
  36.             if (w.obj == null)
  37.                 w.obj = new Helper<object>();
  38.  
  39.             w.ptr.value = ptr;
  40.             return w.obj.value;
  41.         }
  42.     }
  43.  
  44.     class Program
  45.     {
  46.         static void Main(string[] args)
  47.         {
  48.             string x = "azaza";
  49.             short[] qq = new short[0];
  50.  
  51.             IntPtr xptr = Writer.ToPointer(x);
  52.             Marshal.WriteInt32(xptr, Marshal.ReadInt32(Writer.ToPointer(qq)));
  53.  
  54.             short[] q = (short[])Writer.ToObject(xptr);
  55.  
  56.             q[0] = (short) 'b';
  57.  
  58.             Console.WriteLine("azaza");
  59.  
  60.             Console.ReadKey();
  61.         }
  62.     }
  63. }
Add Comment
Please, Sign In to add comment