-
-
Save mattwarren/e56b3bfb98989535903a to your computer and use it in GitHub Desktop.
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
private static void ChangeToReturnFalse(MethodInfo methodInfo) | |
{ | |
var intPtrConstructor = typeof(IntPtr).GetConstructor(new[] { typeof(void*) }); | |
var method = new DynamicMethod("ChangeToReturnFalse", typeof(IntPtr), Type.EmptyTypes, typeof(ServiceLocationModule)); | |
var generator = method.GetILGenerator(); | |
generator.Emit(OpCodes.Ldftn, methodInfo); | |
generator.Emit(OpCodes.Newobj, intPtrConstructor); | |
generator.Emit(OpCodes.Ret); | |
var addressFunctor = (Func<IntPtr>)method.CreateDelegate(typeof(Func<IntPtr>)); | |
var address = addressFunctor(); | |
var x64Asm = new byte[] | |
{ | |
0x33, 0xc0, 0xf3, 0xc3 | |
}; | |
var x86Asm = new byte[] | |
{ | |
0x55, 0x8b, 0xec, 0x33, 0xc0, 0x5d, 0xc3 | |
}; | |
var asm = IntPtr.Size == 8 ? x64Asm : x86Asm; | |
Marshal.Copy(asm, 0, address, asm.Length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment