Skip to content

Commit

Permalink
Merge pull request #971 from Chilledheart/windows_attach_parent_conso…
Browse files Browse the repository at this point in the history
…le_if_current_unavailable

windows: attach parent's console if current is unavailable
  • Loading branch information
Chilledheart authored May 23, 2024
2 parents 82e5fbe + bb44fec commit 31c2579
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/win32/yass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
// This function is primarily useful to applications that were linked with /SUBSYSTEM:WINDOWS,
// which implies to the operating system that a console is not needed
// before entering the program's main method.
if (AttachConsole(ATTACH_PARENT_PROCESS) != 0) {
if (GetFileType(GetStdHandle(STD_ERROR_HANDLE)) != FILE_TYPE_UNKNOWN) {
fprintf(stderr, "attached to current console\n");
fflush(stderr);
} else if (AttachConsole(ATTACH_PARENT_PROCESS) != 0) {
FILE* unusedFile;
// Swap to the new out/err streams
freopen_s(&unusedFile, "CONOUT$", "w", stdout);
freopen_s(&unusedFile, "CONOUT$", "w", stderr);
std::cout.clear();
std::clog.clear();
std::cerr.clear();
fprintf(stderr, "attached to parent process\n");
fprintf(stderr, "attached to parent process' console\n");
fflush(stderr);
}

Expand Down

0 comments on commit 31c2579

Please sign in to comment.