Search code examples
c#.netprocessallocationalloc

Is there a way to allocate memory in a .NET process that is "executable"?


I was wondering if there is any way to allocate memory in a process and have that memory be r/w & executable?

I found System.Runtime.InteropServices.Marshal.AllocHGlobal, dunno if that the thing I am looking for, if so then how does it work? I don't really understand it, where is the allocated memory located.


Solution

  • This is a task for the VirtualAlloc and VirtualProtect API calls rather than the interop marshaller. You will have to declare them [DllImport]. Hhowever this entire process would be painful enough that I would seriously consider using a different language. Perhaps a c++ project that provides just the interop calls you need while the UI remains in C#. (Honestly, interop stuff is the only area where I see c++ and .net working well together).