Hiding Console Window In Runtime

I am working on a project in which there is a login module. This login module is a separate application whereas rest of the program is also divided into different applications. I need to write the backbone of the project i.e the kernel in C++. My question is, how do I execute other applications while running my console in the background, hidden and invisible to the user using other modules in GUI...

I shall be obliged if anyone could help me. Thanks.
Last edited on
You have choices. There is always FreeConsole() (and AllocConsole()), but in a C++ program those are problematic to keep hooked up to the standard streams.

You can also hide and show the window.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

// I'm using Vista ATM (it's evil), but GCC comes assuming the worst.
// If you haven't patched your compiler to agree with the actual version
// of the Windows OS, then you will need to make sure you do it like this.
//
#if       _WIN32_WINNT < 0x0500
  #undef  _WIN32_WINNT
  #define _WIN32_WINNT   0x0500
#endif
#include <windows.h>

// Here's some fun timer stuff for the user.
// (Notice how he won't see it work when the
//  console is hidden, but it will still work.)
void timeout()
  {
  for (int cntr = 3; cntr > 0; cntr--)
    {
    cout << "\r" << cntr << flush;
    Sleep( 1000 );
    }
  cout << "\r" << flush;
  }

// Demonstrate some fun stuff.
// Notice how hiding the console window causes it to disappear from
// the Windows task bar. If you only want to make it minimize, use
// SW_MINIMIZE instead of SW_HIDE.
//
int main()
  {
  cout << "Preparing to hide the console window\n";
  timeout();
  ShowWindow( GetConsoleWindow(), SW_HIDE );

  cout << "Preparing to show the console window\n";
  timeout();
  ShowWindow( GetConsoleWindow(), SW_RESTORE );

  cout << "All done!\n";
  return 0;
  }

Hope this helps.
Last edited on
No no Duoas! Thanks a lot for your help buddy but I think you didn't get my question. I don't need to hide my console for some time or perform fancy tricks with the user... I need to write a kernel for a whole suite of application. Here are my motives.

- The kernel is able to remain in background without showing to the user at all... (PRIORITY-1)

- The kernel must be able to initialize the application modules as needed...

(For example let's say there is a program built for taking and validating the username and password... The kernel initializes it, and then the specified program module takes the control of system... It takes the username, the password and matches it from the dtabase on its own and if the username and password are correct, it returns "SUCCESS" to kernel and exits.)

- The kernel must be able to recognize if the previous application ended successfully and then execute the next application.

(Say the next application is about management of usernames and passwords... The kernel must first recognize that the login application ended successfully and then start the management application... If the kernel is not able to cinfigure the successfull ending of the previous application, a user might be able to hack through the login module of the project easily... He just has to exit the login application and the next module starts... :(!!! So we need to identify if the login application was run till the end and it had validated the passwords and returned them valid befor it exited... Woo-Hoo)


I know buddy that is "MY" project and I am supposed to work my brains out on this... But you can't imagine how damn long have I been thinking over it before asking you people... But now the time is short and I depend on this project... Please help me if you can. Muito obrigado!
Many many thanks senhore!
Oh, I get it.

Use CreateProcess() to start the child processes. Then use any of the wait functions on the returned process handle. WaitForSingleObject() should do.

Sorry about that. :-\
Muito obrigado

is that portuguese... sorry off topic
@ Duoas

You are definitely doing a great work here, buddy...
And please! There is nothing for
Sorry about that. :-\
...
Your little program helped me understand a new implementation of built-in functions of console...
Except for one problem... GetConsooleWindow() is not recognized by my compiler. It is a Visual Studio (I have tried in MSVC-6 and VS-2005 both)... Is there a solution to this problem...?

Also please consider that I am a complete newbie, a 1st year student for Software Designing, you might consider. I have studied the C++ course well. However, still there are some areas that the instructors haven't even touched yet they are being considered for grading. You know, I am in a turmoil.

You explained that my problem can be solved by CreatProcess()... Is that a built-in function? WaitForSingleObject()... Is that built in, too? Or are you referring to defining a class with following manipulators?

Please Douas, will you be generous enough to give me a practical example of what you have explained and what you think could solve my problem mentioned in previous post... thanks a lot buddy! You really are doing great work here; un complimento :P :)
Last edited on
@ hypercube1
Yep...
Lines 8 through 12 of the example above fix things so that you can use GetConsoleWindow().

Most of the functions work just like you see them in the documentation. Here's an example
http://www.cplusplus.com/forum/beginner/1988/page3.html#msg14102

Lines 34 through 51 ought to work for you. Be sure to read the docs:
http://www.google.com/search?btnI=1&q=msdn+CreateProcess
http://www.google.com/search?btnI=1&q=msdn+WaitForSingleObject

When your application activates, just make sure to use the
1 ShowWindow( GetConsoleWindow(), SW_HIDE );
trick to make your program disappear, then use the CreateProcess() and wait function to run the programs you want.

Win32 programming is not usually part of the basic courses...

Good luck!
Hey Douas

I am really sorry for bothering you again and again... But the GetConsoleWindow() is again not working... Somehow, the compiler returns the same error... What might be the problem here?

Secondly, you are right buddy, Win32 was not actually part of our course! But it was me, who doomed myself... I thought I could do it, so I submitted the abstract of the project as being a Windows Win32 Application with kernel built in C++ and the components built in application of my choice... I know it's my fault... But on second thoughts, why the hell not? While you guys here are helping me out I may even submit the abstract of an OS... LOLxxxxx...

:) ;) :P Dun Worry, I won't!

Thanks a lot buddy!
Last edited on
I think you could just create a new WIN32 project (I assume you are using VC), and then remove the default code that displays the window. Then you'll have a program that can be run and won't show anything. You can use that program to display password boxes, start other programs or whatever.
The GetConsoleWindow() function only exists on Windows 2000 and higher kernels. Are you programming on 98 or ME or something like that? If you aren't, make sure your Windows API (header and lib files) are up-to-date.

One way to cover all possibilities would be to initialize a function pointer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <ctime>
#include <windows.h>

#if _WIN32_WINNT < 0x0501

typedef HWND WINAPI (*PGetConsoleWindow)();
PGetConsoleWindow GetConsoleWindow = NULL;

HWND WINAPI MyGetConsoleWindow()
  {
  HWND result;
  char save_title[ 1000 ];
  char temp_title[ 50 ];
  time_t t = time( NULL );
  lstrcpyA( temp_title, "madmaxsantana " );
  lstrcatA( temp_title, ctime( &t ) );

  GetConsoleTitleA( save_title, sizeof( save_title ) );
  SetConsoleTitleA( temp_title );
  result = FindWindowA( NULL, temp_title );
  SetConsoleTitleA( save_title );

  return result;
  }

struct MyInitializeConsole
  {
  MyInitializeConsole()
    {
    HINSTANCE kernel32 = LoadLibraryA( "Kernel32.dll" );
    GetConsoleWindow =
      (PGetConsoleWindow)GetProcAddress( kernel32, "GetConsoleWindow" );
    if (GetConsoleWindow == NULL)
      GetConsoleWindow = &MyGetConsoleWindow;
    FreeLibrary( kernel32 );
    }
  }
  MyInitializedConsole;

#endif  

Hope this helps.

[edit] Fixed a few omissions that could cause problems when compiling with UNICODE. Now all the appropriate functions explicitly use the ANSI/ASCII versions where appropriate. [/edit]
Last edited on
Oh thanks Douas

You said I must check if my header files and source libraries are all updated! I just downloaded the latest one from the web and the function is working... Thanks a lot buddy!
Didn't had to use it, but Thanks for the last post as well!

Do you know which header the GetConsoleWindow() function is declared in?
It is in wincon.h.

You don't need to use that header directly. Just #include <windows.h> and you'll have it. See lines 8 through 12 of my first post above.
In visual C++ 2005 take out the first few comments right at the top and compile some programs.
If you happen to create a virus reply back.
What?
Yes exactly - what?
Topic archived. No new replies allowed.