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.
This commit is contained in:
xThrasherrr committed 2026-10-11 14:27:29 -06:00
1 parent 1b15a04e29
commit f6b4ef2bcd
1 file changed
+7 -1
+7 -1
View File
@@ -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);