Skip to content

Instantly share code, notes, and snippets.

@raincode00
Last active March 24, 2018 11:15
Show Gist options
  • Save raincode00/18a15558bcbece63d841b63ee5f6cc91 to your computer and use it in GitHub Desktop.
Save raincode00/18a15558bcbece63d841b63ee5f6cc91 to your computer and use it in GitHub Desktop.
Launches an application and forces it to the foreground
import win32gui
import win32process
import win32api
import win32con
import time
from fnmatch import fnmatch
ORIGINAL_PROCESS_EXE = "NeptuniaVII_real.exe"
def show_window_by_proc_id(proc_id):
#disable window foreground window lock
win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
def _window_enum_callback(hwnd, proc_id):
if proc_id in win32process.GetWindowThreadProcessId(hwnd):
fgwin = win32gui.GetForegroundWindow()
fg = win32process.GetWindowThreadProcessId(fgwin)[0]
current = win32api.GetCurrentThreadId()
if current != fg:
win32process.AttachThreadInput(fg, current, True)
win32gui.SetForegroundWindow(hwnd)
win32gui.SetActiveWindow(hwnd)
win32process.AttachThreadInput(fg, win32api.GetCurrentThreadId(), False)
win32gui.EnumWindows(_window_enum_callback, proc_id)
def launch_app(exec_file):
si = win32process.STARTUPINFO()
pinfo = win32process.CreateProcess(exec_file,None,None,None,8,8,None,None,si)
proc_id = pinfo[2]
time.sleep(5)
show_window_by_proc_id(proc_id)
def is_running(proc_id):
all_procs = process.EnumProcesses()
for p in all_procs:
if p == proc_id:
return True
return False
launch_app(ORIGINAL_PROCESS_EXE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment