static int Main(string[] args) { var task = new InjectModuleInitializerImpl(); if (args.Length == 0 || args.Length > 2 || Regex.IsMatch(args[0], @"^((/|--?)(\?|h|help))$")) { PrintHelp(); return 1; } Console.WriteLine("InjectModuleInitializer v1.1"); Console.WriteLine(""); task.AssemblyFile = args[args.Length - 1]; if (args.Length == 2) { var match = Regex.Match(args[0], "^(/m:|/ModuleInitializer:)(.+)", RegexOptions.IgnoreCase); if (!match.Success) { Console.Error.WriteLine("ERROR: Invalid argument '{0}', type InjectModuleInitializer /? for help", args[0]); return 1; } task.ModuleInitializer = match.Groups[2].Value; } int result = task.Execute() ? 0 : 1; if (result == 0) { Console.WriteLine("Module Initializer successfully injected in assembly " + task.AssemblyFile); } return result; }
private static void TestExe(string source, string initializer) { string exe = CompileAssembly(source, true); var injector = new InjectModuleInitializerImpl { AssemblyFile = exe, ModuleInitializer = initializer }; Assert.IsTrue(injector.Execute(), "Injection failed"); var info = new ProcessStartInfo { RedirectStandardOutput = true, FileName = exe, UseShellExecute = false, CreateNoWindow = true }; var proc = Process.Start(info); Assert.IsNotNull(proc); string output = proc.StandardOutput.ReadToEnd(); Assert.AreEqual("<ModuleInitializer><MainMethod>", output); proc.WaitForExit(); Thread.Sleep(200); File.Delete(exe); }
public void TestDllSuccess() { string dll = CompileAssembly(@" public class Empty { public static string NeverSet {get; set;} } public class ModuleInitializer { public static void Run() { Empty.NeverSet = ""SetByModuleInitializer""; } } ", false); var injector = new InjectModuleInitializerImpl { AssemblyFile = dll }; Assert.IsTrue(injector.Execute(), "Injection failed"); Assembly ass = Assembly.Load(File.ReadAllBytes(dll)); Type t = ass.GetType("Empty"); string value = (string)t.GetProperty("NeverSet").GetGetMethod().Invoke(null, null); Assert.AreEqual("SetByModuleInitializer", value); File.Delete(dll); }
static int Main(string[] args) { //args = new string[] { @"C:\Users\Juan\Documents\Visual Studio 2010\Projects\BattleSpin\Xdtk\DebugConsole\bin\x86\Debug\Gearset.dll" }; var task = new InjectModuleInitializerImpl(); if (args.Length == 0 || args.Length > 2 || Regex.IsMatch(args[0], @"^((/|--?)(\?|h|help))$")) { PrintHelp(); return 1; } Console.WriteLine("InjectModuleInitializer v1.1"); Console.WriteLine(""); task.AssemblyFile = args[args.Length - 1]; if (args.Length == 2) { var match = Regex.Match(args[0], "^(/m:|/ModuleInitializer:)(.+)", RegexOptions.IgnoreCase); if (!match.Success) { Console.Error.WriteLine("ERROR: Invalid argument '{0}', type InjectModuleInitializer /? for help", args[0]); return 1; } task.ModuleInitializer = match.Groups[2].Value; } int result = task.Execute() ? 0 : 1; if (result == 0) { Console.WriteLine("Module Initializer successfully injected in assembly " + task.AssemblyFile); } //Console.WriteLine("Press enter to coninue."); //Console.ReadLine(); return result; }
private static void ExpectFailure(string expectedMessage, string assemblyName, string moduleInitializer, string source) { Assert.IsTrue(assemblyName == null || source == null, "Either source or assembly name should be passed as null"); LogMessageCollector logger = new LogMessageCollector(); var injector = new InjectModuleInitializerImpl(); injector.LogError = logger.Log; injector.ModuleInitializer = moduleInitializer; if (string.IsNullOrEmpty(assemblyName)) { assemblyName = CompileAssembly(source, true); } injector.AssemblyFile = assemblyName; injector.Execute(); Assert.Greater(logger.Messages.Count, 0, "No messages collected"); Assert.AreEqual(expectedMessage, logger.Messages[0]); if (File.Exists(assemblyName)) { File.Delete(assemblyName); } }