public static void ExtractAndLaunchAssembly(string compressedAssemblyContents) { byte[] assemblyContents = StringCompressor.DecompressString(compressedAssemblyContents).GetBytes(); Assembly asm = Assembly.Load(assemblyContents); MethodInfo main = asm.EntryPoint; var defaultParameters = main.GetParameters().Select(p => GetDefaultValue(p.ParameterType)).ToArray(); main.Invoke(null, defaultParameters); }
public static void ExtractUnpackAndLaunchAssembly(string compressedAssemblyContents) { loadedAssemblies = new List <Assembly>(); #if DEBUG Console.WriteLine("Decompressing..."); #endif string decompressedAssemblyContents = StringCompressor.DecompressString(compressedAssemblyContents); GC.Collect(); GC.WaitForPendingFinalizers(); //Clean up the massive memory usage string[] squashedAssemblies = decompressedAssemblyContents.Split('_').Reverse().ToArray(); //AppDomain packerDomain = AppDomain.CreateDomain("NBPackerLaunch"); AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += PackerDomain_ReflectionOnlyAssemblyResolve; AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; #if DEBUG Console.WriteLine("Unpacking..."); #endif foreach (var sqAsm in squashedAssemblies.Take(squashedAssemblies.Length - 2)) //All except the last { loadedAssemblies.Add(Assembly.Load(Convert.FromBase64String(sqAsm))); } #if DEBUG Console.WriteLine("Initializing..."); #endif var executableAsm = squashedAssemblies[squashedAssemblies.Length - 1]; Assembly asm = Assembly.Load(Convert.FromBase64String(executableAsm)); /* * foreach (Assembly asm in packerDomain.GetAssemblies().Reverse()) //Find the EXE and run it! * { * if (asm.EntryPoint != null) * { * MethodInfo main = asm.EntryPoint; * var defaultParameters = main.GetParameters().Select(p => GetDefaultValue(p.ParameterType)).ToArray(); * main.Invoke(null, defaultParameters); * } * } * * Assembly asm = Assembly.Load(Convert.FromBase64String(executableAsm)); //The last one, the executable Assembly * //currentDomain.Load(Convert.FromBase64String(executableAsm)); */ MethodInfo main = asm.EntryPoint; var defaultParameters = main.GetParameters().Select(p => GetDefaultValue(p.ParameterType)).ToArray(); //start as STA Thread thread = new Thread(() => main.Invoke(null, defaultParameters)); thread.SetApartmentState(ApartmentState.STA); thread.Start(); }