0

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.

user2864740
  • 60,010
  • 15
  • 145
  • 220
JazzEX
  • 119
  • 1
  • 2
  • 6
  • Missed the "an executable" bit - I take it that is important for some experimental tasks.. might also be a secondary question of how to actually start executing at said allocated memory.. and the entire ask might better 'handled' with help of native or C++-CLI code. – user2864740 Dec 31 '17 at 01:25
  • The reason I said "executable" is because I am making a process memory editing library and if someone will want to use it for a game then the allocated memory will have to be executable or the game would freeze... I think.... might be wrong. – JazzEX Dec 31 '17 at 01:27
  • "executable or the game would freeze" - ? Not quite sure I follow. The "game" would have to be loaded into the allocated memory and then "called" for it needing to be executable to apply. What about *creating and launching a separate executable*? – user2864740 Dec 31 '17 at 01:28
  • "executable" in the sense of the code which will be written there will be, well, executable. Dunno... lately I have been fiddling around with Cheat Engine and there is the "Executable" check box which when checked will scan for executable memory and when unchecked then non-executable memory so I would assume that games would need executable memory rather than non-executable. Oops... one thing I miss read `The "game" would have to be loaded into the allocated memory`, what I am trying to do is allocate my own memory in the existing process, so basically add more bytes. – JazzEX Dec 31 '17 at 01:32
  • That really depends on the game and platform to was compiled for: some the information that is useful to modify will be "mixed in" with executable code while other information won't be.. – user2864740 Dec 31 '17 at 01:38
  • Hmmm... well then how would I go about allocating more memory to the already loaded into RAM process? So it would be something like `Process start -> Process end (here are an extra 2048 bytes)` – JazzEX Dec 31 '17 at 01:48

1 Answers1

1

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).

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23