Skip to content

Commit 5410c4f

Browse files
committed
Add function to disable console window
1 parent 8048636 commit 5410c4f

File tree

2 files changed

+73
-54
lines changed

2 files changed

+73
-54
lines changed

source/MoorDyn.cpp

Lines changed: 65 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+
bool disableConsole = false;
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 (!disableConsoleWindow) {
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)
@@ -291,4 +296,10 @@ AllOutput(double t, double dt)
291296
MOORDYN_MSG_LEVEL,
292297
"In version 2, AllOutput is automatically called by "
293298
"MoorDynInit and MoorDynStep");
299+
}
300+
301+
void DECLDIR
302+
SetDisableConsole(bool disable)
303+
{
304+
disableConsole = disable;
294305
}

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 true to disable the console window.
159+
*/
160+
void DECLDIR
161+
SetDisableConsole(bool disable);
162+
155163
/**
156164
* @}
157165
*/

0 commit comments

Comments
 (0)