Click here to Skip to main content
6,596,602 members and growing! (19,625 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
Views:64,684
Bookmarked:45 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
16 votes for this article.
Popularity: 4.82 Rating: 4.00 out of 5
2 votes, 12.5%
1

2
1 vote, 6.3%
3
1 vote, 6.3%
4
12 votes, 75.0%
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)


Member
Simple C# Developer.
Occupation: Web Developer
Company: HTML.it
Location: Italy Italy

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
GeneralOk, problem solved PinmemberMember 35098547:56 3 Feb '09  
Generaljava Pinmembert3dysu1:35 29 Nov '08  
GeneralRe: java PinmemberMember 35098548:20 3 Feb '09  
General"Could not load file or assembly" Pinmembernjuneardave10:28 31 Jul '08  
GeneralWhy is this necessary? PinmemberPIEBALDconsult18:47 3 Apr '08  
AnswerTHIS IS USEFUL! [modified] Pinmembersebastiannielsen11:22 2 Jul '08  
GeneralCMD Pinmemberkoushik_rebaca3:14 1 Sep '07  
GeneralHi Pinmembermezo_2211:35 25 Mar '07  
GeneralRe: Hi PinmemberMike_Silver_A6:41 20 Apr '09  
GeneralVB Demo (Load an exe file and run it from memory) PinmemberUlambayar3:47 14 Jan '07  
GeneralRe: VB Demo (Load an exe file and run it from memory) Pinmemberuyalksouiwh9:43 2 Apr '07  
GeneralRe: VB Demo (Load an exe file and run it from memory) PinmemberMacka00700722:14 25 Jul '07  
GeneralRe: VB Demo (Load an exe file and run it from memory) PinmemberSheridan1019:39 21 Nov '07  
QuestionHow to load exe compiled with /clr? Pinmembermartho24:14 10 Jan '07  
GeneralRun x.exe from y.exe without extracting from y.exe which is stored as resource PinmemberRahul A. H.21:00 29 Oct '06  
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource PinmemberUlambayar3:42 14 Jan '07  
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource PinmemberUlambayar3:43 14 Jan '07  
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource PinmemberMike_Silver_A22:48 14 Jun '09  
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource PinmemberMike_Silver_A22:47 14 Jun '09  
GeneralCan't be used with all exe types PinmembernSerj10:41 24 Oct '06  
GeneralRe: Can't be used with all exe types PinmemberMike_Silver_A22:50 14 Jun '09  
GeneralWhere to use this... PinmemberJun Du9:43 25 Apr '06  
GeneralBIG NOTE! Pinmemberleppie0:26 25 Apr '06  
GeneralWhy CreateInstance? PinmemberRobert Rohde19:26 24 Apr '06  
GeneralRe: Why CreateInstance? PinmemberPreky22:21 24 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-2009
Web16 | Advertise on the Code Project