简体   繁体   中英

Closing Application without showing console window

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM