Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#
Article

Load an EXE File and Run It from Memory

Rate me:
Please Sign up or sign in to vote.
4.88/5 (29 votes)
24 Apr 2006CPOL2 min read 340.4K   18.1K   88   47
Simple application to load an EXE file and run it from memory (only for .NET compiled files)

Introduction

This example shows how to load an application and run it from the memory system. After the application load, you can use it without the source EXE file (usually blocked from the system). This is useful when you don't need to have the source EXE file on HDD (e.g. on a USB key).

Using the Code

This example is divided into two simple steps:

  1. The binary reading of the EXE file
  2. Its loading into the Assembly cache of the read result

First Step

Load the EXE file in one stream and read it as an array of bytes:

C#
// read the bytes from the application EXE file
FileStream fs = new FileStream(filePath, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();

Using the FileStream class, it is possible to open the EXE file (location indicated in the filePath variable), read and close it to release resources.

Second Step

Use the Assembly.Load method to load the EXE file (as array of bytes) into the Assembly cache:

C#
// load the bytes into Assembly
Assembly a = Assembly.Load(bin);
Now we can try to find the entry point of the application:
C#
// search for the Entry Point
MethodInfo method = a.EntryPoint;
if (method != null) {
...
}

If an entry point is found, is possible to create an instance of the application Main method and invoke it from our application launcher:

C#
// create an instance of the Startup form Main method
object o = a.CreateInstance(method.Name);
// invoke the application starting point
method.Invoke(o, null);

If all will be ok, the called application will start without using the source EXE file (try to move it to check).

In the demo project zip file, I put a simple application launches one EXE file found in the same folder of the launcher EXE. If there are more than one files, a request file Form starts to ask the user which file to select from the folder.

NOTE: Pay attention to the Application.SetCompatibleTextRenderingDefault method of the called application. Visual Studio 2005 applies this line in the Main method (located inside the Program.cs file) to use the new GDI+ library in our applications, but it will throw an exception because it must be called before the first IWin32Window object is created.

History

  • 24th April, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer HTML.it
Italy Italy
Simple C# Developer.

Comments and Discussions

 
QuestionIt's posible in vb6 ? Pin
dbonet11-Nov-09 5:23
dbonet11-Nov-09 5:23 
GeneralOk, problem solved Pin
Cryptonite3-Feb-09 6:56
Cryptonite3-Feb-09 6:56 
Generaljava Pin
t3dysu29-Nov-08 0:35
t3dysu29-Nov-08 0:35 
GeneralRe: java Pin
Cryptonite3-Feb-09 7:20
Cryptonite3-Feb-09 7:20 
General"Could not load file or assembly" Pin
njuneardave31-Jul-08 9:28
njuneardave31-Jul-08 9:28 
GeneralRe: "Could not load file or assembly" Pin
clemsy30-Mar-13 5:49
clemsy30-Mar-13 5:49 
QuestionWhy is this necessary? Pin
PIEBALDconsult3-Apr-08 17:47
mvePIEBALDconsult3-Apr-08 17:47 
AnswerTHIS IS USEFUL! [modified] PinPopular
sebastiannielsen2-Jul-08 10:22
sebastiannielsen2-Jul-08 10:22 
GeneralCMD Pin
Koushik1-Sep-07 2:14
Koushik1-Sep-07 2:14 
GeneralHi Pin
mezo_2225-Mar-07 10:35
mezo_2225-Mar-07 10:35 
GeneralRe: Hi Pin
Mike_Silver_A20-Apr-09 5:41
Mike_Silver_A20-Apr-09 5:41 
GeneralVB Demo (Load an exe file and run it from memory) Pin
Ulambayar14-Jan-07 2:47
Ulambayar14-Jan-07 2:47 
GeneralRe: VB Demo (Load an exe file and run it from memory) Pin
uyalksouiwh2-Apr-07 8:43
uyalksouiwh2-Apr-07 8:43 
GeneralRe: VB Demo (Load an exe file and run it from memory) Pin
Macka00700725-Jul-07 21:14
Macka00700725-Jul-07 21:14 
GeneralRe: VB Demo (Load an exe file and run it from memory) Pin
Sheridan10121-Nov-07 8:39
Sheridan10121-Nov-07 8:39 
QuestionHow to load exe compiled with /clr? Pin
martho210-Jan-07 3:14
martho210-Jan-07 3:14 
GeneralRun x.exe from y.exe without extracting from y.exe which is stored as resource Pin
Rahul A. H.29-Oct-06 20:00
Rahul A. H.29-Oct-06 20:00 
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource Pin
Ulambayar14-Jan-07 2:42
Ulambayar14-Jan-07 2:42 
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource Pin
Ulambayar14-Jan-07 2:43
Ulambayar14-Jan-07 2:43 
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource Pin
Mike_Silver_A14-Jun-09 21:48
Mike_Silver_A14-Jun-09 21:48 
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource Pin
Mike_Silver_A14-Jun-09 21:47
Mike_Silver_A14-Jun-09 21:47 
GeneralCan't be used with all exe types Pin
nSerj24-Oct-06 9:41
nSerj24-Oct-06 9:41 
GeneralRe: Can't be used with all exe types Pin
Mike_Silver_A14-Jun-09 21:50
Mike_Silver_A14-Jun-09 21:50 
GeneralWhere to use this... Pin
Jun Du25-Apr-06 8:43
Jun Du25-Apr-06 8:43 
GeneralBIG NOTE! Pin
leppie24-Apr-06 23:26
leppie24-Apr-06 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.