windows 创建隐藏进程(窗口不可见)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1143206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:49:48  来源:igfitidea点击:

Creating hidden processes (window not visible)

cwindowshidecreateprocess

提问by wonderer

I'm using CreateProcess() with startup flags set to STARTF_USESHOWWINDOW and SW_HIDE to start an application in the background with its window hidden. I'm doing this to run a scheduled maintenance tasks and i don't want to be bothered with windows.

我正在使用 CreateProcess(),启动标志设置为 STARTF_USESHOWWINDOW 和 SW_HIDE,以在后台启动应用程序并隐藏其窗口。我这样做是为了运行预定的维护任务,我不想被 Windows 打扰。

In most cases the windows are hidden but there are cases where the program's window pops right out in front of you (for example Google's Chrome - i started testing with different apps to see whether this was a once time problem but nope...).

在大多数情况下,窗口是隐藏的,但在某些情况下,程序的窗口会在您面前弹出(例如 Google 的 Chrome - 我开始使用不同的应用程序进行测试,以查看这是否是一次问题,但不是...)。

This happens less in Windows XP, but it happens a lot on Vista.

这种情况在 Windows XP 中发生得较少,但在 Vista 上却经常发生。

Is there a flag that I am missing? Is there any other way to create a process with its window hidden?

有没有我丢失的标志?有没有其他方法可以创建隐藏窗口的进程?

Thanks!

谢谢!

my sample code is:

我的示例代码是:

char *ProgramName  
STARTUPINFO StartupInfoF;
PROCESS_INFORMATION ProcessInfoF;

memset(&StartupInfoF, 0, sizeof(StartupInfoF));
memset(&ProcessInfoF, 0, sizeof(ProcessInfoF));

StartupInfoF.cb = sizeof(StartupInfoF);
StartupInfoF.wShowWindow = SW_HIDE;
StartupInfoF.dwFlags = STARTF_USESHOWWINDOW;    

if (CreateProcess(ProgramName,
                  "",                 
                  0,
                  0,
                  FALSE,
                  DETACHED_PROCESS,
                  0,
                  0,                              
                  &StartupInfoF,
                  &ProcessInfoF) == FALSE)
{
  // error
}
else
{
  // OK
}

回答by eran

You can start the process on another desktop, using the lpDesktop member of the STARTUPINFO structure passed to CreateProcess. This way the process will have all its windows shown, but on another desktop, so you (or your users) won't be bothered with it.

您可以使用传递给 CreateProcess 的 STARTUPINFO 结构的 lpDesktop 成员在另一个桌面上启动该进程。这样,该过程将显示其所有窗口,但在另一个桌面上,因此您(或您的用户)不会被它打扰。

I've never worked with multiple desktops so I can't say what would be the side effects, but I think it's doable. Start by looking into CreateDesktopand move onward.

我从来没有使用过多个桌面,所以我不能说会有什么副作用,但我认为这是可行的。首先查看CreateDesktop并继续前进。

回答by Kirill V. Lyadvinsky

Some programs could ignore/override SW_HIDEflag. You could try to hide window after child process started.

某些程序可能会忽略/覆盖SW_HIDE标志。您可以尝试在子进程启动后隐藏窗口。

Another option is to try to use CreateProcessAsUserto run processes in Session 0 which has isolated desktop (starting from Vista version).

另一种选择是尝试使用CreateProcessAsUser在具有隔离桌面的会话 0 中运行进程(从 Vista 版本开始)。

回答by Imagist

I don't remember the answer to your question, but I'd like to suggest that maybe you shouldn't keep the window totally hidden? If you want the window out of the way, minimizing it will suffice; hiding it completely only removes the ability to check on your scheduled maintenance tasks.

我不记得你的问题的答案,但我想建议你也许不应该完全隐藏窗口?如果你想让窗口不碍事,把它最小化就足够了;完全隐藏它只会取消检查计划维护任务的能力。

回答by T.E.D.

I'd suggest making it a service. For one thing, that will allow it to run your scheduled maintanence even when nobody is logged in. For another, it is fairly easy to set services up so that they don't have access to the desktop.

我建议让它成为一项服务。一方面,这将允许它在没有人登录的情况下运行您的计划维护。另一方面,设置服务以便他们无法访问桌面是相当容易的。