Last active
November 2, 2015 12:43
-
-
Save LordJZ/cb6d1807a94ddde96b09 to your computer and use it in GitHub Desktop.
Patching VTable of a .NET enum type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class VTablePatch | |
{ | |
enum E { V } | |
static string Interceptor(object e) | |
{ | |
return "huehue"; | |
} | |
static unsafe void Main() | |
{ | |
Console.WriteLine(E.V.ToString()); | |
IntPtr func = ((Func<string>)E.V.ToString).Method.MethodHandle.GetFunctionPointer(); | |
IntPtr* slot = (IntPtr*)(typeof(E).TypeHandle.Value + 0x40); | |
IntPtr* end = slot + 8; | |
for (; slot <= end; ++slot) | |
{ | |
if (*slot == func) | |
break; | |
} | |
if (slot > end) | |
throw new Exception(); | |
*slot = ((Func<object, string>)Interceptor).Method.MethodHandle.GetFunctionPointer(); | |
Console.WriteLine(((object)E.V).ToString()); | |
Console.WriteLine(E.V.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment