23.08.2019 cpp

Cross platform C++ WinMain

Under Windows, you need the infamous WinMain, if you don’t want a shell window pop up. On Linux, there is only main(), usually.
This shows you how to do this in a x-platform way.

 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
/**********************************************************************************
* _author  : Domeniko Gentner
* _mail    : code@tuxstash.de
* _license : This file has been placed in the Public Domain (CC0)
***********************************************************************************/

//-------------------------------------------
int main(int argc, char** argv)
//-------------------------------------------
{
    /*
        Example:

        App app;
        app.run(argc, argv);

        This how I do it. 
        I have a class App where
        I handle the arguments 
        in my run() function.
        You could also tokenize 
        them here and pass a list
        to keep your run function clean.
    */
}

#ifdef _WINDOWS
#include "Windows.h"

//----------------------------------------------------
int CALLBACK WinMain(_In_ HINSTANCE hInstance, 
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPSTR lpCmdLine, 
                     _In_ int nCmdShow)
//----------------------------------------------------
{
    return main(__argc, __argv);
}
#endif // #ifdef _WINDOWS

Link to the author's twitter Link to the authors ko-fi page

comments

Characters: 0/1000

gravatar portrait

 Pinned by contact@tuxstash.de

Come join the discussion and write something nice. You will have to confirm your comment by mail, so make sure it is legit and not a throwaway. Only the name part of it will be displayed, so don't worry about spam. If it does not show up after confirming it, it may be considered spam, but I curate them manually, so don't worry. Please read the privacy statement for more.