Load an EXE file and run it from memory |
Visite: 28219 |
mercoledì 26 aprile 2006 |
This example shows how to load an application and run from memory system. After the application load, you can use it without the source EXE file (usually blocked from the system). Useful when you don't have to have the source EXE file on HDD (e.g. on a USB key).
Using the code
This example is divided into 2 simple step: the binary reading of the exe file and it's 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 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 Istance of the Application Main method and invoke it from our application launcher:
// create an istance 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 file, a request file Form starts to ask to the user which file 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.
Link to the CodeProject article
|
Hello, I am asking some help, and I already wrote you e-mail, I will wait and look what you will say... david
|
| Scritto da david - venerdì 4 agosto 2006 alle ore 16.23 |
|
Hello, did you try to load all exe types?
Not all compilations can be loaded by assembly.
|
| Scritto da Sergey - martedì 24 ottobre 2006 alle ore 21.43 |
Hello, did you try to load all exe types? Not all compilations can be loaded by assembly.
It's only an example.. for me was interesting coz I could run an application without block the source file (I needed it). All 2 applications were made by me so I knew how to load them into assembly ;-)
|
| Scritto da ZofM - martedì 24 ottobre 2006 alle ore 21.51 |
|
is there any way to load exe files into assembly in vb?
|
| Scritto da cabbar - lunedì 5 febbraio 2007 alle ore 22.09 |
|
Hi all,
I have used this mode,work fine also "using" GDI+
Assembly a = Assembly.Load(bin);
a.EntryPoint.Invoke(null, null);
I hope you can see if also for you this is a solution.
Regards
Gianmarco
|
| Scritto da Gianmarco Castagna - giovedì 19 aprile 2007 alle ore 23.40 |
|
Hi , this sample(http://www.gianmarcocastagna.somee.com/Section1/Page1.aspx) contains
a method for calling in all case a windows form application, the code:
Assembly a = Assembly.Load(res);
Form f = (Form)a.CreateInstance(a.EntryPoint.ReflectedType.FullName);
f.Show();
Regards
Gianmarco
|
| Scritto da Gianmarco Castagna - martedì 17 luglio 2007 alle ore 20.56 |
|
Here is the code which works in VB 2005.
Dim fs As New IO.FileStream("C:\\MyFile.exe", IO.FileMode.Open, IO.FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim bin As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))
fs.Close()
br.Close()
Dim a As Assembly = Assembly.Load(bin)
Dim method As MethodInfo = a.EntryPoint
Dim o As Object = a.CreateInstance(method.Name)
method.Invoke(o, New Object() {Nothing})
But, unfortunately, I always got error about “Application.SetCompatibleTextRenderingDefault”
Does anybody have idea what to do about how to overcome this?
|
| Scritto da sheridan101 - mercoledì 21 novembre 2007 alle ore 20.47 |
|
I am facing a problem when the parameter to be passed is null. But when i pass a parameter, I can have the assembly loaded directly from the memory stream.
What can be the probable problem?
|
| Scritto da Divyesh - venerdì 15 febbraio 2008 alle ore 15.55 |
|
I tried to implement your source in C# but is not working.
suggest a help?
|
| Scritto da dwee - domenica 4 maggio 2008 alle ore 8.56 |
|
Remember to use the AppDomain.Default.AssemblyResolve event to handle referenced assemblies.
Regards
|
| Scritto da José Angel Yánez - martedì 6 maggio 2008 alle ore 16.33 |
|
Thank you for this trick :-)
|
| Scritto da Nima nikjoo - giovedì 15 maggio 2008 alle ore 14.40 |
|
I need tool for converting Borland TC file(Cpp or c file) or tc compiled(EXE) into ASM(for assmebling in ;***ASSEMBLER= (TURBO ASSEMBLER VERSION 4.1) COPYRIGHT(C)****;
;***LINKER = (TURBO LINK VERSION 7.1.30.1) COPYRIGHT(C) ****;)
|
| Scritto da linux_unix_2006 - venerdì 16 maggio 2008 alle ore 14.36 |
|
Hello , I have a problem with your code , I can't understand what are you doing , can you give an example to show to me how we can laod a WIN32 exe file into memory then run it .
thank you .
Nima Nikjoo
Atropatsoft Security Teamwork.
|
| Scritto da Nima Nikjoo - venerdì 6 giugno 2008 alle ore 0.38 |
|
@Nima:
Sorry but the example of this content is working only with Framework .NET compiled applications.
|
| Scritto da ZofM - venerdì 6 giugno 2008 alle ore 12.39 |
|
Thank you very much , so i have one problem again
i compiled your code but i did not work .
can you explane what i should to do for using it , please :-(
|
| Scritto da Nima Nikjoo - martedì 24 giugno 2008 alle ore 19.48 |
|
I have been used to delphi programming, so that difficulty for changing to delphi
Can you give example in programs delphi?
thank you,
yours sincerely
|
| Scritto da Budi Kris - domenica 10 agosto 2008 alle ore 11.14 |
|
I can't seem to make this work on a wpf app - I get an exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the
target of an invocation. ---> System.IO.IOException: Cannot locate resource
'window1.xaml'....
Is there a way to do this with wfp apps?
|
| Scritto da Jim McCartney - sabato 17 gennaio 2009 alle ore 0.43 |
|
hello,
i have a problem, i can load a program wich is written in C# with windows forms useing this solution. though i cant run a C# console application this way.. it doesnt matter if i pass any para meters or just simply 2 times null. it gives a incorrect parameter exeption,
any help with this??
|
| Scritto da fjux - venerdì 15 maggio 2009 alle ore 10.51 |
|
good tut
|
| Scritto da vanphong - martedì 1 settembre 2009 alle ore 9.24 |
|
hi. is it working also for smart device?
thanks
|
| Scritto da dani - martedì 29 settembre 2009 alle ore 4.46 |
|
you can read any bin file and put in "method.Invoke(o, null);" ??
|
| Scritto da Sanukode - giovedì 18 marzo 2010 alle ore 22.09 |
Scrivi nuovo commento