1

I am trying to run an executable from python using subprocess.call(), see the code below. However, the executable has a GUI which pops up whenever it is called. I don't need to see the executable window and so I would like it to just open in the background. Note, that the variable tool below is the name of the executable and chdir_file is the required input file for the executable. Is there a way to stop the called program from popping up and instead opening in the background?

subprocess.call('"' + tool + '"' + " /R: " + '"' + chdir_file + '"')

Please note that the command window is not shown when I execute the above code, only the executable window. I have seen a lot about how to hide the command window during this command but not about how to hide the called program. Here are some of the things I have tried:

1)

si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.call('"' + tool + '"' + " /R: " + '"' + chdir_file + '"', startupinfo=si)

Result: partially successful This opens the executable windows in the background but removes the cursor from the currently active window, which I would like not to happen.

2)

CREATE_NO_WINDOW = 0x08000000
subprocess.call('"' + tool + '"' + " /R: " + '"' + chdir_file + '"', creationflags=CREATE_NO_WINDOW)

Result - no change: No command window, but the executable window still pops up in the screen foreground.

3)

DETACHED_PROCESS = 0x00000008
subprocess.call('"' + tool + '"' + " /R: " + '"' + chdir_file + '"', creationflags=DETACHED_PROCESS)

Result - no change: Same as number (2) - No command window, but the executable window still pops up in the screen foreground.

4)

subprocess.call('"' + tool + '"' + " /R: " + '"' + chdir_file + '"', shell=True)

Result - no change: Same as number (2) & (3) - No command window, but the executable window still pops up in the screen foreground.

5)

subprocess.call("start " + '"' + tool + '"' + " /B" + " /R: " + '"' + chdir_file + '"')

Result - no change: Same as number (2) & (3) & (4) - No command window, but the executable window still pops up in the screen foreground.

Thanks in advance!

Bruce
  • 11
  • 3
  • 1
    Your code does not use "a lot about to hide the command window". You call it without any parameters, so it won't do anything special. Which of the "a lot" options have you tried and how did they affect the result? – Thomas Weller May 26 '21 at 11:06
  • Does this answer your question? [How do I hide the console when I use os.system() or subprocess.call()?](https://stackoverflow.com/questions/7006238/how-do-i-hide-the-console-when-i-use-os-system-or-subprocess-call) – JonSG May 26 '21 at 13:16
  • Good point, I will edit my original post now to explain the things I have tried. I had seen this thread and have tried the suggested modifications. Using the STARTUPINFO suggestion means that the executable does not come to the forefront of the screen, which is an improvement, but when the executable is open it takes the cursor from the window I am currently on and have to reclick on my window. This sounds minor, but the executable will be called every couple of seconds for 2/3 hours, so I would prefer it just ran in the background. – Bruce May 26 '21 at 13:51

1 Answers1

0

STARTF_USESHOWWINDOW works in concert with another STARTUPINFO option, wShowWindow. The default is 0, but it can have any of the values on this page:

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow

Could you try a variety of them for your application? 7 looks promising.

SW_SHOWMINNOACTIVE: 7
Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except the window is not activated.

Unfortunately, it seems that the result will be unpredictable unless the target process is built in a certain fashion:

[For] ... the STARTF_USESHOWWINDOW flag to have an effect on your program, [...] pass SW_SHOWDEFAULT to ShowWindow.

  • Hi Richard, thanks for this. I agree with you, this seems like it should work, yet alas... I have added in si.wShowWindow = 7, and also tried 2, 4, and 11, however no matter which one I use it still seems to be activating the executable window it is opening. It seems to be doing the right thing about whether the window is shown or minimised, but always it seems to be activating the window, even if I have selected an option which shouldnt be activated (like 7 for example). – Bruce May 26 '21 at 15:26
  • Do you have control over the code in this subprocess? It seems it is possible for a process to ignore STARTF_USESHOWWINDOW depending on how it is built: https://devblogs.microsoft.com/oldnewthing/20100301-00/?p=14773 so you may be SOL if the original authors were determined to show the window. – Richard Sheridan May 26 '21 at 15:33
  • Hmmm... this is what I was thinking... it is a commercial software, so I don't have access to the source code. You might be right, I have contacted the developers to see if it is a feature of the software to have it always activate when opened. – Bruce May 26 '21 at 15:51