1

How I can hide the console during c++ program run-time?

My compiler : MinGw (g++)

I tried a lot of things but they didn't work:

  1. Add -mwindows command
  2. ShowWindow(GetConsoleWindow(), SW_HIDE);
  3. WinMain(...)

Code with problem is here (from comment):

#include <iostream> 
#include <Windows.h> 

int main() { 
  std::cout << "Recompiling compile app..."; 
  system("taskkill /IM Compile.exe"); 
  system("g++ Compile.cpp -o Compile.exe"); 
  system("Start Compile.exe"); return 0; 
}

How I can resolve my problem?

Клаус Шварц
  • 3,158
  • 28
  • 44
Carl
  • 47
  • 7

2 Answers2

2

Seems like your problem is arising from calls to system function, which runs with console window by default. If you need at least one console window for your own programm, this example will help you. If you don't need any output, just uncomment the line in the example.

#include <iostream> 
#include <Windows.h> 

int main() { 
  // Uncomment next line if you don't need output at all
  // FreeConsole();

  std::cout << "Recompiling compile app..."; 
  WinExec("taskkill /IM Compile.exe", SW_HIDE); 
  WinExec("g++ Compile.cpp -o Compile.exe", SW_HIDE); 
  WinExec("C:\\Path\\To\\Compile.exe", SW_HIDE); 

  return 0; 
}

You can combine it with my old answer to achieve desired result.


Old answer (still might be helpful for someone);

This question had been answered here and here already, assuming you are talking about compiling C++ app for Windows.

Basically first answer will help you to compile windowed application without a window, and the second one is a console app which will immediately hide the console window, though it will flash on the screen for a second or so.

Клаус Шварц
  • 3,158
  • 28
  • 44
  • The first hide perfectly the console of the program but it open three other cmd windows, and for the seconde I won't see the console even for 1 second – Carl Aug 14 '17 at 19:04
  • It is hard to help you without seeing sources. Please post listing of the cpp file you are compiling as well as commands you issue to compile it with mingw. – Клаус Шварц Aug 14 '17 at 19:17
  • `code` #include #include int main() { std::cout << "Recompiling compile app..."; system("taskkill /IM Compile.exe"); system("g++ Compile.cpp -o Compile.exe"); system("Start Compile.exe"); return 0; } `code` command : g++ Recompiler.cpp -o Recompiler.exe – Carl Aug 14 '17 at 19:34
  • You see three other cmd windows because you launch 3 system processes one by one: `system("taskkill /IM Compile.exe"); system("g++ Compile.cpp -o Compile.exe"); system("Start Compile.exe");` :) – Клаус Шварц Aug 14 '17 at 20:07
  • It is possible to remove those window ? – Carl Aug 14 '17 at 20:12
  • Do you want to run Compile.exe with this command? If yes, try something like this: `WinExec("C:\\Path\\To\\Executable\\Compile.exe", SW_HIDE);` – Клаус Шварц Aug 14 '17 at 20:41
  • Yes and I have the same issue – Carl Aug 14 '17 at 20:50
  • Try just `WinExec("Compile.exe", SW_HIDE);`. If it won't work, show me full path to your compile.exe. – Клаус Шварц Aug 14 '17 at 20:53
  • It doesn't work : G:\WORK\*****\TEMP - TEST\**** Test Programme\Auto compiling\Compile.exe (it's in the same folder of the Recompiler program | and the * are my privacy) – Carl Aug 14 '17 at 21:01
  • I found why. The program Compile.exe is currently runing somewhere in the computer so i can't compile it (I had reactivate the console to see the error message). I will retest it tomorrow because i need to restart my PC and it's late for me (I'm in France). – Carl Aug 14 '17 at 21:15
  • Here is an example of the path with spaces from official docs ( https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms687393(v=vs.85).aspx ) : `WinExec("\"C:\\Program Files\\MyApp.exe\" -L -S", SW_HIDE)`. You need to add quotes around. Good luck. – Клаус Шварц Aug 14 '17 at 21:19
1

This works for me (FreeConsole MSDN)

#include <Windows.h>
// Other includes

int main(void)
{
    FreeConsole();

    // Do whatever you want here
    for (int i = 0; i < 10000; i++)
        std::cout << "You cant see me!" << std::endl;

    return 0;
}
kocica
  • 6,412
  • 2
  • 14
  • 35
  • It doesnt work for you becouse program ends. Ill modify it so you will see the result. – kocica Aug 14 '17 at 19:08
  • Tried what you said but it doesn't word too (it's exactly the same issue with the commend: -mvwindows: it perfectly the console of the program but it open three other cmd windows), and i want than my program ends – Carl Aug 14 '17 at 19:16