Load an EXE File and Run It from Memory

, 24 Apr 2006

LoadExeIntoAssembly_demo.zip

LoadExeIntoAssembly_src.zip

Simple application to load an EXE file and run it from memory (only for .NET compiled files)

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:

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

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:

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Hide   Copy Code

// load the bytes into Assembly
Assembly a = Assembly.Load(bin);

Now we can try to find the entry point of the application:

Hide   Copy Code

// 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:

Hide   Copy Code

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

[출처] https://www.codeproject.com/Articles/13897/Load-an-EXE-File-and-Run-It-from-Memory

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
82 Machine Learning with ML.Net and C#/VB.Net file 졸리운_곰 2018.08.16 997
81 오픈소스 닷넷 라이브러리 Awesome .NET! file 졸리운_곰 2018.07.08 2342
80 Swagger UI Authoirze권한 주는 방법 file 졸리운_곰 2018.05.22 299
79 Build Simple AI .NET Library - Part 6 - ML Algorithms file 졸리운_곰 2018.03.26 1102
78 Build Simple AI .NET Library - Part 5 - Artificial Neural Networks file 졸리운_곰 2018.03.26 1046
77 Build Simple AI .NET Library - Part 4 - Beyond Perceptron file 졸리운_곰 2018.03.26 293
76 Build Simple AI .NET Library - Part 3 - Perceptron file 졸리운_곰 2018.03.26 238
75 Build Simple AI .NET Library - Part 2 - Machine Learning Introduction file 졸리운_곰 2018.03.26 309
74 Build Simple AI .NET Library - Part 1 - Basics First file 졸리운_곰 2018.03.26 293
73 텍스트 파일을 한 번에 한 줄씩 읽기(Visual C#) 졸리운_곰 2017.08.30 354
72 마우스 클릭 매크로 (C# 버전) file 졸리운_곰 2017.07.30 1401
71 Assembly Manipulation and C# / VB.NET Code Injection file 졸리운_곰 2017.07.01 419
70 [VS2008] 코드 난독화 (Dotfuscator Community Edition 사용) file 졸리운_곰 2017.07.01 612
69 C# getMethod : Call Method by Name 졸리운_곰 2017.07.01 258
68 Reflection for C++ 졸리운_곰 2017.07.01 343
» Load an EXE File and Run It from Memory file 졸리운_곰 2017.07.01 333
66 .Net code injection 닷넷 코드 인젝션 file 졸리운_곰 2017.07.01 298
65 An Application Framework for Editing Objects at Run Time in VB.NET file 졸리운_곰 2017.07.01 1220
64 Runtime Object Editor file 졸리운_곰 2017.07.01 539
63 Snoop is the open source WPF spying utility file 졸리운_곰 2017.07.01 263
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED