1

I am trying to create a simple command-line process and show it to the user (I do NOT want the process to be hidden):

import subprocess
import win32con 

kwargs = {}
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = win32con.SW_SHOWMAXIMIZED
ExecuteString = ('process.cmd')
kwargs['startupinfo'] = info
sp = subprocess.Popen(ExecuteString, **kwargs)

It works with e.g. notepad.exe but not with the simple process.cmd:

echo "This is a process run from python"
pause

I run out of ideas, how to achieve this. I find all kind of stuff, how to HIDE a process. But I want to achieve the opposite. Any idea?

Thanks!

jfs
  • 399,953
  • 195
  • 994
  • 1,670
rosch
  • 21
  • 2
  • I'm not sure what all this `win32con` stuff does but if you just want to print the program's console output you can try [this](http://stackoverflow.com/questions/4417546/constantly-print-subprocess-output-while-process-is-running). –  Apr 29 '14 at 15:32
  • What happens if you run your script from the command line as `python your_script.py`? Do you see the output? Do you see any errors? `subprocess.check_call("process.cmd")` starts the command and waits for it to finish raising an exception if it returns non-zero exit status. Are you sure that your script is literally called `program.cmd`? – jfs Apr 29 '14 at 16:25
  • You probably want to pass the `CREATE_NEW_CONSOLE` flag, but I don't know how to do that from Python. – Harry Johnston Apr 29 '14 at 22:36
  • @HarryJohnston: just pass `creationflags = subprocess.CREATE_NEW_CONSOLE` to `Popen` though you shouldn't need it to do it explicitly unless you want to open a *new* console for the script in addition to the current one. My point is `check_call("process.cmd")` should do the right thing already unless the parent script is named `*.pyw` – jfs Apr 30 '14 at 04:11
  • @J.F. Sebastian, if I use check_call("process.cmd"), the process starts as a new HIDDEN windows console as well. It seems to make no difference. The windows console is always hidden. – rosch Apr 30 '14 at 09:30
  • @André, interesting point. But what I want is a bit different. I don't want to capture-and-display any output, but I want to show the windows command prompt executing a console program (in my example "process.cmd". If the program requires user input, it will be stuck in a hidden process with killing it being the only way out. – rosch Apr 30 '14 at 09:37
  • @J.F.Sebastian, even using creationflags = subprocess.CREATE_NEW_CONSOLE leads to a HIDDEN console. – rosch Apr 30 '14 at 09:51
  • 1
    What happens if you don't pass the startupinfo structure but do pass CREATE_NEW_CONSOLE? (You might be inheriting some kooky settings via subprocess.STARTUPINFO().) Does process.cmd work properly when double-clicked via Explorer? – Harry Johnston Apr 30 '14 at 20:48

2 Answers2

0

You seem to be confusing the notions of process and window. All windows are associated to a process, but a certain process may not be associated with any window.

Your simple batch script is interpreted from the cmd.exe process. If you're used to the behaviour of windows when you open batch scripts with a double-click, might believe cmd.exe is always associated with a window, but that is not true. You can check this yourself by simply running cmd.exe inside a existing command prompt - it doesn't open a new window (as running notepad.exe, for example, would).

In python, processes run "as if" they were run from a command prompt - which is why you don't get another window.

This doesn't actually answer the question, but it might be useful in understanding the problem.

loopbackbee
  • 21,962
  • 10
  • 62
  • 97
  • Thanks for the clarification. What happens is that popen starts a new windows console (cmd.exe). In the console, my program is then executed. The console has an associated window. This window is hidden and I am looking for a way to unhide it. – rosch Apr 30 '14 at 09:54
  • @rosch: I do not understand, [you said](http://stackoverflow.com/questions/23368941/how-to-unhide-and-show-a-process-created-with-subprocess-popen#comment35823786_23368941) that the console is hidden. Do you or do not see a console window (cmd.exe) when you run your Python script? If you do then your question is how do your start *new* console (answer `start ...` or `cmd.exe /c cmd.exe /k ..`, etc). – jfs Apr 30 '14 at 11:30
  • @J.F.Sebastian, I DO NOT see a console window, when I run my Python script. – rosch May 01 '14 at 09:04
  • @rosch: Are you sure? Do you exclude the already opened console window where you can type command-line commands? [Update your question](http://stackoverflow.com/posts/23368941/edit), to describe how do you start your Python script. Have you tried the commands that create *new* window? – jfs May 01 '14 at 11:31
0

For Windowed applications, you simply need to use the SW_HIDE constant instead of SW_SHOWMAXIMIZED.

If you also want to cover console applications that start up a terminal window, I'm guessing that you would want to run something like this:

  1. start the process;
  2. use EnumWindows();
  3. in your EnumWindowsProc implementation, check for a top-level window (using GetParent()) that is owned by the process you just launched (use GetWindowThreadProcessId()).

This will allow you to find the top-level window(s) of the process you just launched. From there, you can call ShowWindow() to show/hide the Window.

Note that this may be subject to some timing issues (if your search runs before the child process can create its window, the search will yield no results) as well as flicker (because you'll hide the window after it displays).

André Caron
  • 44,541
  • 12
  • 67
  • 125