64879

get window handler from started process

Question:

I see there's win32process.GetWindowThreadProcess() that gets a window handler and returns it's process id. Is there a way to do the opposite: get the window handler of a running process by it's process id? Something like win32gui.GetWindowHandler(processId) ?

<strong>Specifically What I'm trying to do:</strong> <br />

I have a python script that runs an external program, lets say notepad.exe. <br /> Notepad is fired when runProgram() method is called. I want to prevent this method from running Notepad <strong>more than once.</strong> I accomplish this in the following way, using <a href="http://docs.activestate.com/activepython/2.4/pywin32/win32process.html" rel="nofollow">win32process</a>:

import win32process as process import sys PORTABLE_APPLICATION_LOCATION = "C:\\Windows\\system32\\notepad.exe" processHandler = -1 def runProgram(): global processHandler #don't run a process more than once if (isLiveProcess(processHandler)): #Bring focus back to running window! return; try: startObj = process.STARTUPINFO() myProcessTuple = process.CreateProcess(PORTABLE_APPLICATION_LOCATION,None,None,None,8,8,None,None,startObj) processHandler = myProcessTuple[2] except: print(sys.exc_info[0]) def isLiveProcess(processHandler): #Process handler is dwProcessId processList = process.EnumProcesses() for aProcess in processList: if (aProcess == processHandler): return True return False runProgram()

This works as expected, but if the process is found to be already alive, I'd like to bring it's window back to front with <a href="http://docs.activestate.com/activepython/2.4/pywin32/win32gui.html" rel="nofollow">win32gui</a>

Answer1:

I dont think that Windows API provides a method for this , but you could iterate over all open windows , and find the one that belongs to you .

I have modified your program so it looks like this :

import win32process import win32process as process import win32gui import sys PORTABLE_APPLICATION_LOCATION = "C:\\Windows\\system32\\notepad.exe" processHandler = -1 def callback(hwnd, procid): if procid in win32process.GetWindowThreadProcessId(hwnd): win32gui.SetForegroundWindow(hwnd) def show_window_by_process(procid): win32gui.EnumWindows(callback, procid) def runProgram(): global processHandler #don't run a process more than once if (isLiveProcess(processHandler)): #Bring focus back to running window! show_window_by_process(processHandler) return; try: startObj = process.STARTUPINFO() myProcessTuple = process.CreateProcess(PORTABLE_APPLICATION_LOCATION,None,None,None,8,8,None,None,startObj) processHandler = myProcessTuple[2] except: print(sys.exc_info[0]) def isLiveProcess(processHandler): #Process handler is dwProcessId processList = process.EnumProcesses() for aProcess in processList: if (aProcess == processHandler): return True return False runProgram()

Recommend

  • TextPad “find in files” not matching on simple OR regex
  • Printing out Japanese (Chinese) characters
  • VBScript to check for open process by user
  • subprocess.Popen('start') fails
  • Extjs, handling success or failure when doing a standard submit in a form
  • How to replace TouchesBegan with UIGestureRecognizer
  • Fire callback when selection was made with select2 4.0, and retrieve the value of last selection
  • Changing media screen makes div overlay
  • Change Checkbox value without raising event
  • Can my PDF ping my server when it is opened?
  • Two Tables Serving as one Model in Rails
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Bash if statement with multiple conditions
  • Approximate Order-Preserving Huffman Code
  • Zoom in and out of jPanel
  • Generate random number from custom distribution
  • Firefox Extension - Monitor refresh and change of tab
  • Tamper-proof configuration files in .NET?
  • Remove final comma from string in vb.net
  • Declaring variable dynamically in VB.net
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Play WS (2.2.1): post/put large request
  • Google Custom Search with transparent background
  • Get data from AJAX - How to
  • Initializer list vs. initialization method
  • Spring security and special characters
  • Trying to switch camera back to front but getting exception
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • SVN: Merging two branches together
  • Python: how to group similar lists together in a list of lists?
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Programmatically clearing map cache
  • Busy indicator not showing up in wpf window [duplicate]