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.
License
About the Author
Gianni Marzaloni (ZofM)
source: http://www.codeproject.com/script/Membership/Profiles.aspx?mid=2887297
-------------------------------------
Related:
Trik vb.net2008 code
basic io explorer vb2005
checking if files exist vb2005
Connecting passworded server another domain
Connecting reading from excel VBnet
copy files created between date
Copy Picture from Database
list disk drive vb2005
list file in vb2005
list sub forlder vb2005
Load exe file run from memory
move diferrent location vb2005
network connections VisualBasic2005
open multiple image no OpenFileDialog
play audio files VisualBasic2005
read file vb2005
sqlserver2005-security
view information vb2005
write file in vb2005
Jumat, 06 Februari 2009
Langganan:
Postingan (Atom)