简体   繁体   English

C ++禁止显示命令提示符

[英]C++ disallow command prompt from displaying

Is there any thing I can do to make sure that the shell/command prompt does not show up when I run my C++ program, it is meant to be a background program? 我可以做些什么来确保我运行C ++程序时shell /命令提示符不会出现,这意味着它是一个后台程序? While it isn't a huge deal if it displays because it quickly closes, I would prefer it doesn't display at all. 虽然由于快速关闭而显示并不算什么,但我希望它根本不显示。 Program will run on windows xp/vista/7 OS 程序将在Windows XP / Vista / 7 OS上运行

Configure your compiler or linker to mark your program as a GUI application. 配置您的编译器或链接器,以将您的程序标记为GUI应用程序。

Windows recognizes two main types of programs: GUI and console. Windows识别两种主要类型的程序:GUI和控制台。 If the EXE header is marked as a console program, then the OS creates a console window prior to executing it. 如果EXE标头标记为控制台程序,则OS在执行之前会创建一个控制台窗口。 Otherwise, it doesn't. 否则,事实并非如此。 This isn't something you can control at run time; 这不是您在运行时可以控制的。 you need to set it at link time. 您需要在链接时进行设置。 (You can call ShowWindow(GetConsoelWindow(), SW_HIDE) to try to control it at run time, but that doesn't prevent the window from flickering on and then off again. Plus, if your program is sharing the console with another program, like cmd.exe, then you'll have just hidden the user's command-prompt window!) Even if your program doesn't have any actual GUI, that's still the mode you need to avoid having a console window created for you. (您可以调用ShowWindow(GetConsoelWindow(), SW_HIDE)尝试在运行时对其进行控制,但这不会阻止窗口闪烁然后再次关闭。另外,如果您的程序与另一个程序共享控制台,像cmd.exe,那么您将只隐藏用户的命令提示符窗口!)即使您的程序没有任何实际的GUI,仍然需要避免为您创建控制台窗口的方式。

If you're starting a new project in Visual Studio, select the "Win32 Console Application" option. 如果要在Visual Studio中启动新项目,请选择“ Win32控制台应用程序”选项。 If you already have a project, then in your project's configuration properties, find the "Subsystem" setting of the "Linker/System" section and set it to "Console." 如果您已经有一个项目,则在项目的配置属性中,找到“链接器/系统”部分的“子系统”设置,并将其设置为“控制台”。 That makes the linker use the /subsystem:console option. 这使链接器使用/subsystem:console选项。 If you're using Mingw, use the -Wl,--subsystem,windows option. 如果您使用的是Mingw,请使用-Wl,-subsystem -Wl,--subsystem,windows选项。

Sounds to me like you want to create a Windows service, not a command line utility. 在我看来,您想要创建Windows服务而不是命令行实用程序。 This is why services exist. 这就是为什么存在服务的原因。 Long running background tasks that require no user interaction. 不需要用户交互的长期运行后台任务。 A simple example here . 这里有个简单的例子

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

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