Link to home
Start Free TrialLog in
Avatar of ray_code
ray_code

asked on

C# Making a program run in memory also .net and non .net ?)

Hi,

Is it possible to make a program run in memory and also non.net.

I know the process class you can run a program, but that runs an existing program file location.

Also, I know there is assembly class that you can invoke the main method, but that limited to .net.

How I can acheive this?
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

If the question you are asking is, is there a way to write, compile and execute a program that not depend on the .Net Framework the answer is yes. You can use Visual Studio .Net C++ compiler in native mode. This will produce a native exe that will not reference .Net. Of course that also means that you can't use any of the .Net functions but only the Windows API's. You can also get a non .Net compiler and use it.
If the question is can you create a program that will run under the .Net Framework and also on systems that do not have the .Net Framework then the answer is NO.
Avatar of ray_code
ray_code

ASKER

ok thanks, however to clarify:

In C sharp, how would you run a  .exe file , non .net, in binary , and run that in memory?
Are we talking about Windows System? Or Linux system?
Windows.

I want to run any executable from memory as a byte array. So the exe can be  encrypted text file or even as resource data, and run it directly from memory.
Need to understand the question first:

1. The initial program the one that will run the 2nd program from memory, Is this a C# .Net Application?
2. The program that will be embedded into resource of program 1 above, Is it a .Net application?
3. Is program 2 a windows, console or other application.
1. Yes, the c# application will read in an external file (the exe), as bytes.

2. The bytes of the read in exe, could be .net application or might be a binary c program.

3.  The program could be a windows or console or anything.

--
So, I have the bytes of the program, I want to run it in memory. So, I can check if it's a .net assembly and I think if I'm currect I can do something along the lines like:

// load the bytes into Assembly
Assembly a = Assembly.Load(bin);

// search for the Entry Point
MethodInfo method = a.EntryPoint;
if (method != null) {
// create an istance of the Startup form Main method
object o = a.CreateInstance(method.Name);
// invoke the application starting point
method.Invoke(o, null);
}

...

But like, what if it isn't a .net application, how would I run that?

The reason why I want to read the exe in, is because the exe might be pre-encrypted, store various data or need various manipulation, decryption on the bytes before running them in memory.
To your statement, "2. The bytes of the read in exe, could be .net application or might be a binary c program.", To be able to use Reflection on an object the object needs to be a .Net assembly. An assembly "is a reusable, versionable, and self-describing building block of a common language, CLR, runtime application", so it can NOT be used on a binary c program.

So if the program to be run in memory is a .Net application then this can be done.
Yes, I want a to run a NON .net exe in memory.

So this is not possible?
To your statement, "Yes, I want a to run a NON .net exe in memory."

No you can NOT do this because you can only use Reflection on .Net assembles therefore you will not be able to do any of the following :

Assembly a = Assembly.Load(bin);
MethodInfo method = a.EntryPoint;
object o = a.CreateInstance(method.Name);
method.Invoke(o, null);

Because it all uses Reflection.
So there is no other way?
Well you could read the program in and then do what ever on the file such as decrypting it, save it out to a disk file, then run the program using System.Diagnostics.Process to start the program, once you have completed using the program delete it from disk. But that is about it.
Oh, I knew that method from the start. Not exactly what I wanted. hmm.
I think I need to reverse engineer visual basic project found at:

http://www.vbforums.com/showthread.php?t=535124&highlight=runpe
 
and see how that works.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
An accurate solution didn't seem possible