static int Main(string[] args) { //Parse the command line CommandLineData commands = ParseCommandLine(args); if (commands == null) { return(1); } AssemblyRunner runner = null; try { //Create the sandbox runner = PTRunnerLib.GetPartialTrustInstance <AssemblyRunner>(commands.permission, commands.fullTrustAssemblies.ToArray()); } catch (InvalidOperationException ex) { Console.WriteLine("ERROR: {0}", ex.Message); return(-1); } //And we execute the assembly!!! return(runner.ExecuteAssembly(commands.programName, commands.arguments)); }
/// <summary> /// Command line parser. /// </summary> /// <param name="args">The command line arguments</param> /// <returns>A structure with information about the run</returns> private static CommandLineData ParseCommandLine(string[] args) { try { int i = 0; CommandLineData ret = new CommandLineData(); while (i < args.Length) { if (args[i].StartsWith("-")) { switch (args[i]) { //The partial trust PermissionSet case "-ps": PermSetNames permSet = (PermSetNames)Enum.Parse(typeof(PermSetNames), args[++i], true); ret.permission = PTRunnerLib.GetStandardPermission(permSet); break; //Add full trust assembly case "-af": Assembly asm = Assembly.LoadFrom(args[++i]); ret.fullTrustAssemblies.Add(asm); break; case "-xml": StreamReader sr = new StreamReader(args[++i]); SecurityElement elem = SecurityElement.FromString(sr.ReadToEnd()); ret.permission = new PermissionSet(PermissionState.None); ret.permission.FromXml(elem); break; default: Console.WriteLine("{0} - unknonw option", args[i]); Usage(); return(null); } ++i; } else { break; } } if (i < args.Length) { //This are the arguments for the program that will be run ret.programName = args[i++]; int argsSize = args.Length - i; ret.arguments = new string[argsSize]; if (argsSize > 0) { Array.Copy(args, i, ret.arguments, 0, argsSize); } if (ret.permission == null) { ret.permission = PTRunnerLib.GetStandardPermission(PermSetNames.Execution); } return(ret); } } catch (Exception ex) { Console.WriteLine(":RUNNER: Got exception while parsing command line: {0}", ex.Message); } Usage(); return(null); }