Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a program with python minimized or hidden

Tags:

python

windows

What I'm trying to do is to write a script which would open an application only in process list. Meaning it would be "hidden". I don't even know if its possible in python.

If its not possible, I would settle for even a function that would allow for a program to be opened with python in a minimized state maybe something like this:

import subprocess
def startProgram():
    subprocess.Hide(subprocess.Popen('C:\test.exe')) #  I know this is wrong but you get the idea...
startProgram()

Someone suggested to use win32com.client but the thing is that the program that i want to launch doesn't have a COM server registered under the name.

Any ideas?

like image 807
wtz Avatar asked Feb 23 '10 16:02

wtz


People also ask

How do you hide a program in Python?

There are ways to 'hide' your Python source code: ship your application as . pyc files (saved in the __pycache__ folders in Python3) ; this doesn't stop someone using the dis library from looking at the source code equivalent - but it does make it difficult.


2 Answers

It's easy :)
Python Popen Accept STARTUPINFO Structure...
About STARTUPINFO Structure: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx

Run Hidden:

import subprocess

def startProgram():
    SW_HIDE = 0
    info = subprocess.STARTUPINFO()
    info.dwFlags = subprocess.STARTF_USESHOWWINDOW
    info.wShowWindow = SW_HIDE
    subprocess.Popen(r'C:\test.exe', startupinfo=info)
startProgram()

Run Minimized:

import subprocess

def startProgram():
    SW_MINIMIZE = 6
    info = subprocess.STARTUPINFO()
    info.dwFlags = subprocess.STARTF_USESHOWWINDOW
    info.wShowWindow = SW_MINIMIZE
    subprocess.Popen(r'C:\test.exe', startupinfo=info)
startProgram()
like image 144
Tomer Zait Avatar answered Sep 25 '22 01:09

Tomer Zait


You should use win32api and hide your window e.g. using win32gui.EnumWindows you can enumerate all top windows and hide your window

Here is a small example, you may do something like this:

import subprocess
import win32gui
import time

proc = subprocess.Popen(["notepad.exe"])
# lets wait a bit to app to start
time.sleep(3)

def enumWindowFunc(hwnd, windowList):
    """ win32gui.EnumWindows() callback """
    text = win32gui.GetWindowText(hwnd)
    className = win32gui.GetClassName(hwnd)
    #print hwnd, text, className
    if text.find("Notepad") >= 0:
        windowList.append((hwnd, text, className))

myWindows = []
# enumerate thru all top windows and get windows which are ours
win32gui.EnumWindows(enumWindowFunc, myWindows)

# now hide my windows, we can actually check process info from GetWindowThreadProcessId
# http://msdn.microsoft.com/en-us/library/ms633522(VS.85).aspx
for hwnd, text, className in myWindows:
    win32gui.ShowWindow(hwnd, False)

# as our notepad is now hidden
# you will have to kill notepad in taskmanager to get past next line
proc.wait()
print "finished."
like image 35
Anurag Uniyal Avatar answered Sep 23 '22 01:09

Anurag Uniyal