Tech Support > Microsoft Windows > Development Resources > c++ win32 console app with configurable displaying of console window
c++ win32 console app with configurable displaying of console window
Posted by Damo on November 18th, 2005


Hi,
This question has been asked before but what I'm doing is a slight
twist on all of the questions/answers I've seen so far. I developing a
c++ console app (has to be a console app, a wins service is no use to
me) - I don't want the console window to be displayed when the app is
running - I've seen on several posts how this can be done by changing a
projects linker properties to the following /link /subsystem:windows
/entry:mainCRTStartup. This does the job - however, I also want to be
able to occasionally turn back on displaying of the console window at
app startup time using a reg setting or some such thing. Is there
anyway that I can programmatically switch on/off displaying of the
console window?
I have tried using FreeConsole() but the problem with this is that the
console window flashes on the screen for a second and then disappears -
I don't want that flashing - I'd like the console window to either
appear or not appear depending on my reg setting.

Thanks,
Damo

Posted by Vincent Fatica on November 18th, 2005


How about you continue using subsystem:windows, get a console for the first
time with AllocConsole(), and thereafter, toggle its visibility with

ShowWindow(GetConsoleWindow(), SW_HIDE);
or
ShowWindow(GetConsoleWindow(), SW_SHOW);

- Vince

On 18 Nov 2005 05:48:19 -0800, "Damo" <damien.obrien@gmail.com> wrote:

- Vince

Posted by zcsizmadia@gmail.com on November 18th, 2005


Use /link /subsystem:windows /entry:mainCRTStartup with the following
sample code:

#include <windows.h>
#include <stdio.h>
#include <iostream.h>

BOOL CreateConsole()
{
// Create console
if (!AllocConsole())
return FALSE;

// Redirect printf, scanf to console
freopen("CONIN$", "rb", stdin);
freopen("CONOUT$", "wb", stdout);
freopen("CONOUT$", "wb", stderr);

// Redirect cout, cin to console
ios::sync_with_stdio();
::setvbuf(stdin, NULL, _IONBF, 0);
::setvbuf(stdout, NULL, _IONBF, 0);
::setvbuf(stderr, NULL, _IONBF, 0);

return TRUE;
}

int main(int argc, char* argv[])
{
// Create console if you want
if (1)
CreateConsole();

// If console is not created, nothing will happen
printf("Test printf\n");
cout << "Test cout" << endl;

return 0;
}

Zoltan

Posted by Damo on November 21st, 2005


Thanks guys - both suggestions worked. That extra bit of code to
redirect printf and cout to console is definitely needed if you intend
writing to the console window.

Cheers,
Damo


Similar Posts