extern static int mprotect(IntPtr addr, IntPtr len, mprot prot);
public static int MemoryProtection(IntPtr ptr, long length, mprot prot) { long start = ptr.ToInt64() % pagesize; long size = pagesize - start; var page = new IntPtr(ptr.ToInt64() - start); int i; for (i = 1; length - size > pagesize * i; i++); if (mprotect(page, (IntPtr)(pagesize * i), prot) < 0) { return -1; } return i; }