Using System;
Using System. Reflection;
Class Program
{
Public delegate uint Ret1ArgDelegate (uint arg1 );
Static uint PlaceHolder1 (uint arg1) {return 0 ;}
Public static byte [] asmBytes = new byte []
{
0x89, 0xD0, // mov eax, EDX
0xD1, 0xC8, // ror eax, 1
0xC3 // RET
};
Unsafe static void Main (string [] args)
{
Fixed (byte * startAddress = & asmBytes [0]) // Take the address of our x86 code
{
// Get the FieldInfo for "_ methodPtr"
Type delType = typeof (Delegate );
FieldInfo _ methodPtr = delType. GetField ("_ methodPtr", BindingFlags. NonPublic | BindingFlags. Instance );
// Set our delegate to our x86 code
Ret1ArgDelegate del = new Ret1ArgDelegate (PlaceHolder1 );
_ MethodPtr. SetValue (del, (IntPtr) startAddress );
// Enjoy
Uint n = (uint) 0 xFFFFFFFC;
N = del (n );
Console. WriteLine ("{0: x}", n );
}
}
}
/* Forgot where to reprint it. The author can contact me. */