[C#] 纯文本查看 复制代码 var kernel32 = NativeLibrary.Load("kernel32.dll");
var virtualProtectEx = (delegate* unmanaged[Cdecl, SuppressGCTransition]<nint, void*, nint, int, out int, bool>)NativeLibrary.GetExport(kernel32, "VirtualProtectEx");
var processHandle = Process.GetCurrentProcess().Handle;
Memory<byte> code = new byte[] {
0x8d, 0x04, 0x11, // lea rax, [rcx+rdx]
0xc3 // ret
};
using (var handle = code.Pin())
{
virtualProtectEx(processHandle, handle.Pointer, code.Length, 0x40, out _);
var f = (delegate*<int, int, int>)handle.Pointer;
Console.WriteLine(f(2, 3)); // 5
}
virtualProtectEx = null;
NativeLibrary.Free(kernel32);
这样也可以 |