Create an Application without a Window

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;
}

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;
} 

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.


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.

Tags:

C++

C

Winapi