tags:

views:

822

answers:

5

How would you program a C/C++ application that could run without opening a window or console?

+5  A: 

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;
}
Brian R. Bondy
Two complaints: WinMain is unnecessary if you don't include windows.h; and return 0; is optional in main.
coppro
You wan to return 0 so you can signify success to programs calling it.
Brian R. Bondy
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
I think that's actually the /SUBSYSTEM linker switch that determines if there's a console window.
Adam Mitz
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
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
@Brian - main() returns 0 by default if no return value is specified. It's a crazy special case that only applies to main().
Aaron
+9  A: 

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.

A: 

If you are using MSVC or Visual Studio just use the new Project Wizard and select the Console Application.

jussij
console application will always open up a console window when it runs
Are you sure you know what you are talking about?http://en.wikipedia.org/wiki/Win32_consoleI think not!!! Last time I looked a console app was a non gui win32 application!!!
jussij
jussij - Correct. it's a CLUI application. and in order to interact with that CLUI, windows creates a CL GUI for you.
Aaron
+1  A: 

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.

Ilya
A: 

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.

Vinay