How would you program a C/C++ application that could run without opening a window or console?
-
1Related thread: http://stackoverflow.com/questions/13051412/how-to-hide-the-console-window-in-a-win32-project-using-visual-studio-2010 – BuvinJ Jun 24 '15 at 19:59
7 Answers
When you write a WinMain program, you automatically get the /SUBSYSTEM option to be windows in the compiler. (Assuming you use Visual Studio). For any other compiler a similar option might be present but the flag name might be different.
This causes the compiler to create an entry in the executable file format (PE format) that marks the executable as a windows executable.
Once this information is present in the executable, the system loader that starts the program will treat your binary as a windows executable and not a console program and therefore it does not cause console windows to automatically open when it runs.
But a windows program need not create any windows if it need not want to, much like all those programs and services that you see running in the taskbar, but do not see any corresponding windows for them. This can also happen if you create a window but opt not to show it.
All you need to do, to achieve all this is,
#include <Windows.h>
int WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int cmdShow)
{
/* do your stuff here. If you return from this function the program ends */
}
The reason you require a WinMain itself is that once you mark the subsystem as Windows, the linker assumes that your entry point function (which is called after the program loads and the C Run TIme library initializes) will be WinMain and not main. If you do not provide a WinMain in such a program you will get an un-resolved symbol error during the linking process.

- 4,321
- 1
- 21
- 18
-
I have a similar requirement where i use a timer. i want the program to run without stopping. In this program a timer has been set. so how do i do it. ? – Jayapal Chandran Nov 05 '12 at 12:53
-
2
-
An actual todo in Visual Studio 2017 for example: 1) open visual studio 2) File->New->Project 3) Visual C++ -> General 4) Choose Empty Project 5) Cut and paste the snippet above. Add Sleep(60000); so that program will wait for you. Compile program to produce program.exe. Double-click program.exe, and the program stays alive for 60 seconds. Check Task Manager and see program.exe there. – daparic Jul 05 '18 at 13:55
In windows:
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// <-- Program logic here
return 0;
}
Be sure to use the /SUBSYSTEM linker switch as mentioned by Adam Mitz.
On other platforms:
int main(int argc, char**argv)
{
// <-- Program logic here
return 0;
}

- 339,232
- 124
- 596
- 636
-
Two complaints: WinMain is unnecessary if you don't include windows.h; and return 0; is optional in main. – coppro Oct 22 '08 at 02:13
-
2You wan to return 0 so you can signify success to programs calling it. – Brian R. Bondy Oct 22 '08 at 02:54
-
If you make a console application and have int main for windows instead you will have a console window popup when you double click the exe. – Brian R. Bondy Oct 22 '08 at 02:55
-
I think that's actually the /SUBSYSTEM linker switch that determines if there's a console window. – Adam Mitz Oct 22 '08 at 03:26
-
Thanks Adam. I think it's that switch that determines whether to use main or WinMain. I added it to my answer. – Brian R. Bondy Oct 22 '08 at 04:17
-
OK, other answers are mentioning it too so this is well covered. I'm sure most users are only familiar with creating a "Win32 Console project" in Visual Studio. – Adam Mitz Oct 22 '08 at 05:56
-
3@Brian - main() returns 0 by default if no return value is specified. It's a crazy special case that only applies to main(). – Aaron Oct 22 '08 at 17:28
If you have a need to contiguously run your program without having console or window you might find useful deamon on *NIX or services on Windows, this .NET example if you need plain win32 just google a little bit for sample.
Since your question tagged as win32 i assume that services are more relevant for you.

- 3,104
- 3
- 23
- 30
This also processes messages:
#include <windows.h>
#include <stdio.h>
int CALLBACK WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
MSG msg;
DWORD curThreadId;
curThreadId = GetCurrentThreadId();
// Send messages to self:
PostThreadMessage(curThreadId, WM_USER, 1, 2);
PostThreadMessage(curThreadId, WM_USER+1, 3, 4);
PostThreadMessage(curThreadId, WM_USER+2, 5, 6);
PostThreadMessage(curThreadId, WM_USER+3, 7, 8);
PostThreadMessage(curThreadId, WM_QUIT, 9, 10);
while (GetMessage(&msg, NULL, 0, 0)) {
printf("message: %d; wParam: %d; lParam: %d\n", msg.message, msg.wParam, msg.lParam);
}
return (int) msg.wParam;
}

- 21
- 1
In Visual Studio Express 2010 after setting the subsystem to windows (as suggested by user17224), alternatively to changing the main to WinMain (as suggested by user17224 and Brian R. Bondy), one can set the entry function to main in properties, linker, advanced, entry point: just type main in the text box.

- 19
- 2
-
I tried 'main' in Visual Studio 2017 and with every combinations of /SUBSYSTEM liker options but to no avail. However, I find 'WinMain' entry point to be the answer here instead of 'main'. – daparic Jul 05 '18 at 14:18
Use Visual Studio wizard to create the Win32 Application. But don't create the window i.e., you remove the window creation function. Alternatively we can create Win Service application.

- 4,743
- 7
- 33
- 43
If you are using MSVC or Visual Studio just use the new Project Wizard and select the Console Application.

- 10,370
- 1
- 33
- 49
-
console application will always open up a console window when it runs – computinglife Oct 22 '08 at 05:46
-
1Are you sure you know what you are talking about? http://en.wikipedia.org/wiki/Win32_console I think not!!! Last time I looked a console app was a non gui win32 application!!! – jussij Oct 22 '08 at 13:20
-
1jussij - Correct. it's a CLUI application. and in order to interact with that CLUI, windows creates a CL GUI for you. – Aaron Oct 22 '08 at 17:29
-
@jussji - You are right - the console program does not have any "windows" but try double clicking a console program from explorer & the OS will automatically create a console window for running the console program. The Op did not seem to want neither a real window nor a console. Hence my comment. – computinglife Oct 23 '08 at 14:48
-
-
-
is there any consensus here? Can we create a "console app" that does not get a (dos) windows from the OS? – Xan-Kun Clark-Davis Jul 19 '23 at 08:25
-