forked from killswitch1111/powerguivsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleManager.cs
More file actions
112 lines (95 loc) · 3.71 KB
/
Copy pathConsoleManager.cs
File metadata and controls
112 lines (95 loc) · 3.71 KB
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
using PowerShellTools.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using PowerShellTools.Common.Logging;
namespace PowerShellTools.HostService
{
[SuppressUnmanagedCodeSecurity]
internal static class ConsoleManager
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ConsoleManager));
private const string Kernel32_DllName = "kernel32.dll";
[DllImport(Kernel32_DllName)]
private static extern bool AllocConsole();
[DllImport(Kernel32_DllName)]
private static extern bool FreeConsole();
[DllImport(Kernel32_DllName)]
private static extern IntPtr GetConsoleWindow();
[DllImport(Kernel32_DllName)]
private static extern bool AttachConsole(uint dwProcessId);
/// <summary>
/// Check if there is already a console attached.
/// One application can only have one console attached.
/// </summary>
internal static bool HasConsole
{
get { return GetConsoleWindow() != IntPtr.Zero; }
}
/// <summary>
/// Creates a new console instance if the process is not attached to a console already.
/// </summary>
internal static void AttachConsole()
{
if (!HasConsole)
{
ServiceCommon.Log("Creating and Attaching a console into pshost!");
try
{
Process p = CreateConsole();
if (p != null)
{
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(
(s, eventArgs) =>
{
AttachConsole();
});
ServiceCommon.Log("Attaching the created console");
AttachConsole((uint)p.Id);
}
}
catch (Exception ex)
{
Log.Error("Failed to attach console to PowerShell host process.", ex);
}
}
}
/// <summary>
/// Create console process
/// </summary>
/// <returns>Console process created</returns>
private static Process CreateConsole()
{
Process p = new Process();
try
{
string exeName = Constants.PowerShellHostConsoleExeName;
string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string exeFullPath = Path.Combine(currentPath, exeName);
string hostArgs = String.Format(CultureInfo.InvariantCulture,
"{0}{1}",
Constants.ConsoleProcessIdArg, Process.GetCurrentProcess().Id);
Log.DebugFormat("Starting console. {0} {1}", exeFullPath, hostArgs);
p.StartInfo.FileName = exeFullPath;
p.StartInfo.Arguments = hostArgs;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
}
catch (Exception ex)
{
Log.Error("Failed to create console for PowerShell host process.", ex);
}
return p;
}
}
}