Wondering if anyone could help me with this.

So im new to C++ and know C#. But i need help with something.

I need to Get the bytes of a .net exe file aka c# file
Then execute them in unmanaged c++ into memory


This is the proccess i need it in.

Open a .net compiled exe file
Get the bytes of it
Put into byte array
Execute the bytes into memory without writing the file onto the disk
And quit
All that in c++

Anyhelp?
Last edited on
What do you mean by "executing the bytes into memory?"

Joe
www.concordspark.com
Hi binzel,

It sounds like you are wanting to do reverse engineering. That would be nigh on impossible in today's computing environment because compilers are very aggressive in what they do. Compiled code doesn't have variable names, it would be difficult to pull out some value that was a function argument say.

Then again the whole thing sounds like a virus. The real question is why do you want to do the things mentioned in the OP?
Last edited on
I think it's not possible since a .net executable contains managed code and needs to be executed by the .NET runtime.
To understand how a .exe looks like you can use ILSpy.
https://ourcodeworld.com/articles/read/456/how-to-decompile-read-source-code-of-net-framework-assemblies-using-ilspy
Hmm. Can you write the executable image to a memory mapped file and execute it without writing it to disk? If not, can you install a small ramdisk (yes, these still exist lol) and execute from that? I have not tried the former, and the latter works but its not exactly what you said, its a workaround that avoids the disk though, and the amount of memory on a machine today, allocating a 1-10mb RD is not even a blip on the radar.

There is one other thing that might work. You could compile the program without a main as a library, inject the library code into your array, and I think if you got really hands-on with a function pointer you could cast a pointer to the starting point of the program inside that array and invoke it.

I haven't tried any of this. Just 'thinking out loud' at you.

Last edited on
binzel wrote:
Then execute them in unmanaged c++ into memory


I apologise, I have misread the OP.

So you simply want to run C# code in C++ ? C++ doesn't have a concept of managed code as in garbage collection etc, so I now see what you mean about running it unmanaged in c++. There is no concept of "running" code in C++, instead code is compiled and linked into an executable, which is run by the OS. This involves files.

But the method you are proposing sounds very scary, maybe if there was such a thing as a porting tool c# to c++ ? If not, say you have the code generated by Ipsy that Thomas10965 provided a link to, it's still scary because one would have to convert (rewrite in c++) all the System:: stuff, and take care to do something appropriate instead of the garbage collection.

If you were somehow able to execute this code, what is the advantage of that over simply running the C# executable?



Sounds epic, and not worth it, unless you start again and write it in c++.
Last edited on
I need to Get the bytes of a .net exe file...
Execute the bytes into memory without writing the file onto the disk

It sounds to me like the .net exe is already stored in a file. So why not just execute that file?
Topic archived. No new replies allowed.