-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsoleHelper.cs
More file actions
60 lines (54 loc) · 1.88 KB
/
Copy pathConsoleHelper.cs
File metadata and controls
60 lines (54 loc) · 1.88 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
/*
ConsoleColorPicker — sample integrated external tool for ImageGlass v10.
Copyright (C) 2026 DUONG DIEU PHAP
Project homepage: https://imageglass.org
MIT License
*/
using System.Runtime.InteropServices;
namespace ConsoleColorPicker;
/// <summary>
/// Best-effort console attachment for a GUI-launched child process.
/// <para>
/// ImageGlass is a GUI process; a child started with <c>UseShellExecute=false</c>
/// inherits no stdout, so <see cref="Console.WriteLine(string)"/> silently goes
/// nowhere. On Windows we allocate our own console window so the user can watch
/// live output. All output is also mirrored to <see cref="Log"/>.
/// </para>
/// </summary>
internal static class ConsoleHelper
{
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool AllocConsole();
/// <summary>
/// Tries to allocate and wire up a visible console window on Windows.
/// No-op on other platforms. Never throws.
/// </summary>
public static void TryAttach()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}
try
{
if (AllocConsole())
{
var stdout = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true };
var stderr = new StreamWriter(Console.OpenStandardError()) { AutoFlush = true };
Console.SetOut(stdout);
Console.SetError(stderr);
Console.Title = "ConsoleColorPicker — ImageGlass tool";
Log.Write("AllocConsole succeeded.");
}
else
{
Log.Write($"AllocConsole returned false (LastError = {Marshal.GetLastWin32Error()}).");
}
}
catch (Exception ex)
{
Log.Write($"AllocConsole threw: {ex}");
}
}
}