views:

984

answers:

4

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
}
A: 

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

Kirill V. Lyadvinsky
but that would flicker the window in front of me. Very annoying...
wonderer
+3  A: 

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.

eran
ok, this seems to work, however i'm getting an error and after searching the MSDN it says that I need not have DESKTOP_CREATEWINDOW privileges. Do you know how to change the privileges of my process and add DESKTOP_CREATEWINDOW?
wonderer
Ok, I figure it out. createdesktop was the answer. thanks!
wonderer
A: 

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.

Imagist
A: 

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.

T.E.D.