Skip to content

Commit b69ecbe

Browse files
authored
Merge pull request #333 from jtgrasb/disable_console
Add function to disable console window
2 parents 8048636 + 42b620d commit b69ecbe

File tree

3 files changed

+76
-56
lines changed

3 files changed

+76
-56
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.10)
22
set(MOORDYN_MAJOR_VERSION 2)
3-
set(MOORDYN_MINOR_VERSION 4)
4-
set(MOORDYN_PATCH_VERSION 1)
3+
set(MOORDYN_MINOR_VERSION 5)
4+
set(MOORDYN_PATCH_VERSION 0)
55
set(MOORDYN_VERSION ${MOORDYN_MAJOR_VERSION}.${MOORDYN_MINOR_VERSION})
66
project(Moordyn VERSION ${MOORDYN_VERSION})
77

source/MoorDyn.cpp

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ int OwnConsoleWindow = 0;
6666

6767
#endif
6868

69+
// Default is false, meaning the console will open
70+
int disableConsole = 0;
71+
6972
/**
7073
* @}
7174
*/
@@ -85,63 +88,65 @@ MoorDyn md_singleton = NULL;
8588
int DECLDIR
8689
MoorDynInit(const double x[], const double xd[], const char* infilename)
8790
{
91+
if (!disableConsole) {
8892
#ifdef WIN32
89-
// ------------ create console window for messages if none already available
90-
// ----------------- adapted from Andrew S. Tucker, "Adding Console I/O to a
91-
// Win32 GUI App" in Windows Developer Journal, December 1997. source code
92-
// at http://dslweb.nwnexus.com/~ast/dload/guicon.htm
93-
94-
FILE* fp;
95-
// get pointer to environment variable "PROMPT" (NULL if not in console)
96-
PromptPtr = getenv("PROMPT");
97-
98-
// TODO: simplify this to just keep the output parts I need
99-
100-
HWND consoleWnd = GetConsoleWindow();
101-
if (!consoleWnd) {
102-
// if not in console, create our own
103-
OwnConsoleWindow = 1;
104-
105-
// allocate a console for this app
106-
if (AllocConsole()) {
107-
// set the screen buffer to be big enough to let us scroll text
108-
static const WORD MAX_CONSOLE_LINES = 500;
109-
CONSOLE_SCREEN_BUFFER_INFO coninfo;
110-
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
111-
&coninfo);
112-
coninfo.dwSize.Y = MAX_CONSOLE_LINES;
113-
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
114-
coninfo.dwSize);
115-
116-
// redirect unbuffered STDOUT to the console
117-
// lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
118-
lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
119-
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
120-
fp = _fdopen(hConHandle, "w");
121-
*stdout = *fp;
122-
setvbuf(stdout, NULL, _IONBF, 0);
123-
124-
// redirect unbuffered STDERR to the console
125-
lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE);
126-
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
127-
fp = _fdopen(hConHandle, "w");
128-
*stderr = *fp;
129-
setvbuf(stderr, NULL, _IONBF, 0);
130-
131-
// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
132-
// point to console as well
133-
std::ios::sync_with_stdio();
134-
135-
std::cout << "(MoorDyn-initiated console window)" << std::endl;
136-
} else {
137-
// This is not a likely scenario, but we've run into some situations
138-
// where you can neither get the console nor allocate a console.
139-
// So just fall back to using whatever cout and cerr were before.
140-
std::cout << "AllocConsole failed" << std::endl;
141-
OwnConsoleWindow = 0;
93+
// ------------ create console window for messages if none already available
94+
// ----------------- adapted from Andrew S. Tucker, "Adding Console I/O to a
95+
// Win32 GUI App" in Windows Developer Journal, December 1997. source code
96+
// at http://dslweb.nwnexus.com/~ast/dload/guicon.htm
97+
98+
FILE* fp;
99+
// get pointer to environment variable "PROMPT" (NULL if not in console)
100+
PromptPtr = getenv("PROMPT");
101+
102+
// TODO: simplify this to just keep the output parts I need
103+
104+
HWND consoleWnd = GetConsoleWindow();
105+
if (!consoleWnd) {
106+
// if not in console, create our own
107+
OwnConsoleWindow = 1;
108+
109+
// allocate a console for this app
110+
if (AllocConsole()) {
111+
// set the screen buffer to be big enough to let us scroll text
112+
static const WORD MAX_CONSOLE_LINES = 500;
113+
CONSOLE_SCREEN_BUFFER_INFO coninfo;
114+
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
115+
&coninfo);
116+
coninfo.dwSize.Y = MAX_CONSOLE_LINES;
117+
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
118+
coninfo.dwSize);
119+
120+
// redirect unbuffered STDOUT to the console
121+
// lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
122+
lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
123+
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
124+
fp = _fdopen(hConHandle, "w");
125+
*stdout = *fp;
126+
setvbuf(stdout, NULL, _IONBF, 0);
127+
128+
// redirect unbuffered STDERR to the console
129+
lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE);
130+
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
131+
fp = _fdopen(hConHandle, "w");
132+
*stderr = *fp;
133+
setvbuf(stderr, NULL, _IONBF, 0);
134+
135+
// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
136+
// point to console as well
137+
std::ios::sync_with_stdio();
138+
139+
std::cout << "(MoorDyn-initiated console window)" << std::endl;
140+
} else {
141+
// This is not a likely scenario, but we've run into some situations
142+
// where you can neither get the console nor allocate a console.
143+
// So just fall back to using whatever cout and cerr were before.
144+
std::cout << "AllocConsole failed" << std::endl;
145+
OwnConsoleWindow = 0;
146+
}
142147
}
143-
}
144148
#endif
149+
}
145150

146151
MoorDyn instance = MoorDyn_Create(infilename);
147152
if (!instance)
@@ -192,6 +197,7 @@ MoorDynClose(void)
192197
std::cin.get();
193198
FreeConsole();
194199
}
200+
OwnConsoleWindow = 0;
195201
#endif
196202

197203
return 0;
@@ -291,4 +297,10 @@ AllOutput(double t, double dt)
291297
MOORDYN_MSG_LEVEL,
292298
"In version 2, AllOutput is automatically called by "
293299
"MoorDynInit and MoorDynStep");
300+
}
301+
302+
void DECLDIR
303+
SetDisableConsole(int disable)
304+
{
305+
disableConsole = disable;
294306
}

source/MoorDyn.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ GetNodePos(int LineNum, int NodeNum, double pos[3]);
152152
void DECLDIR
153153
AllOutput(double, double);
154154

155+
/** @brief Set the variable to disable the console window.
156+
*
157+
* Use this function to control display of the console window popup.
158+
* @param disable Set disable to 1 to disable the console window.
159+
*/
160+
void DECLDIR
161+
SetDisableConsole(int disable);
162+
155163
/**
156164
* @}
157165
*/

0 commit comments

Comments
 (0)