简体   繁体   English

关闭应用程序而不显示控制台窗口

[英]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. 如果没有命令行参数(这意味着只有1个参数),应用程序将关闭而不显示黑色窗口,当前,如果您运行下面的代码,因为它没有命令行参数,它将立即关闭,但将显示黑色的窗口一秒钟,我想避免它。 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. 为了运行该程序,Windows需要一个控制台窗口。 After the console window is created, the control enters main. 创建控制台窗口后,控件进入main。 It is only inside main that you can check the command line parameters. 只有在main内部,您才能检查命令行参数。 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. 但是,您可以使用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. 因此,如果可以从另一个程序运行控制台应用程序,则可以检查是否存在命令行参数,然后决定是否应使用CreateProcess来显示控制台窗口。

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. 要不使用CreateProcess显示控制台窗口,请将STARTUPINFOdwFlags参数设置为STARTF_USESHOWWINDOW,然后在wShowWindow参数中指定SW_HIDE。

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. 但是您可以使用入口点WinMain创建win32应用程序 ,并且不会创建窗口,而只是作为控制台程序工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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