Reputation: 87
I'm developing an application using C that should run on Windows and Linux. This application should hide the shell it runs from. On Windows I was able to fix it by calling the ShowWindow function but on Linux I can't find a way to achieve the same effect.
void main(void)
{
do_stuff();
#ifdef _WIN32
ShowWindow(GetConsoleWindow(), SW_HIDE);
#else
// Linux hide current console
#endif
while(1)
{
do_other_stuff();
}
}
Upvotes: 0
Views: 193
Reputation: 12668
In linux, the "console" (I put quotes around the word for reasons explained below) you call is not such a console, but another application that runs separate of the main application, so you don't have normally full control of it (because the application doesn't know if it is running from the actual system console, from an xterm(1)
application, gnome-terminal
application, and the like)
Assume you are launching your application from an ssh connection to a remote server, with the DISPLAY
environment variable indicating the X11 server to display the info. In that case, the command shell that spawns your application is not a graphical environment "console" but a local shell running on a ssh session started from a remote connection that is completely out of control.
Note about the term "console": The Windows term "console" makes reference to the common POSIX shell interface of starting a process with three (in windows, five) open file descriptors, standard input, standard output, and standard error (in MS-DOS also standard printer and standard communications port) descriptors to allow by default the program to read from its standard input, and write to its standard output and standard error. The term console comes from the fact that, in MS-DOS, (MS-DOS made an improvement from CP/M by making similar system calls as UNIX to read and write to descriptors) all this stuff happened on the console, so they selected (this time in windows) the term "a Windows console application" as a Windows normal application normally didn't provide this interface. For that, Windows requires your program to be linked as a "Windows console" this means to link a special library that opens a Windows window (under control of the application) and makes all input, output and standard error to go there. This makes Windows console applications being capable of controlling the console, as it is an application window, not the case of Linux, that can be, or can be not.
In UNIX systems, the term console applies to a special device (of similar characteristics that the ones used for user sessions) in which the kernel used to start communications with the administrator (something like ---but not equal--- the standard input, standard output and standard error of the kernel) A special device, normally attached to a printer, to show the kernel messages about device errors or kernel security messages. UNIX systems have normally many text devices attached and only one is selected as the console of the system, but due to this difference from Windows machines, Microsoft decided to reinterpret the term to their convenience. Probably a wrong, but accepted decision.
Upvotes: 1