• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 990
  • Last Modified:

C++ Hiding Console Window of Application

Hey all. I'm working on a program that runs in the background so I would like the console window to be hidden.

I'm currently using the code below that makes the windows disappear but it pops up for a second before it disappears.

Is there something that I can do to make it not show up at all?
#define _WIN32_WINNT 0x0500 
#include <windows.h> 
#include <iostream> 
int main() 
{ 
HWND hWnd = GetConsoleWindow(); 
ShowWindow( hWnd, SW_HIDE ); 
 
return 0; 
}

Open in new window

0
Cyber-Dragon
Asked:
Cyber-Dragon
  • 4
  • 3
  • 2
  • +1
1 Solution
 
xtravaganCommented:
The program that launches the program can start it minimized. I don't think there is a way to set this on your application programmaticaly.

Have a look at "start" in a command prompt to get an idea or you can do is create a windows shortcut that starts your program minimized which also has support for these options.
0
 
mrjoltcolaCommented:
Write it as a service?
0
 
xtravaganCommented:
Hmm, you don't want the console window ever?

Then why do you create a console application? Create a normal win32 program with no windows attached to it (basically what a service is, however a service need to implement the windows service API and register with the service framework). The largest difference will be that your program starts at WinMain instead of main.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mrjoltcolaCommented:
I agree with xtravagan. Use one of the wizards to generate an app skeleton for you. The Visual Studio 2005 and 2008 app wizards have some good ones to choose from, and if you use the MFC app wizard there is very little code to write.

0
 
6748222Commented:
hm.. in delphi u can make form invisible

alphablend set to True
alphablendvalue to 0

just give u a tip how its can be done easy way
0
 
Cyber-DragonAuthor Commented:
I've been using a blank project so far so I've made everything in it.

I created a sample Win32 Windows Application and all I got was a bunch of code that I don't understand.
// Code.cpp : Defines the entry point for the application.
//
 
#include "stdafx.h"
#include "Code.h"
 
#define MAX_LOADSTRING 100
 
// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name
 
// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
 
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);
 
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;
 
	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_CODE, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);
 
	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}
 
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CODE));
 
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
 
	return (int) msg.wParam;
}
 
 
 
//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
 
	wcex.cbSize = sizeof(WNDCLASSEX);
 
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CODE));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_CODE);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
 
	return RegisterClassEx(&wcex);
}
 
//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
 
   hInst = hInstance; // Store instance handle in our global variable
 
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
 
   if (!hWnd)
   {
      return FALSE;
   }
 
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
 
   return TRUE;
}
 
//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
 
	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
 
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;
 
	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

Open in new window

0
 
xtravaganCommented:
You can scrap it all if you want. All you need is this (and not even that if you don't want to)

I haven't tried it or compiled it, I just wrote it so you might have some fixups to do. But basically that is all you need. As I said, all that has changed is that main has been changed to WinMain.

The while/GetMessage loop is normally a good thing to keep to play nice with the rest of the system. Now just spawn a thread before entering that loop to do your own work.
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
 
    MSG msg;
    int iRes;
 
    while (iRes = GetMessage(&msg, NULL, NULL, NULL)) 
    { 
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
   }
   return 0;
}

Open in new window

0
 
Cyber-DragonAuthor Commented:
@xtravagan

That worked perfectly except I had to take out APIENTRY _t.

I was wondering if you would explain the code or comment it a little so that I understand what it is doing.
0
 
Cyber-DragonAuthor Commented:
Remove the "APIENTRY _t" and replace it with '__stdcall" with a space after it.
0
 
xtravaganCommented:
In windows from the very beginning a dispatch architecture was used. Especially visible in Windows 2/3/3.11. This was a cooperative environment. However when this was chaged in NT and 95 to a preemptive environment the dispatching mechanism was kept as it is a pretty good way to communicate between components, especially if they are in different process spaces but even within the same message passing is a simple architecture.

The while loop above drives this message pump (as it is called). So any message directred towards your application thread or window will be returned in the GetMessage above. TranslateMessage will turn keyboard down events into e.g. VM_CHAR etc. DispatchMessage will do the actual dispatching to the WindowProc that is registered in the currently active (or otherwise registered) window.

If you have a dialog box in your application you should filter all message from GetMessage through IsDialogMessage if it returns 0 pass it to translate/dispatch otherwise do not. This is becaue the dialogbox funtion handles all the dispatching to the proper active control and such.

So basically if your application won't have a windows (Visibile or hidden) you do not really need a message pump (the while). However there will be messages sent to the application's thread that can be of interest to handle so it is always better to keep the message pump, at least I think so. If for no other reason than to handle WM_QUIT.

You can find information about all this in the MSDN documentation, it is vast and comprehensive.

http://msdn.microsoft.com/en-us/library/ms644936(VS.85).aspx


Hope that helps.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now