From f6b4ef2bcd4cd03d24321e1920c8f8cb82f21159 Mon Sep 17 00:00:00 2001 From: xThrasherrr Date: Sun, 11 Oct 2026 14:27:29 -0600 Subject: [PATCH] fix(server): keep piped output when started headless on Windows Reopening CONOUT$ closed the pipes or file a manager or service gave the server, so nothing it wrote or was sent got through. --- Server/main.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Server/main.cpp b/Server/main.cpp index a30398b..3c65fdd 100644 --- a/Server/main.cpp +++ b/Server/main.cpp @@ -698,7 +698,13 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int cmd_show) { } if (headless) { - if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole()) { + // A server started with its output piped or in a file (by a manager or a service) keeps + // what it was given: reopening CONOUT$ closes it, and then nothing the server writes, or + // is sent, gets through. Started by hand, it takes its parent's console or one of its own. + const HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); + const DWORD out_type = out && out != INVALID_HANDLE_VALUE ? GetFileType(out) : FILE_TYPE_UNKNOWN; + const bool redirected = out_type == FILE_TYPE_PIPE || out_type == FILE_TYPE_DISK; + if (!redirected && (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())) { FILE *fp = nullptr; freopen_s(&fp, "CONOUT$", "w", stdout); freopen_s(&fp, "CONOUT$", "w", stderr);