14

Where I can disable in Microsoft-Visual-C++ showing console window?

6 Answers6

25

In your console application, goto

Properties > Linker > System 

change SubSystem to Windows

and in your code replace

int _tmain(int argc, _TCHAR* argv[])

with

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)

and add

#include <windows.h>

This should avoid showing a console window in your console application.

Indy9000
  • 8,651
  • 2
  • 32
  • 37
  • 3
    For future googlers: You don't have to change the subsystem. If no subsystem is selected (default for console project), just replacing `_tmain` with `_tWinMain` is enough. – Navin Mar 22 '13 at 20:43
  • What if we're writing a standards-compliant program that doesn't have Windows' weird fake-`main`? – Nic May 01 '18 at 18:52
  • @NicHartley this solution is one way of achieving this. It may not be complete. If your requirements are stricter and nuanced, perhaps you should spend sometime reading relevant win api documents rather than expecting to copy paste an answer. – Indy9000 May 15 '18 at 17:05
  • Does anyone know how to do it in CMake projects? – a_girl Jun 01 '20 at 18:24
3

You could hide it right on startup. I do not know whether this will cause flicker:

HWND hWnd = GetConsoleWindow();
ShowWindow( hWnd, SW_HIDE );
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
3

You can disable console by manipulating pre-compiled EXE subsystem- this way you don't need any change in code as you are working on final product- negative aspect is that you would need to do this every time you recompile the project. You can do it via HEX editor or use free CFF Explorer.

  1. Open EXE via CFF Explorer
  2. Go to Nt Headers>Optional Header
  3. Navigate over Subsystem and on the right side click on Windows Console and select Windows GUI.
  4. Save file, console will no longer appear

You can do binary comparison and find the exact location in raw PE header, then maybe do some automation after-compile in VS

MaKiPL
  • 1,200
  • 8
  • 15
  • This works -- although I had personally luck with the linux HT hex editor. Not easy to automate, though – Vincent Fourmond Jan 26 '22 at 21:35
  • mhm- maybe there is a way. The PE is different for both x32 and x64 executables. I was doing a Python script for ASLR disabling so the flag for GUI/Console should be somewhere earlier- just need to calculate some addresses. MSDN is the key. – MaKiPL Jan 27 '22 at 22:24
3

In my case (vs2022 c++) all i did was:

Modify

int main() {

to

int WinMain() {

and

Properties > Linker > System change SubSystem to Windows

John
  • 31
  • 1
1

For CMake users.

add_executable(${exeName} WIN32)

You'll need to use WinMain instead of main for the entry point symbol.

Underdisc
  • 163
  • 1
  • 10
0

Your question is quite ambiguous, so I'm going to try and answer how I interpreted it... If you don't want a console window, try using a different subsystem. Specifically, you probably want the Windows or Native subsystem rather than the Console subsystem.

Matthew Iselin
  • 10,400
  • 4
  • 51
  • 62