Skip to content

Commit

Permalink
Redirect output to newly allocated console (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
shatyuka authored Dec 10, 2023
1 parent 41de47d commit 2aaa1bc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
10 changes: 8 additions & 2 deletions Hi3Helper.Core/Classes/Data/InvokeProp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,20 @@ public class OpenFileName
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetStdHandle(int nStdHandle, IntPtr hHandle);

[DllImport("kernel32.dll")]
public static extern uint GetLastError();

[DllImport("Kernel32.dll", SetLastError = true)]
public static extern bool AllocConsole();

[DllImport("Kernel32.dll")]
public static extern void AllocConsole();
public static extern bool FreeConsole();

[DllImport("Kernel32.dll")]
public static extern void FreeConsole();
public static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, uint lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, uint hTemplateFile);

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
Expand Down
25 changes: 20 additions & 5 deletions Hi3Helper.Core/Classes/Logger/Type/LoggerConsole.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Hi3Helper
Expand Down Expand Up @@ -83,19 +84,33 @@ public static void AllocateConsole()
return;
}

InvokeProp.AllocConsole();
InvokeProp.m_consoleHandle = InvokeProp.GetStdHandle(-11);
if (!InvokeProp.AllocConsole())
{
throw new ContextMarshalException($"Failed to allocate console with error code: {Marshal.GetLastPInvokeError()}");
}

const uint GENERIC_READ = 0x80000000;
const uint GENERIC_WRITE = 0x40000000;
const uint FILE_SHARE_WRITE = 2;
const uint OPEN_EXISTING = 3;
InvokeProp.m_consoleHandle = InvokeProp.CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);

const int STD_OUTPUT_HANDLE = -11;
InvokeProp.SetStdHandle(STD_OUTPUT_HANDLE, InvokeProp.m_consoleHandle);

Console.OutputEncoding = Encoding.UTF8;
Console.Title = "Collapse Console";

if (!InvokeProp.GetConsoleMode(InvokeProp.m_consoleHandle, out uint mode))
{
throw new ContextMarshalException("Failed to initialize console mode!");
throw new ContextMarshalException($"Failed to get console mode with error code: {Marshal.GetLastPInvokeError()}");
}

if (!InvokeProp.SetConsoleMode(InvokeProp.m_consoleHandle, mode | 12))
const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4;
const uint DISABLE_NEWLINE_AUTO_RETURN = 8;
if (!InvokeProp.SetConsoleMode(InvokeProp.m_consoleHandle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN))
{
throw new ContextMarshalException($"Failed to set console mode with error code: {InvokeProp.GetLastError()}");
throw new ContextMarshalException($"Failed to set console mode with error code: {Marshal.GetLastPInvokeError()}");
}
}
#endregion
Expand Down

0 comments on commit 2aaa1bc

Please sign in to comment.