Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Runtime.InteropServices;
- namespace ConsoleApp1
- {
- [StructLayout(LayoutKind.Explicit)]
- struct Writer
- {
- private class Helper<T>
- {
- public T value;
- }
- [FieldOffset(0)]
- private Helper<object> obj;
- [FieldOffset(0)]
- private Helper<IntPtr> ptr;
- private static Writer w;
- public static IntPtr ToPointer(object obj)
- {
- if (w.obj == null)
- w.obj = new Helper<object>();
- w.obj.value = obj;
- return w.ptr.value;
- }
- public static object ToObject(IntPtr ptr)
- {
- if (w.obj == null)
- w.obj = new Helper<object>();
- w.ptr.value = ptr;
- return w.obj.value;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- string x = "azaza";
- short[] qq = new short[0];
- IntPtr xptr = Writer.ToPointer(x);
- Marshal.WriteInt32(xptr, Marshal.ReadInt32(Writer.ToPointer(qq)));
- short[] q = (short[])Writer.ToObject(xptr);
- q[0] = (short) 'b';
- Console.WriteLine("azaza");
- Console.ReadKey();
- }
- }
- }
Add Comment
Please, Sign In to add comment