VB.Net EXE Run It from Memory
First Step
Load the EXE file in one stream and read it as an array of bytes:
Hide Copy Code
// 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();
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);
No comments: