-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: not print anything at cli mode
- Loading branch information
Showing
4 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#include "guicon.h" | ||
|
||
#include <tlhelp32.h> | ||
|
||
#include <memory> | ||
#include <stdexcept> | ||
#include <cassert> | ||
#include <iostream> | ||
|
||
DWORD GetParentPID() { | ||
int pid = GetCurrentProcessId(); | ||
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | ||
std::shared_ptr<void> defer(nullptr, [h](auto) { | ||
CloseHandle(h); | ||
}); | ||
|
||
PROCESSENTRY32 pe = {0}; | ||
pe.dwSize = sizeof(PROCESSENTRY32); | ||
|
||
if (!Process32First(h, &pe)) { | ||
throw std::runtime_error("failed at Process32First"); | ||
} | ||
|
||
DWORD parentPid = 0; | ||
|
||
do { | ||
if (pe.th32ProcessID != pid) { | ||
continue; | ||
} | ||
|
||
assert(parentPid == 0); | ||
|
||
parentPid = pe.th32ParentProcessID; | ||
} while (Process32Next(h, &pe)); | ||
|
||
return parentPid; | ||
} | ||
|
||
bool ReleaseConsole() { | ||
bool result = true; | ||
FILE *fp; | ||
|
||
// Just to be safe, redirect standard IO to NUL before releasing. | ||
|
||
// Redirect STDIN to NUL | ||
if (freopen_s(&fp, "NUL:", "r", stdin) != 0) | ||
result = false; | ||
else | ||
setvbuf(stdin, NULL, _IONBF, 0); | ||
|
||
// Redirect STDOUT to NUL | ||
if (freopen_s(&fp, "NUL:", "w", stdout) != 0) | ||
result = false; | ||
else | ||
setvbuf(stdout, NULL, _IONBF, 0); | ||
|
||
// Redirect STDERR to NUL | ||
if (freopen_s(&fp, "NUL:", "w", stderr) != 0) | ||
result = false; | ||
else | ||
setvbuf(stderr, NULL, _IONBF, 0); | ||
|
||
// Detach from console | ||
if (!FreeConsole()) | ||
result = false; | ||
|
||
return result; | ||
} | ||
|
||
bool RedirectConsoleIO() { | ||
bool result = true; | ||
FILE *fp; | ||
|
||
// Redirect STDIN if the console has an input handle | ||
if (GetStdHandle(STD_INPUT_HANDLE) != INVALID_HANDLE_VALUE) | ||
if (freopen_s(&fp, "CONIN$", "r", stdin) != 0) | ||
result = false; | ||
else | ||
setvbuf(stdin, NULL, _IONBF, 0); | ||
|
||
// Redirect STDOUT if the console has an output handle | ||
if (GetStdHandle(STD_OUTPUT_HANDLE) != INVALID_HANDLE_VALUE) | ||
if (freopen_s(&fp, "CONOUT$", "w", stdout) != 0) | ||
result = false; | ||
else | ||
setvbuf(stdout, NULL, _IONBF, 0); | ||
|
||
// Redirect STDERR if the console has an error handle | ||
if (GetStdHandle(STD_ERROR_HANDLE) != INVALID_HANDLE_VALUE) | ||
if (freopen_s(&fp, "CONOUT$", "w", stderr) != 0) | ||
result = false; | ||
else | ||
setvbuf(stderr, NULL, _IONBF, 0); | ||
|
||
// Make C++ standard streams point to console as well. | ||
std::ios::sync_with_stdio(true); | ||
|
||
// Clear the error state for each of the C++ standard streams. | ||
std::wcout.clear(); | ||
std::cout.clear(); | ||
std::wcerr.clear(); | ||
std::cerr.clear(); | ||
std::wcin.clear(); | ||
std::cin.clear(); | ||
|
||
return result; | ||
} | ||
|
||
void AdjustConsoleBuffer(short minLength) { | ||
// Set the screen buffer to be big enough to scroll some text | ||
CONSOLE_SCREEN_BUFFER_INFO conInfo; | ||
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &conInfo); | ||
if (conInfo.dwSize.Y < minLength) | ||
conInfo.dwSize.Y = minLength; | ||
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), conInfo.dwSize); | ||
} | ||
|
||
bool AttachParentConsole(short minLength) { | ||
bool result = false; | ||
|
||
// Release any current console and redirect IO to NUL | ||
ReleaseConsole(); | ||
|
||
// Attempt to attach to parent process's console | ||
if (AttachConsole(ATTACH_PARENT_PROCESS)) { | ||
AdjustConsoleBuffer(minLength); | ||
result = RedirectConsoleIO(); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include <Windows.h> | ||
|
||
DWORD GetParentPID(); | ||
|
||
bool ReleaseConsole(); | ||
|
||
bool RedirectConsoleIO(); | ||
|
||
bool AttachParentConsole(short minLength); |