From 5410c4fb3eaa9764d68d273958a2f6f0f91fd80e Mon Sep 17 00:00:00 2001 From: jtgrasb <87095491+jtgrasb@users.noreply.github.com> Date: Wed, 22 Oct 2025 09:52:36 -0600 Subject: [PATCH 1/5] Add function to disable console window --- source/MoorDyn.cpp | 119 +++++++++++++++++++++++++-------------------- source/MoorDyn.h | 8 +++ 2 files changed, 73 insertions(+), 54 deletions(-) diff --git a/source/MoorDyn.cpp b/source/MoorDyn.cpp index 5a43b045..c7fe24e9 100644 --- a/source/MoorDyn.cpp +++ b/source/MoorDyn.cpp @@ -66,6 +66,9 @@ int OwnConsoleWindow = 0; #endif +// Default is false, meaning the console will open +bool disableConsole = false; + /** * @} */ @@ -85,63 +88,65 @@ MoorDyn md_singleton = NULL; int DECLDIR MoorDynInit(const double x[], const double xd[], const char* infilename) { + if (!disableConsoleWindow) { #ifdef WIN32 - // ------------ create console window for messages if none already available - // ----------------- adapted from Andrew S. Tucker, "Adding Console I/O to a - // Win32 GUI App" in Windows Developer Journal, December 1997. source code - // at http://dslweb.nwnexus.com/~ast/dload/guicon.htm - - FILE* fp; - // get pointer to environment variable "PROMPT" (NULL if not in console) - PromptPtr = getenv("PROMPT"); - - // TODO: simplify this to just keep the output parts I need - - HWND consoleWnd = GetConsoleWindow(); - if (!consoleWnd) { - // if not in console, create our own - OwnConsoleWindow = 1; - - // allocate a console for this app - if (AllocConsole()) { - // set the screen buffer to be big enough to let us scroll text - static const WORD MAX_CONSOLE_LINES = 500; - CONSOLE_SCREEN_BUFFER_INFO coninfo; - GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), - &coninfo); - coninfo.dwSize.Y = MAX_CONSOLE_LINES; - SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), - coninfo.dwSize); - - // redirect unbuffered STDOUT to the console - // lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); - lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE); - hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); - fp = _fdopen(hConHandle, "w"); - *stdout = *fp; - setvbuf(stdout, NULL, _IONBF, 0); - - // redirect unbuffered STDERR to the console - lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE); - hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); - fp = _fdopen(hConHandle, "w"); - *stderr = *fp; - setvbuf(stderr, NULL, _IONBF, 0); - - // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog - // point to console as well - std::ios::sync_with_stdio(); - - std::cout << "(MoorDyn-initiated console window)" << std::endl; - } else { - // This is not a likely scenario, but we've run into some situations - // where you can neither get the console nor allocate a console. - // So just fall back to using whatever cout and cerr were before. - std::cout << "AllocConsole failed" << std::endl; - OwnConsoleWindow = 0; + // ------------ create console window for messages if none already available + // ----------------- adapted from Andrew S. Tucker, "Adding Console I/O to a + // Win32 GUI App" in Windows Developer Journal, December 1997. source code + // at http://dslweb.nwnexus.com/~ast/dload/guicon.htm + + FILE* fp; + // get pointer to environment variable "PROMPT" (NULL if not in console) + PromptPtr = getenv("PROMPT"); + + // TODO: simplify this to just keep the output parts I need + + HWND consoleWnd = GetConsoleWindow(); + if (!consoleWnd) { + // if not in console, create our own + OwnConsoleWindow = 1; + + // allocate a console for this app + if (AllocConsole()) { + // set the screen buffer to be big enough to let us scroll text + static const WORD MAX_CONSOLE_LINES = 500; + CONSOLE_SCREEN_BUFFER_INFO coninfo; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), + &coninfo); + coninfo.dwSize.Y = MAX_CONSOLE_LINES; + SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), + coninfo.dwSize); + + // redirect unbuffered STDOUT to the console + // lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); + lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE); + hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); + fp = _fdopen(hConHandle, "w"); + *stdout = *fp; + setvbuf(stdout, NULL, _IONBF, 0); + + // redirect unbuffered STDERR to the console + lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE); + hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); + fp = _fdopen(hConHandle, "w"); + *stderr = *fp; + setvbuf(stderr, NULL, _IONBF, 0); + + // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog + // point to console as well + std::ios::sync_with_stdio(); + + std::cout << "(MoorDyn-initiated console window)" << std::endl; + } else { + // This is not a likely scenario, but we've run into some situations + // where you can neither get the console nor allocate a console. + // So just fall back to using whatever cout and cerr were before. + std::cout << "AllocConsole failed" << std::endl; + OwnConsoleWindow = 0; + } } - } #endif + } MoorDyn instance = MoorDyn_Create(infilename); if (!instance) @@ -291,4 +296,10 @@ AllOutput(double t, double dt) MOORDYN_MSG_LEVEL, "In version 2, AllOutput is automatically called by " "MoorDynInit and MoorDynStep"); +} + +void DECLDIR +SetDisableConsole(bool disable) +{ + disableConsole = disable; } \ No newline at end of file diff --git a/source/MoorDyn.h b/source/MoorDyn.h index 7742ff5b..8b90c53d 100644 --- a/source/MoorDyn.h +++ b/source/MoorDyn.h @@ -152,6 +152,14 @@ GetNodePos(int LineNum, int NodeNum, double pos[3]); void DECLDIR AllOutput(double, double); +/** @brief Set the variable to disable the console window. + * + * Use this function to control display of the console window popup. + * @param disable Set disable to true to disable the console window. + */ +void DECLDIR +SetDisableConsole(bool disable); + /** * @} */ From fc605382bbce387286b7c87ebb651dcee8a633e0 Mon Sep 17 00:00:00 2001 From: jtgrasb <87095491+jtgrasb@users.noreply.github.com> Date: Thu, 23 Oct 2025 07:24:18 -0600 Subject: [PATCH 2/5] fix variable name --- source/MoorDyn.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/MoorDyn.cpp b/source/MoorDyn.cpp index c7fe24e9..48a8a7f3 100644 --- a/source/MoorDyn.cpp +++ b/source/MoorDyn.cpp @@ -88,7 +88,7 @@ MoorDyn md_singleton = NULL; int DECLDIR MoorDynInit(const double x[], const double xd[], const char* infilename) { - if (!disableConsoleWindow) { + if (!disableConsole) { #ifdef WIN32 // ------------ create console window for messages if none already available // ----------------- adapted from Andrew S. Tucker, "Adding Console I/O to a From 5aee6ac96bf272784b18f1e0d253998b07d97fd4 Mon Sep 17 00:00:00 2001 From: jtgrasb <87095491+jtgrasb@users.noreply.github.com> Date: Mon, 27 Oct 2025 15:41:28 -0600 Subject: [PATCH 3/5] own console window = 0 --- source/MoorDyn.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/source/MoorDyn.cpp b/source/MoorDyn.cpp index 48a8a7f3..dfb8966f 100644 --- a/source/MoorDyn.cpp +++ b/source/MoorDyn.cpp @@ -197,6 +197,7 @@ MoorDynClose(void) std::cin.get(); FreeConsole(); } + OwnConsoleWindow = 0; #endif return 0; From 6a10a21b42e502cd89c436c54a59608157c589f1 Mon Sep 17 00:00:00 2001 From: jtgrasb <87095491+jtgrasb@users.noreply.github.com> Date: Tue, 28 Oct 2025 07:37:06 -0600 Subject: [PATCH 4/5] use int instead of bool --- source/MoorDyn.cpp | 6 +++--- source/MoorDyn.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/source/MoorDyn.cpp b/source/MoorDyn.cpp index dfb8966f..f086dfda 100644 --- a/source/MoorDyn.cpp +++ b/source/MoorDyn.cpp @@ -67,7 +67,7 @@ int OwnConsoleWindow = 0; #endif // Default is false, meaning the console will open -bool disableConsole = false; +int disableConsole = 0; /** * @} @@ -88,7 +88,7 @@ MoorDyn md_singleton = NULL; int DECLDIR MoorDynInit(const double x[], const double xd[], const char* infilename) { - if (!disableConsole) { + if (disableConsole == 0) { #ifdef WIN32 // ------------ create console window for messages if none already available // ----------------- adapted from Andrew S. Tucker, "Adding Console I/O to a @@ -300,7 +300,7 @@ AllOutput(double t, double dt) } void DECLDIR -SetDisableConsole(bool disable) +SetDisableConsole(int disable) { disableConsole = disable; } \ No newline at end of file diff --git a/source/MoorDyn.h b/source/MoorDyn.h index 8b90c53d..d40a10cb 100644 --- a/source/MoorDyn.h +++ b/source/MoorDyn.h @@ -155,10 +155,10 @@ AllOutput(double, double); /** @brief Set the variable to disable the console window. * * Use this function to control display of the console window popup. - * @param disable Set disable to true to disable the console window. + * @param disable Set disable to 1 to disable the console window. */ void DECLDIR -SetDisableConsole(bool disable); +SetDisableConsole(int disable); /** * @} From 42b620df3dcd6d4ece7ca46729df4e78a79b687e Mon Sep 17 00:00:00 2001 From: jtgrasb <87095491+jtgrasb@users.noreply.github.com> Date: Wed, 29 Oct 2025 14:38:50 -0600 Subject: [PATCH 5/5] version --- CMakeLists.txt | 4 ++-- source/MoorDyn.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b19c598f..56eb0f4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.10) set(MOORDYN_MAJOR_VERSION 2) -set(MOORDYN_MINOR_VERSION 4) -set(MOORDYN_PATCH_VERSION 1) +set(MOORDYN_MINOR_VERSION 5) +set(MOORDYN_PATCH_VERSION 0) set(MOORDYN_VERSION ${MOORDYN_MAJOR_VERSION}.${MOORDYN_MINOR_VERSION}) project(Moordyn VERSION ${MOORDYN_VERSION}) diff --git a/source/MoorDyn.cpp b/source/MoorDyn.cpp index f086dfda..5291c216 100644 --- a/source/MoorDyn.cpp +++ b/source/MoorDyn.cpp @@ -88,7 +88,7 @@ MoorDyn md_singleton = NULL; int DECLDIR MoorDynInit(const double x[], const double xd[], const char* infilename) { - if (disableConsole == 0) { + if (!disableConsole) { #ifdef WIN32 // ------------ create console window for messages if none already available // ----------------- adapted from Andrew S. Tucker, "Adding Console I/O to a