0

Is it possible to close the console without shutting down the program?

For example to open a custom created console or if I want to open a HWND and don't want the console in the background.

Yes I know I could use the WINAPI and WinMain but I want my program to be also executable on Linux-Systems (not with an HWND here).

Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
Rafiwui
  • 544
  • 6
  • 19

4 Answers4

6

C++ has no idea of the console. If you need to manipulate the console then you are going to need to use the API for the system you are running on to do that.

To do that on your own you could provide a common interface and then using the preprocessor to conditionally compile the API calls you need to make based on the OS symbols.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Gonna accept this as answer because this is basically what I wanted to know. Thanks. – Rafiwui Aug 04 '16 at 13:56
  • No problem. There is a lot out there on how to manipulate the console in windows and linux. You just need to combine that and use the preprocessor to pick which parts to use when compiling. – NathanOliver Aug 04 '16 at 13:59
1

The common way to accomplish this is to use conditional compilation to implement WinMain on windows, and main on other platforms.

one possible formulation:

#ifdef _WIN32
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPTSTR lpCmdLine, int)
{
    int argc;
    char ** argv;
    std::tie (argc, argv) = parse_command_line_arguments (lpCmdLine);
#else
int main (int argc, char ** argv)
{
#endif

   return 0

}
nate
  • 1,771
  • 12
  • 17
1

For Windows:

I used to use ShowWindow (GetConsoleWindow(), SW_HIDE); in such case, however if you don't need console, you shouldn't create console app project.


For Linux:

(I'm not a Linux guy, so you need to wait for other answers)


For MAC:

Not MAC developer either :)


And as a final piece you will need preprocessor to compile different code for different platforms.

#ifdef _WIN32
//do what is required for windows
#endif
#ifdef __linux__
//do what is required for linux
#endif
#ifdef __APPLE__
//do what is required for OS x machines
#endif 
ST3
  • 8,826
  • 3
  • 68
  • 92
-2

There is no cross-platform way to do this. On Linux you can do:

int fd = open("/dev/tty", O_RDWR);
ioctl(fd, TIOCNOTTY, NULL);

to detach from terminal.

dE fENDER
  • 130
  • 1
  • 8
  • You are right in the sense that there is no standard cross platform function to do this but it you can make a cross-platform application that does this. – NathanOliver Aug 04 '16 at 14:10
  • Where did you see in my answer that I told that it is impossible to make such thing? – dE fENDER Aug 04 '16 at 14:16
  • You state: *There is no cross-platform way to do this* but there is. You just have to roll your own or find a library that does it. – NathanOliver Aug 04 '16 at 14:20
  • "Cross-platform" usually means that you do not have to search a custom way to do smth for each platform independently. When you find enough ways to complete task and made a library - there will be. But for now: there is not. – dE fENDER Aug 04 '16 at 15:04