Skip to content

Instantly share code, notes, and snippets.

@LordJZ
Last active November 2, 2015 12:43
Show Gist options
  • Save LordJZ/cb6d1807a94ddde96b09 to your computer and use it in GitHub Desktop.
Save LordJZ/cb6d1807a94ddde96b09 to your computer and use it in GitHub Desktop.
Patching VTable of a .NET enum type
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