summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.cpp39
-rw-r--r--src/shared/util.cpp43
-rw-r--r--src/shared/util.h14
3 files changed, 58 insertions, 38 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 8b5648c9..65f4bd05 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -732,46 +732,9 @@ int runApplication(MOApplication &application, SingleInstance &instance,
}
}
-class Console
-{
-public:
- Console()
- {
- // open a console
- AllocConsole();
-
- // redirect stdin, stdout and stderr to it
- freopen_s(&m_in, "CONIN$", "r", stdin);
- freopen_s(&m_out, "CONOUT$", "w", stdout);
- freopen_s(&m_err, "CONOUT$", "w", stderr);
- }
-
- ~Console()
- {
- // close redirected handles
- std::fclose(m_err);
- std::fclose(m_out);
- std::fclose(m_in);
-
- // close console
- FreeConsole();
-
- // redirect stdin, stdout and stderr to NUL, don't bother closing the
- // handles
- freopen_s(&m_in, "NUL", "r", stdin);
- freopen_s(&m_out, "NUL", "w", stdout);
- freopen_s(&m_err, "NUL", "w", stderr);
- }
-
-private:
- FILE* m_in = nullptr;
- FILE* m_out = nullptr;
- FILE* m_err = nullptr;
-};
-
int doCoreDump(env::CoreDumpTypes type)
{
- Console c;
+ env::Console c;
// dump
const auto b = env::coredumpOther(type);
diff --git a/src/shared/util.cpp b/src/shared/util.cpp
index 17df3b92..29e52f40 100644
--- a/src/shared/util.cpp
+++ b/src/shared/util.cpp
@@ -441,6 +441,49 @@ private:
};
+Console::Console()
+ : m_hasConsole(false), m_in(nullptr), m_out(nullptr), m_err(nullptr)
+{
+ // open a console
+ if (!AllocConsole()) {
+ // failed, ignore
+ }
+
+ m_hasConsole = true;
+
+ // redirect stdin, stdout and stderr to it
+ freopen_s(&m_in, "CONIN$", "r", stdin);
+ freopen_s(&m_out, "CONOUT$", "w", stdout);
+ freopen_s(&m_err, "CONOUT$", "w", stderr);
+}
+
+Console::~Console()
+{
+ // close redirected handles and redirect standard stream to NUL in case
+ // they're used after this
+
+ if (m_err) {
+ std::fclose(m_err);
+ freopen_s(&m_err, "NUL", "w", stderr);
+ }
+
+ if (m_out) {
+ std::fclose(m_out);
+ freopen_s(&m_out, "NUL", "w", stdout);
+ }
+
+ if (m_in) {
+ std::fclose(m_in);
+ freopen_s(&m_in, "NUL", "r", stdin);
+ }
+
+ // close console
+ if (m_hasConsole) {
+ FreeConsole();
+ }
+}
+
+
Shortcut::Shortcut()
: m_iconIndex(0)
{
diff --git a/src/shared/util.h b/src/shared/util.h
index c4a2ed7d..a5d096ac 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -54,6 +54,20 @@ bool CaseInsensitiveEqual(const std::wstring &lhs, const std::wstring &rhs);
namespace env
{
+class Console
+{
+public:
+ Console();
+ ~Console();
+
+private:
+ bool m_hasConsole;
+ FILE* m_in;
+ FILE* m_out;
+ FILE* m_err;
+};
+
+
// an application shortcut that can be either on the desktop or the start menu
//
class Shortcut