Compare commits

...

2 Commits

  1. 22
      application/commandlineutils.cpp
  2. 4
      application/commandlineutils.h

22
application/commandlineutils.cpp

@ -37,9 +37,27 @@ bool confirmPrompt(const char *message, Response defaultResponse)
}
#ifdef PLATFORM_WINDOWS
/*!
* \brief Closes stdout, stdin and stderr and stops the console.
* \remarks Interanlly used by startConsole() to close the console when the application exits.
*/
void stopConsole()
{
fclose(stdout);
fclose(stdin);
fclose(stderr);
if(auto *consoleWindow = GetConsoleWindow()) {
PostMessage(consoleWindow, WM_KEYUP, VK_RETURN, 0);
FreeConsole();
}
}
/*!
* \brief Starts the console and sets the console output code page to UTF-8 if this is configured.
* \remarks This method is only available on Windows and used to start a console from a GUI application.
* \remarks
* - only available under Windows
* - used to start a console from a GUI application
* - closes the console automatically when the application exists
*/
void startConsole()
{
@ -72,6 +90,8 @@ void startConsole()
#endif
// sync
ios::sync_with_stdio(true);
// ensure the console prompt is shown again when app terminates
atexit(stopConsole);
}
/*!

4
application/commandlineutils.h

@ -26,15 +26,19 @@ bool CPP_UTILITIES_EXPORT confirmPrompt(const char *message, Response defaultRes
#ifdef PLATFORM_WINDOWS
void CPP_UTILITIES_EXPORT startConsole();
void CPP_UTILITIES_EXPORT stopConsole();
std::pair<std::vector<std::unique_ptr<char[]> >, std::vector<char *> > CPP_UTILITIES_EXPORT convertArgsToUtf8();
# define CMD_UTILS_START_CONSOLE \
::ApplicationUtilities::startConsole();
# define CMD_UTILS_STOP_CONSOLE \
::ApplicationUtilities::stopConsole();
# define CMD_UTILS_CONVERT_ARGS_TO_UTF8 \
auto utf8Args = ::ApplicationUtilities::convertArgsToUtf8(); \
argv = utf8Args.second.data(); \
argc = static_cast<int>(utf8Args.second.size());
#else
# define CMD_UTILS_START_CONSOLE
# define CMD_UTILS_STOP_CONSOLE
# define CMD_UTILS_CONVERT_ARGS_TO_UTF8
#endif

Loading…
Cancel
Save