-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleManager.cs
37 lines (32 loc) · 1.14 KB
/
ConsoleManager.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
using System;
using System.Runtime.InteropServices;
namespace Gh61.EdgePdfPreviewEnabler
{
internal static class ConsoleManager
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(uint dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FreeConsole();
private const uint ATTACH_PARENT_PROCESS = 0xFFFFFFFF;
/// <summary>
/// If the application is run from console, it will attach it to this process, so we can write to the console.
/// If not, it returns false.
/// </summary>
/// <remarks>
/// After all writings to console is done, call <see cref="DetachConsole"/> method.
/// </remarks>
public static bool TryAttachConsole()
{
return AttachConsole(ATTACH_PARENT_PROCESS);
}
/// <summary>
/// Will detach console after successful <see cref="TryAttachConsole"/> call.
/// </summary>
public static void DetachConsole()
{
Console.Out.Flush();
FreeConsole();
}
}
}