Hide console of Windows Application

109,785

Solution 1

It sounds like your linker configuration is incorrect. Right-click the project, Properties, Linker, System, SubSystem setting. Make sure "Windows" is selected, not "Console".

And, change main() to WinMain().

Solution 2

In the project build linker options set

/SUBSYSTEM:windows
/ENTRY:mainCRTStartup

Or use the following #pragma in the source file with the int main(...)

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

Solution 3

You can get rid of the console by calling:

FreeConsole();

Solution 4

i use that method and it worked

HWND hwnd = GetConsoleWindow();
ShowWindow(hwnd, 0);

Solution 5

Next solution ;)

Env: WixXP x64, msvs 2008, Qt v4.5.3

  1. Set Projects settings/Configuration properties/Linker/System/SubSystem = Windows (/SUBSYSTEM:WINDOWS)

    But For x64 there is linker error: LNK2019: unresolved external symbol _WinMain referenced in function _WinMainCRTStartup" To avoid it

  2. Replace the following code:

    int main(int argc, char *argv[])
    {
         QApplication app(argc, argv);
         // your code*
    }
    

    by

    int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
    {
        int argc = 0;
        QApplication app( argc, 0 );
     }
    

It works fine for both - Win32 and x64 platforms.

Share:
109,785
ufukgun
Author by

ufukgun

:) c++ java openGL delta3D osg glsl qt equalizer openAL ...

Updated on February 12, 2020

Comments

  • ufukgun
    ufukgun over 4 years

    I have a Qt application, and when I run this application, there is a console opening behind it. In development it is nice because i see debug outputs on the console, but when I want to give this executable to the customer there should be no console window. how do I hide it?

    (I am using Visual Studio 2008)

  • Veeti
    Veeti over 14 years
    The actual Visual Studio project is created using the configuration parameters in the QMake project file, so this could very well be the cause.
  • Andy M
    Andy M over 14 years
    Erm, in the VCProj properties, maybe by going in Links Edition, System and finally Subsystem... Try putting the value "Windows (/SUBSYSTEM:WINDOWS)"... I'm not really sure you can do it like that tho...
  • Andy M
    Andy M over 14 years
    I quickly tested in one of my application and for some obscure reason, i need to add the following lib : c:\...\Qt\4.6.0-vs2008\lib\qtmain.lib in VCProj Properties-> Links Edition -> Entry -> Additionnal Dependencies
  • Andy M
    Andy M over 14 years
    And it looks like it's the final solution :) lists.trolltech.com/qt-interest/2005-12/thread00170-0.html
  • ufukgun
    ufukgun over 14 years
    is there a difference between main() and WinMain()?
  • datenwolf
    datenwolf almost 13 years
    This method will prevent the program from getting parameters from the command line. Very, very bad idea. -1. If you want to do it correctly, tokenize the string you receive fin the third parameter of WinMain. And yes, there are plenty of Windows programs that interpret command line parameters. For example the "Printing" context menu in Windows Explorer starts a program with command line parameters.
  • Cray
    Cray over 12 years
    Imo the best and to-the-point answer, dealing with both the subsystem part and the different entry points part. Small unicode hint: if you compile with unicode and have wmain as opposed to main, comment should be "/ENTRY:wmainCRTStartup".
  • Cray
    Cray over 12 years
    datenwolf, They are still there using __argv and __argc macros (at least in MSVC) or GetCommandLine/CommandLineToArgv winapi function.
  • Jan Turoň
    Jan Turoň over 12 years
    You may also set the entry point of the project to standard main() in Properties -> Linker -> Advanced -> Entry point
  • datenwolf
    datenwolf about 12 years
    @Cray: WCHAR is not Unicode. If you're using real Unicode with the appropriate libraries like ICU, normal mainCRTStrartup works just fine.
  • Cray
    Cray about 12 years
    Sure, however I was talking about the compiler option (ie, "compile with unicode"). They are actually calling it "Use Unicode Character Set".
  • Guy Joel McLean
    Guy Joel McLean about 11 years
    This solution worked for me and my OpenGL application. I started the project months ago as a console application, using the console window for early debugging. Starting the project again as a windows application and pasting all of the files over, as well as re-entering all of the library dependencies just seemed tedious.
  • datenwolf
    datenwolf about 11 years
    @GuyJoelMcLean: Pro-Tipp: If you need a console later on for debugging you can use AllocConsole msdn.microsoft.com/en-us/library/windows/desktop/…
  • Tomáš Zato
    Tomáš Zato over 9 years
    The problem is that when I turn the program in Windows it doesn't terminate, even if I fix the main/WinMain issue.
  • nwp
    nwp over 9 years
    If you start the program from command line the command line will disappear. FreeConsole does not have that issue.
  • John Neuhaus
    John Neuhaus almost 9 years
    In VS 2010 the pragma didn't work for me. I had to set the two settings through project properties, removed _CONSOLE from the preprocessor. Then I rebuilt, and it worked perfectly.
  • Nic
    Nic about 6 years
    @JanTuroň How? What option do we put in that field? It's a string field and I've tried a wide variety of solutions, none of which work.
  • Jan Turoň
    Jan Turoň about 6 years
    @NicHartley back then in 2008 it should be a selectbox. Perharps there are other settings in config. Could you ask another question and provide step by step setup to reproduce that?
  • alexpanter
    alexpanter over 4 years
    This should be the accepted answer. E.g. if you are developing with CMake on Windows, this is the only option. Also, it is portable to other windowing systems, such as GLFW, SDL, ...
  • Noah.Ehrnstrom
    Noah.Ehrnstrom almost 4 years
    This helped out very much!
  • Superior
    Superior over 3 years
    You'll have to #include <Windows.h> for that
  • Tayab
    Tayab about 3 years
    This solved the issue for me. Many thank :))
  • Radim Cernej
    Radim Cernej over 2 years
    Worked for me. Qt6, Win10, MSVC2019, amd64 build.