forked from SeidChr/RunHiddenConsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
154 lines (125 loc) · 5.29 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
namespace PowerShellWindowHost
{
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using PowerShellWindowHost.Extensions;
public static class Program
{
private const string DoubleQuote = "\"";
public static int Main()
{
////Debugger.Launch();
var arguments = GetArguments();
GetAssemblyData(out var executingAssemblyLocation, out var executingAssemblyFileName);
Configure(executingAssemblyFileName);
SimpleLog.Info($"Full Commandline: {Environment.CommandLine}");
SimpleLog.Info($"Detected Attributes: {arguments}");
SimpleLog.Info($"RunHiddenConsole Executing Assembly FullName: {executingAssemblyFileName}");
var targetExecutablePath = GetTargetExecutablePath(executingAssemblyLocation, executingAssemblyFileName);
if (targetExecutablePath == null)
{
SimpleLog.Error("Unable to find target executable name in own executable name.");
return -7001;
}
var startInfo = new ProcessStartInfo
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Hidden,
WorkingDirectory = Directory.GetCurrentDirectory(),
Arguments = arguments,
FileName = targetExecutablePath,
};
try
{
var proc = Process.Start(startInfo);
if (proc == null)
{
SimpleLog.Error("Unable to start the target process.");
return -7002;
}
// process will close as soon as its waiting for interactive input.
proc.StandardInput.Close();
proc.WaitForExit();
return proc.ExitCode;
}
catch (Exception ex)
{
SimpleLog.Error("Starting target process threw an unknown Exception: " + ex);
SimpleLog.Log(ex);
return -7003;
}
}
private static void Configure(string executingAssemblyFileName)
{
var logLevelString = ConfigurationManager.AppSettings["LogLevel"];
var logLocation = ConfigurationManager.AppSettings["LogLocation"];
if (logLocation != null)
{
SimpleLog.SetLogDir(logLocation, true);
}
if (logLevelString != null
&& Enum.TryParse(logLevelString, true, out SimpleLog.Severity logLevel))
{
SimpleLog.LogLevel = logLevel;
}
else
{
SimpleLog.LogLevel = SimpleLog.Severity.Error;
}
SimpleLog.BackgroundTaskDisabled = true;
SimpleLog.Prefix = $"{executingAssemblyFileName}.";
}
private static void GetAssemblyData(out string assemblyLocation, out string assemblyFileName)
{
var executingAssembly = Assembly.GetExecutingAssembly();
assemblyFileName = Path.GetFileName(executingAssembly.Location);
assemblyLocation = Path.GetDirectoryName(executingAssembly.Location) ?? string.Empty;
}
private static string GetTargetExecutablePath(string executingAssemblyLocation, string executingAssemblyFileName)
{
var match = Regex.Match(executingAssemblyFileName, @"(.+)w(\.\w{1,3})");
if (!match.Success)
{
return null;
}
var targetExecutableName = match.Groups[1].Value + match.Groups[2].Value;
var envPaths = Environment.GetEnvironmentVariable("PATH")
.SplitExt(Path.PathSeparator);
var configPaths = ConfigurationManager.AppSettings["TargetExecutablePaths"]
.SplitExt(Path.PathSeparator);
// TODO: this can be somewhat expensive in terms of file io. probably a good idea to cache the result for a few seconds.
var targetExecutablePath = configPaths
.AppendExt(executingAssemblyLocation)
.Concat(envPaths)
.NotNullOrWhitespaceExt()
.TrimExt()
.Select(p => Path.Combine(p, targetExecutableName))
.FirstOrDefault(File.Exists);
return targetExecutablePath;
}
private static string GetArguments()
{
var commandLineExecutable = Environment
.GetCommandLineArgs()[0]
.Trim();
var commandLine = Environment
.CommandLine
.Trim();
var argsStartIndex = commandLineExecutable.Length
+ (commandLine.StartsWith(DoubleQuote)
? 2
: 0);
var args = commandLine
.Substring(argsStartIndex)
.Trim();
return args;
}
}
}