简体   繁体   English

C++ 在后台运行程序 Windows 10

[英]C++ run program in background Windows 10

I am trying to run an executable in background written in C++ in Windows 10. The program does not have any GUI.我正在尝试在 Windows 10 中以 C++ 编写的后台运行可执行文件。该程序没有任何 GUI。 I tried this我试过这个

window = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(window,0);

But although it does work on Windows 7 and opens and instantly closes the console, and the executable runs in background, on Windows 10 it opens a console and the console stays open.但是,尽管它确实在 Windows 7 上工作并打开并立即关闭控制台,并且可执行文件在后台运行,但在 Windows 10 上它会打开一个控制台并且控制台保持打开状态。 If I run the.exe with run /B main.exe I have to keep the console open in order for the program to keep running, and if I close the cmd console execution stops.如果我使用run /B main.exe运行.exe,我必须保持控制台打开以使程序继续运行,如果我关闭 cmd 控制台执行将停止。

Is there a way to programatically run the file in background?有没有办法以编程方式在后台运行文件? I am aware that I can run it in background using vbscript, but I would like to avoid that.我知道我可以使用 vbscript 在后台运行它,但我想避免这种情况。

With FreeConsole() you can detach a process from the console window -- that worked for me:使用 FreeConsole() 您可以从控制台 window 分离一个进程——这对我有用:

int main() 
{
   FreeConsole();
   while (1) ::Sleep(1000);
   return 0;
}

For detailed description see also the related Microsoft documentation有关详细说明,另请参阅相关的 Microsoft 文档

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

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