title: Creating a function from IntPtr
body: <p>I have an IntPtr which points to a memory address which is the start of a function without arguments (it is callable).
  Specially I am trying to convert this C code into C#:</p>
  <pre><code>LPVOID executable_area = VirtualAlloc(NULL, len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  
  memcpy(executable_area, box, len);
  VirtualProtect(executable_area, len, PAGE_EXECUTE, &amp;old_protect);
  
  int(*f)() = (int(*)()) executable_area;
  f();
  </code></pre>
  <p>This is what I got so far:</p>
  <pre><code>IntPtr executable_area = VirtualAlloc(IntPtr.Zero, Class2.len, 0x00001000 | 0x00002000, 0x04);
  Marshal.Copy(Class2.c, 0, executable_area, Class2.c.Length);
  uint old_protect = 0;
  bool result = VirtualProtect(executable_area, Class2.len, 0x10, out old_protect);
  </code></pre>
  <p>This code works. Now comes the trickier part: Calling the memory.
  I tried creating a Func object with Marshal.GetDelegateForFunctionPointer, but this does not work with Func</p>
  <p>Is there anyway to create a function out of an address, which is stored in an IntPtr?</p>