Running executable from MemoryStream / Resources / binary array
Submitted by Tomasz Paprocki on 8 September, 2010 - 15:28
Be aware, that executables or assemblies loaded in such manner share main application (host) domain. This implies that if there is any reference needed other from GAC (GlobalAssemblyCache) or host working directory - you will need to handle AppDomain.CurrentDomain.AssemblyResolve event or create new AppDomain for example. Sample code:
using System; using System.IO; using System.Reflection; namespace ExecutableMemoryStream { class Program { static void Main(string[] args) { try { if (args.Length == 0) throw new TargetParameterCountException( "Expected at least one parameter containing executable path."); using (FileStream fileStream = new FileStream(args[0], FileMode.Open)) using (BinaryReader reader = new BinaryReader(fileStream)) { byte[] bin = reader.ReadBytes(Convert.ToInt32(fileStream.Length)); Assembly assembly = Assembly.Load(bin); MethodInfo method = assembly.EntryPoint; if (method != null) { object o = assembly.CreateInstance(method.ReflectedType.Name); if (method.GetParameters().Length == 0) method.Invoke(o, new object[0]); else { string[] parameters = new string[args.Length - 1]; for (int i = 1; i < args.Length; i++) parameters[i - 1] = args[i]; method.Invoke(o, new[] { parameters }); } } } } catch (Exception ex) { Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(); Console.WriteLine(ex.Message.PadRight(80)); Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Hit 'd' for details. Any other key will terminate application."); if (Console.ReadKey(true).KeyChar == 'd') { Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(); Console.WriteLine(ex.StackTrace); Console.ReadKey(); } } } } }
Imports System.IO Imports System.Reflection Namespace ExecutableMemoryStream Class Program Private Shared Sub Main(args As String()) Try If args.Length = 0 Then Throw New TargetParameterCountException("Expected at least one parameter containing executable path.") End If Using fileStream As New FileStream(args(0), FileMode.Open) Using reader As New BinaryReader(fileStream) Dim bin As Byte() = reader.ReadBytes(Convert.ToInt32(fileStream.Length)) Dim assembly__1 As Assembly = Assembly.Load(bin) Dim method As MethodInfo = assembly__1.EntryPoint If method IsNot Nothing Then Dim o As Object = assembly__1.CreateInstance(method.ReflectedType.Name) If method.GetParameters().Length = 0 Then method.Invoke(o, New Object(-1) {}) Else Dim parameters As String() = New String(args.Length - 2) {} For i As Integer = 1 To args.Length - 1 parameters(i - 1) = args(i) Next method.Invoke(o, New () {parameters}) End If End If End Using End Using Catch ex As Exception Console.BackgroundColor = ConsoleColor.Red Console.ForegroundColor = ConsoleColor.White Console.WriteLine() Console.WriteLine(ex.Message.PadRight(80)) Console.BackgroundColor = ConsoleColor.Black Console.ForegroundColor = ConsoleColor.Yellow Console.WriteLine("Hit 'd' for details. Any other key will terminate application.") If Console.ReadKey(True).KeyChar = "d"C Then Console.ForegroundColor = ConsoleColor.Gray Console.WriteLine() Console.WriteLine(ex.StackTrace) Console.ReadKey() End If End Try End Sub End Class End Namespace
Attachment | Size |
---|---|
[binaries] ExecutableMemoryStream.zip | 29.29 KB |
[sources] ExecutableMemoryStream.zip | 42.86 KB |
Comments
Post new comment