Closing Application without showing console window - c++

I have written very very simple console application which supports some command line options.
If there is no command line argument (it means there is only 1 argument) the application closes without showing black window, currently if you run the code below, because it has no command line arguments, it will close immediately, but it will show the black window for a second, I want to avoid it. So How Can I do it in a simple way?
#include <iostream>
using namespace std;
int main(int argc,char** argv)
{
if (argc==1) return 0;
if (argc!=1)
for (int i=2; i<=argc; i++)
cout << argv[i] << endl;
cin.sync();cin.get();
return 0;
}

You can't escape from console window creation if you create console application.
But you can create win32 application with entry point WinMain and there do not create window, just work as console program.

For the program to run, Windows needs a console window. After the console window is created, the control enters main. It is only inside main that you can check the command line parameters. Which means, it is not possible to do what you want.
You can, however, run the program without showing the console window using the CreateProcess API. So if you can run the console application from another program, then you can check if there are command line arguments and then decide whether you should use CreateProcess to show the console window or not.
To not show the console window using CreateProcess, set the dwFlags parameter of STARTUPINFO to STARTF_USESHOWWINDOW and specify SW_HIDE in the wShowWindow parameter.

Related

QtCreator: What is the best way to print strings to the terminal?

I am learning to use Qtcreator on lubuntu 17.10, and want to print some output to the terminal. I've had trouble, so have stripped my program down to a basic Hello World to print a single string to the terminal. Currently, I am using this code:
#include <QCoreApplication>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout << "HELLO WORLD" << std::endl;
return a.exec();
}
My .pro file contains these lines which look like they could influence the terminal behaviour:
QT -= gui
CONFIG += c++11 console
I also have 'Run in terminal' checked in the Run Settings.
This builds without issue. When I run it, the terminal pops up(I am using terminator), and is blank, with a blinking cursor. To get the string to appear, I have to press <Enter>. The string appears, but the program doesn't end. I don't get the "Press enter to close the window" type message in the terminal that would indicate the program has ended, and I must close the terminal 'manually'. Also, in the Application Output window, it says:
/home/user/path_to_my_program crashed.
Presumably because I force close the terminal. I would really like to figure out why it's not printing the string to terminal and exiting cleanly.
The terminal after I press ENTER(blank beforehand):
This is normal for a Qt console Application. It is running a messageloop and exactly doing what it should.
To send text to the console you can use std::cout, qout or qDebug(). The latter is used for debugging. More information can be found here. It also contains the pittfalls you run into. Qt console Application

starting another program with arguments from qt app

I have a simple app
int main(int argc, char* argv[]){
//cout << argv[1];
cout << "hello world";
getchar();
}
and I want to start it from qt program using
QProcess *process= new QProcess(this);
QString appPath= "..../.../TestApp2.exe";
process->start(appPath);
the problem is that my program dosen't starts, even without arguments. I have tried to start a standard app like "calc" and it worked. How could I start my app with specific args (sure after uncommitting the second line of the first snippet)
I have tried to start a standard app like "calc" and it worked. How could I start my app
Your application is a console application.
QProcess hides the console window for console applications and redirects their STDOUT/STDERR for you to read them (using readAllStandardOutput(), readAllStandardError(), ...). and whatever you write() to your QProcess goes to its STDIN. So, if you are expecting to see a console window when the process starts, you are wrong.
If you want to start a console application without hiding its console window, you can use QProcess::startDetached():
QProcess::startDetached("test.exe");
But most of the times there is no reason to do so. QProcess is meant to be used from a GUI application in order to start a process behind the scenes, and take a result from it. After that, you can display the result to the user the way you like. A user of a GUI application usually doesn't expect a console window asking him/her for input every now and then. Also, He/She wouldn't expect to see the result in a console window.

How to minimize console window?

I am running a C++ console application,
for some period of time,
I want to minimize the window in which my application is running.
for eg. I launch myApp.exe from cmd. Then its launched in new window.
So what are libraries which can minimize the window in which application is running.
Application doesnt have any GUI
I suppose your application is running on Windows (this is not portable across different operating systems).
You have first to get handle of your Console window with GetConsoleWindow() function, then you can use ShowWindow() to hide/show it as required. Ddon't forget to include windows.h:
ShowWindow(GetConsoleWindow(), SW_MINIMIZE);
Instead of SW_MINIMIZE you can use SW_HIDE to completely hide it (but it'll flash visible once when application just started).
Note that if you have control over process creation you can create it as DETACHED_PROCESS: a detached console application does not have a console window. CreateProcess() function has also other workarounds you may be interested in (for example you may create a child process for outputting...)
UPDATE: as follow-up of Patrick's answer you may change the subsystem from Console to Windows and then, if you require to write to console, create a new one using AllocConsole:
if (AllocConsole()) {
printf("Now I can print to console...\n");
FreeConsole();
}
Another option is to change
Properties... | Configuration Properties | Linker | System | Subsystem
from Console to Windows.
However, you then need to add a WinMain() entry point, such as:
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{ int argc = 0;
LPWSTR* argv = CommandLineToArgvW(GetCommandLine(), &argc);
return Main(argc, argv);
}
assuming unicode. To avoid confusion, I rename the console's wmain() function to something like Main(), as above. Of course printf no longer has a console to write to.

console app behaviour in Qt

Hi I'm just trying to see if my Qt creator is running properly, what happens is when I create a new project in Qt and I choose Qt console application and I change the main to:
#include <QtCore/QCoreApplication>
int main()
{
return 0;
}
then it runs with no errors but the console window just appears for less than a second, just flashes once and closes. Is that normal or is it supposed to wait for me to press a key to close?
No that's normal, there's no activity happening so it creates the application, does no lines of code then returns a 0 and exits.

Win32 programming hiding console window

I'm learning C++ and I made a new program. I deleted some of my code and now my console window is not hidden. Is there a way to make it hide on startup without them seeing it?
If you're writing a console program and you want to disconnect your program from the console it started with, then call FreeConsole. Ultimately, you probably won't be satisfied with what that function really does, but that's the literal answer to the question you asked.
If you're writing a program that you never want to have a console in the first place, then configure your project so that it is not a console program. "Consoleness" is a property of the EXE file. The OS reads that setting and decides whether to allocate a console for your program before any of your code ever runs, so you can't control it within the program. Sometimes a non-console program is called a "GUI program," so you might look for a choice between "console" and "GUI" in the configuration options of your development environment. Setting it to GUI doesn't require that you have any user interface at all, though. The setting merely controls whether your program starts with a console.
If you're trying to write a program that can sometimes have a console and sometimes not, then please see an earlier question, Can one executable be both a console and GUI app?
Assuming you're on windows, configure your linker to make a gui-program, not a console program.
VS: Look in Linker ptions on project properties
LINK: add /SUBSYSTEM:WINDOWS
MinGW: -mwindows
#include <windows.h>
#include <iostream>
using namespace std;
void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth,0);
}
int main()
{
cout<<"this sentence is visible\n";
Stealth(); //to hide console window
cout<<"this sentence is not visible\n";
system("PAUSE");
return EXIT_SUCCESS;
}
I used to use ShowWindow (GetConsoleWindow(), SW_HIDE); in such case, however if you no need console, so don't create console app project.
As already said, starting the application with console or not is set in the exe. Using gnu compiler the option is -mwindows for no console, for example
g++ -mwindows winapp.c
it seems that the method
#define _WIN32_WINNT 0x0500
#include <wincon.h>
....
case WM_CREATE :
ShowWindow (GetConsoleWindow(), SW_HIDE);
close all parent consoles as well, so if you launch the winapp.exe from a
command line console this will be closed as well!
To literally hide/show the console window on demand, you could use the following functions:
It's possible to hide/show the console by using ShowWindow. GetConsoleWindow retrieves the window handle used by the console.
IsWindowVisible can be used to checked if a window (in that case the console) is visible or not.
#include <Windows.h>
void HideConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}
void ShowConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_SHOW);
}
bool IsConsoleVisible()
{
return (::IsWindowVisible(::GetConsoleWindow()) != FALSE);
}
You can create your window minimized. Or paint it outside the visible screen.
But you could also have messed with the window creation flags. If you really messed things up. It is often better to start a new window. (Or restore from a previous version, or the backup).
You can try this
#include <windows.h>
int main() {
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
MessageBox(NULL,"The console Window has been hidden.","Console Hidden",MB_ICONINFORMATION);
return 0;
}
It is part of the win32 API, which you can include using "#include "
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
The first argument tells the program to get the console window that is currently running the program. The second argument passes down the instruction for what you want to do with the window. "SW_HIDE" hides the window, while "SW_SHOW" shows the window.