From db9e7bb41426079cb8c5cef9e77d9601600ac3f5 Mon Sep 17 00:00:00 2001 From: de4dot Date: Tue, 14 Feb 2012 21:51:31 +0100 Subject: [PATCH] Pause before exiting if not started from cmd.exe (n00b proof) --- de4dot.cui/CommandLineParser.cs | 2 +- de4dot.cui/Program.cs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/de4dot.cui/CommandLineParser.cs b/de4dot.cui/CommandLineParser.cs index f9d5625e..028e65ca 100644 --- a/de4dot.cui/CommandLineParser.cs +++ b/de4dot.cui/CommandLineParser.cs @@ -280,7 +280,7 @@ namespace de4dot.cui { } void exit(int exitCode) { - Environment.Exit(exitCode); + throw new ExitException(exitCode); } void usage() { diff --git a/de4dot.cui/Program.cs b/de4dot.cui/Program.cs index 763746c9..530fef91 100644 --- a/de4dot.cui/Program.cs +++ b/de4dot.cui/Program.cs @@ -24,6 +24,13 @@ using de4dot.code; using de4dot.code.deobfuscators; namespace de4dot.cui { + class ExitException : Exception { + public readonly int code; + public ExitException(int code) { + this.code = code; + } + } + public class Program { static IList deobfuscatorInfos = createDeobfuscatorInfos(); @@ -48,6 +55,8 @@ namespace de4dot.cui { } public static int main(string[] args) { + int exitCode = 0; + try { if (Console.OutputEncoding.IsSingleByte) Console.OutputEncoding = new UTF8Encoding(false); @@ -61,16 +70,36 @@ namespace de4dot.cui { parseCommandLine(args, options); new FilesDeobfuscator(options).doIt(); } + catch (ExitException ex) { + exitCode = ex.code; + } catch (UserException ex) { Log.e("ERROR: {0}", ex.Message); + exitCode = 1; } catch (Exception ex) { printStackTrace(ex); Log.e("\nTry the latest version before reporting this problem!"); - return 1; + exitCode = 1; } - return 0; + if (isN00bUser()) { + Console.Error.WriteLine("\n\nPress any key to exit...\n"); + try { + Console.ReadKey(true); + } + catch (InvalidOperationException) { + } + } + + return exitCode; + } + + static bool isN00bUser() { + var env = Environment.GetEnvironmentVariables(); + if (env["VisualStudioDir"] != null) + return false; + return env["windir"] != null && env["PROMPT"] == null; } public static void printStackTrace(Exception ex, Log.LogLevel logLevel = Log.LogLevel.error) {