MinGW, build GUI application with console

前端 未结 4 1717
情深已故
情深已故 2021-02-02 04:19

I\'m using MinGW to build my application on Windows. When compiling and linking, the option \"-mwindows\" is put in command line to have Win32 API functions.

To be more

相关标签:
4条回答
  • 2021-02-02 04:36

    You need to manually capture hInstance and nCmdShow (WinMain arguments). You can use the following C functions to do this:

    HINSTANCE GetHInstance( ) {
       return (HINSTANCE) GetModuleHandleW(NULL);
    }
        
    int GetNCmdShow() {
       STARTUPINFOW startupInfo;
       GetStartupInfoW(&startupInfo);
       if ((startupInfo.dwFlags & STARTF_USESHOWWINDOW) != 0) {
          return startupInfo.wShowWindow;
       }
       return SW_SHOWDEFAULT;
    }
    
    0 讨论(0)
  • 2021-02-02 04:49

    The -mconsole switch is used to specify that you want to target the console subsystem. You really do want to do that to ensure that your process connects to the existing console if started from a console app. For example, suppose you do go down your route of targeting the GUI subsystem, and then calling AllocConsole(), as per your own answer. Then you'll find your app shows a brand new console rather than using the existing one when started from another console app, e.g. cmd.exe.

    If you need to use other libraries, then you are free to add them on command line using -l. There's nothing special about a console app that means that it cannot link to any Win32 API function. It's just that the default set of libraries associated with -mconsole is missing some of the libraries that you want.

    On the other hand, you can use both -mconsole and -mwindows when you build your app. They are not mutually exclusive.

    gcc -mconsole -mwindows main.c
    

    This produces an application that targets the console subsystem. And you get the standard -mwindows set of Win32 libraries automatically linked. It's probably the simplest way to achieve your goal.

    0 讨论(0)
  • 2021-02-02 04:54

    I have no evidence for this answer, only a bit of experiments that were successful. If I have a hello app, like this:

    #include <stdio.h>
    #include <windows.h>
    
    int main(void)
    {
        puts("hi");
        MessageBox(NULL, "test", "test", NULL);
        GetStockObject(0);
        return 0;
    }
    

    I cannot compile it with -mconsole, because linker complains about GetStockObject. But when I add the necessary library with -lgdi32 switch on my command line, the app compiles and executes cleanly. Maybe this is the way to keep both console and gdi. This is the command line:

    gcc -mconsole test_gdi.c -lgdi32
    
    0 讨论(0)
  • 2021-02-02 04:54

    I found the answer. As taken from Using STDIN with an AllocConsole()

    AllocConsole();
    freopen("CONIN$", "r",stdin); 
    freopen("CONOUT$","w",stdout); 
    freopen("CONOUT$","w",stderr);  
    

    It works like magic!

    Reference for 'freopen': http://www.cplusplus.com/reference/clibrary/cstdio/freopen/

    0 讨论(0)
提交回复
热议问题