Click here to Skip to main content
12,813,626 members (36,080 online)
Click here to Skip to main content
Add your own
alternative version


81 bookmarked
Posted 24 Apr 2006

Load an EXE File and Run It from Memory

, 24 Apr 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple application to load an EXE file and run it from memory (only for .NET compiled files)


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

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.


  • 24th April, 2006: Initial post


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)
Web Developer
Italy Italy
Simple C# Developer.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Jean-Pierre Bachmann11-Sep-16 23:12
professionalJean-Pierre Bachmann11-Sep-16 23:12 
Questionfor VBNET IDE, use this; Pin
Member 1109542921-Jun-16 14:07
memberMember 1109542921-Jun-16 14:07 
QuestionPass Paramete Pin
Kishan Sojitra25-Apr-16 22:45
professionalKishan Sojitra25-Apr-16 22:45 
QuestionIt's can't load win32 exe and also not found entry point for .net assembly :( Pin
Shohag_Ifas12-Feb-15 16:07
memberShohag_Ifas12-Feb-15 16:07 
GeneralMy vote of 5 Pin
Paulo Zemek1-Apr-13 10:36
memberPaulo Zemek1-Apr-13 10:36 
QuestionThis doesn't work with WPF projects Pin
clemsy1-Apr-13 10:11
memberclemsy1-Apr-13 10:11 
Generalit can not run a exe Pin
on-raptor15-Jul-10 9:53
memberon-raptor15-Jul-10 9:53 
QuestionIt's posible in vb6 ? Pin
dbonet11-Nov-09 6:23
memberdbonet11-Nov-09 6:23 
GeneralOk, problem solved Pin
Member 35098543-Feb-09 7:56
memberMember 35098543-Feb-09 7:56 
Generaljava Pin
t3dysu29-Nov-08 1:35
membert3dysu29-Nov-08 1:35 
GeneralRe: java Pin
Member 35098543-Feb-09 8:20
memberMember 35098543-Feb-09 8:20 
General"Could not load file or assembly" Pin
njuneardave31-Jul-08 10:28
membernjuneardave31-Jul-08 10:28 
GeneralRe: "Could not load file or assembly" Pin
clemsy30-Mar-13 6:49
memberclemsy30-Mar-13 6:49 
QuestionWhy is this necessary? Pin
PIEBALDconsult3-Apr-08 18:47
memberPIEBALDconsult3-Apr-08 18:47 
AnswerTHIS IS USEFUL! [modified] Pin
sebastiannielsen2-Jul-08 11:22
membersebastiannielsen2-Jul-08 11:22 
This is EXTREMELY useful.

Just think: Load the target EXE into a byte array. Then encrypt it with System.Security.Cryptography.Rijndaelmanaged using a sha256 of the password as key, and then convert the encrypted EXE to a base64 string.

Then you can make a new project, hard code the base64 string and have a password promt. Then the password decrypt the EXE, and run it from memory as described here. Everything is done in a Try...Catch...End Try block so if wrong password is supplied then it can "MsgBox" the user about it.

This can also be used to build a Anti-Piracy wrapper for a application by encrypting it with a key that is a result from a calculation on a valid CD-key. Incorrect CD-key = the application wont decrypt.

Also you can use this to fetch a encrypted EXE from a server and then decrypt it in-memory with a hard-coded key. Now updating has never been as easy as putting your updated EXE into your webspace and Voilá, the application is up2date for everyone that has previously downloaded your application.
This could also be used in Anti-Piracy situations, by just having a CGI script to encrypt your EXE with the supplied serial number as key, and then the target application who downloaded the encrypted EXE decryptes it using the serial as key.

Of course you have a hard coded "salt" value in the application so not anybody with the correct serial can decrypt for themselves.
Then if you see that a specific serial is used at multiple places, just ban that serial and your application can never be used by that serial any longer, since the client application can no longer download the encrypted EXE.

Another fun thing you can do if you let your client fetch the EXE from the internet, is that if the serial in question is marked as piracy, you could make your server send a encrypted variant of some software that install itself in startup and keylog, screenlog, and records sound from attached mic and take some snaps if theres a webcam. Then stuff everything in a packet and then into your server, that will log IP & time too.

Its s simple matter of prosecuting that person for piracy after that, since you both have IP evidence and *image evidence*.
GeneralCMD Pin
koushik_rebaca1-Sep-07 3:14
memberkoushik_rebaca1-Sep-07 3:14 
GeneralHi Pin
mezo_2225-Mar-07 11:35
membermezo_2225-Mar-07 11:35 
GeneralRe: Hi Pin
Mike_Silver_A20-Apr-09 6:41
memberMike_Silver_A20-Apr-09 6:41 
GeneralVB Demo (Load an exe file and run it from memory) Pin
Ulambayar14-Jan-07 3:47
memberUlambayar14-Jan-07 3:47 
GeneralRe: VB Demo (Load an exe file and run it from memory) Pin
uyalksouiwh2-Apr-07 9:43
memberuyalksouiwh2-Apr-07 9:43 
GeneralRe: VB Demo (Load an exe file and run it from memory) Pin
Macka00700725-Jul-07 22:14
memberMacka00700725-Jul-07 22:14 
GeneralRe: VB Demo (Load an exe file and run it from memory) Pin
Sheridan10121-Nov-07 9:39
memberSheridan10121-Nov-07 9:39 
QuestionHow to load exe compiled with /clr? Pin
martho210-Jan-07 4:14
membermartho210-Jan-07 4:14 
GeneralRun x.exe from y.exe without extracting from y.exe which is stored as resource Pin
Rahul A. H.29-Oct-06 21:00
memberRahul A. H.29-Oct-06 21:00 
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource Pin
Ulambayar14-Jan-07 3:42
memberUlambayar14-Jan-07 3:42 
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource Pin
Ulambayar14-Jan-07 3:43
memberUlambayar14-Jan-07 3:43 
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource Pin
Mike_Silver_A14-Jun-09 22:48
memberMike_Silver_A14-Jun-09 22:48 
GeneralRe: Run x.exe from y.exe without extracting from y.exe which is stored as resource Pin
Mike_Silver_A14-Jun-09 22:47
memberMike_Silver_A14-Jun-09 22:47 
GeneralCan't be used with all exe types Pin
nSerj24-Oct-06 10:41
membernSerj24-Oct-06 10:41 
GeneralRe: Can't be used with all exe types Pin
Mike_Silver_A14-Jun-09 22:50
memberMike_Silver_A14-Jun-09 22:50 
GeneralWhere to use this... Pin
Jun Du25-Apr-06 9:43
memberJun Du25-Apr-06 9:43 
GeneralBIG NOTE! Pin
leppie25-Apr-06 0:26
memberleppie25-Apr-06 0:26 
QuestionWhy CreateInstance? Pin
Robert Rohde24-Apr-06 19:26
memberRobert Rohde24-Apr-06 19:26 
AnswerRe: Why CreateInstance? Pin
Preky24-Apr-06 22:21
memberPreky24-Apr-06 22:21 
GeneralRe: Why CreateInstance? Pin
Dario Solera24-Apr-06 23:29
memberDario Solera24-Apr-06 23:29 
GeneralRe: Why CreateInstance? Pin
Preky24-Apr-06 23:55
memberPreky24-Apr-06 23:55 
GeneralRe: Why CreateInstance? Pin
Dario Solera25-Apr-06 0:00
memberDario Solera25-Apr-06 0:00 
GeneralRe: Why CreateInstance? Pin
Preky25-Apr-06 0:15
memberPreky25-Apr-06 0:15 
GeneralRe: Why CreateInstance? Pin
Dario Solera25-Apr-06 0:18
memberDario Solera25-Apr-06 0:18 
GeneralRe: Why CreateInstance? Pin
Preky25-Apr-06 0:30
memberPreky25-Apr-06 0:30 
GeneralRe: Why CreateInstance? Pin
Robert Rohde25-Apr-06 2:52
memberRobert Rohde25-Apr-06 2:52 
GeneralRe: Why CreateInstance? Pin
ZofM25-Apr-06 4:40
memberZofM25-Apr-06 4:40 
GeneralRe: Why CreateInstance? Pin
WillSmithWorld19-Oct-06 8:05
memberWillSmithWorld19-Oct-06 8:05 
QuestionRe: Why CreateInstance? Pin
netajibasa14-Dec-06 3:25
membernetajibasa14-Dec-06 3:25 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 24 Apr 2006
Article Copyright 2006 by Gianni Marzaloni (ZofM)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid