mirror of
https://github.com/moonlight-stream/moonlight-qt
synced 2025-01-09 01:38:44 +00:00
Don't clobber existing std handles when attaching to the parent console
Fixes #1270
This commit is contained in:
parent
aa33432c81
commit
9117f6565e
1 changed files with 16 additions and 6 deletions
22
app/main.cpp
22
app/main.cpp
|
@ -559,16 +559,19 @@ int main(int argc, char *argv[])
|
||||||
s_SuppressVerboseOutput = true;
|
s_SuppressVerboseOutput = true;
|
||||||
#endif
|
#endif
|
||||||
#ifdef Q_OS_WIN32
|
#ifdef Q_OS_WIN32
|
||||||
// Attach to the console to be able to print output.
|
// If we don't have stdout or stderr handles (which will normally be the case
|
||||||
// Since we're a /SUBSYSTEM:WINDOWS app, we won't be attached by default.
|
// since we're a /SUBSYSTEM:WINDOWS app), attach to our parent console and use
|
||||||
|
// that for stdout and stderr.
|
||||||
|
//
|
||||||
|
// If we do have stdout or stderr handles, that means the user has used standard
|
||||||
|
// handle redirection. In that case, we don't want to override those handles.
|
||||||
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
|
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
|
||||||
HANDLE conOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
// If we didn't have an old stdout/stderr handle, use the new CONOUT$ handle
|
||||||
if (conOut != INVALID_HANDLE_VALUE && conOut != NULL) {
|
if (IS_UNSPECIFIED_HANDLE(oldConOut)) {
|
||||||
freopen("CONOUT$", "w", stdout);
|
freopen("CONOUT$", "w", stdout);
|
||||||
setvbuf(stdout, NULL, _IONBF, 0);
|
setvbuf(stdout, NULL, _IONBF, 0);
|
||||||
}
|
}
|
||||||
HANDLE conErr = GetStdHandle(STD_ERROR_HANDLE);
|
if (IS_UNSPECIFIED_HANDLE(oldConErr)) {
|
||||||
if (conErr != INVALID_HANDLE_VALUE && conErr != NULL) {
|
|
||||||
freopen("CONOUT$", "w", stderr);
|
freopen("CONOUT$", "w", stderr);
|
||||||
setvbuf(stderr, NULL, _IONBF, 0);
|
setvbuf(stderr, NULL, _IONBF, 0);
|
||||||
}
|
}
|
||||||
|
@ -762,5 +765,12 @@ int main(int argc, char *argv[])
|
||||||
// sometimes freezing and blocking process exit.
|
// sometimes freezing and blocking process exit.
|
||||||
QThreadPool::globalInstance()->waitForDone(30000);
|
QThreadPool::globalInstance()->waitForDone(30000);
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN32
|
||||||
|
// Without an explicit flush, console redirection for the list command
|
||||||
|
// doesn't work reliably (sometimes the target file contains no text).
|
||||||
|
fflush(stderr);
|
||||||
|
fflush(stdout);
|
||||||
|
#endif
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue