Creating hidden processes (window not visible)

折月煮酒 提交于 2019-12-10 10:02:43

问题


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.

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...).

This happens less in Windows XP, but it happens a lot on 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
}

回答1:


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.

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 CreateDesktop and move onward.




回答2:


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.




回答3:


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

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




回答4:


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.



来源:https://stackoverflow.com/questions/1143206/creating-hidden-processes-window-not-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!