Carl
Carl

Reputation: 47

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

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?

Upvotes: 1

Views: 2982

Answers (2)

Клаус Шварц
Клаус Шварц

Reputation: 3268

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.

Upvotes: 2

kocica
kocica

Reputation: 6467

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;
}

Upvotes: 1

Related Questions