5,699,997 members and growing! (14,658 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

Load an EXE File and Run It from Memory

By Gianni Marzaloni (ZofM)

Simple application to load an EXE file and run it from memory (only for .NET compiled files)
C#, Windows, .NET, Visual Studio, Dev

Posted: 24 Apr 2006
Updated: 24 Apr 2006
Views: 45,345
Bookmarked: 34 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 4.48 Rating: 3.91 out of 5
2 votes, 14.3%
1
0 votes, 0.0%
2
1 vote, 7.1%
3
1 vote, 7.1%
4
10 votes, 71.4%
5

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:

// 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:

// load the bytes into Assembly
Assembly a = Assembly.Load(bin);
Now we can try to find the entry point of the application:
// 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:

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

About the Author

Gianni Marzaloni (ZofM)


Simple C# Developer.
Occupation: Web Developer
Location: Italy Italy

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
Generaljavamembert3dysu1:35 29 Nov '08  
General"Could not load file or assembly"membernjuneardave10:28 31 Jul '08  
GeneralWhy is this necessary?memberPIEBALDconsult18:47 3 Apr '08  
AnswerTHIS IS USEFUL! [modified]membersebastiannielsen11:22 2 Jul '08  
GeneralCMDmemberkoushik_rebaca3:14 1 Sep '07  
GeneralHimembermezo_2211:35 25 Mar '07  
GeneralVB Demo (Load an exe file and run it from memory)memberUlambayar3:47 14 Jan '07  
GeneralRe: VB Demo (Load an exe file and run it from memory)memberuyalksouiwh9:43 2 Apr '07  
GeneralRe: VB Demo (Load an exe file and run it from memory)memberMacka00700722:14 25 Jul '07  
GeneralRe: VB Demo (Load an exe file and run it from memory)memberSheridan1019:39 21 Nov '07  
QuestionHow to load exe compiled with /clr?membermartho24:14 10 Jan '07  
GeneralRun x.exe from y.exe without extracting from y.exe which is stored as resourcememberRahul A. H.21:00 29 Oct '06  
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resourcememberUlambayar3:42 14 Jan '07  
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resourcememberUlambayar3:43 14 Jan '07  
GeneralCan't be used with all exe typesmembernSerj10:41 24 Oct '06  
GeneralWhere to use this...memberJun Du9:43 25 Apr '06  
GeneralBIG NOTE!memberleppie0:26 25 Apr '06  
GeneralWhy CreateInstance?memberRobert Rohde19:26 24 Apr '06  
GeneralRe: Why CreateInstance?memberPreky22:21 24 Apr '06  
GeneralRe: Why CreateInstance?memberDario Solera23:29 24 Apr '06  
GeneralRe: Why CreateInstance?memberPreky23:55 24 Apr '06  
GeneralRe: Why CreateInstance?memberDario Solera0:00 25 Apr '06  
GeneralRe: Why CreateInstance?memberPreky0:15 25 Apr '06  
GeneralRe: Why CreateInstance?memberDario Solera0:18 25 Apr '06  
GeneralRe: Why CreateInstance?memberPreky0:30 25 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Apr 2006
Editor: Deeksha Shenoy
Copyright 2006 by Gianni Marzaloni (ZofM)
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project