[python-win32] GetModuleFileName requires Process or Thread handle?
Walter Decker
wcdsoft at live.com
Tue Aug 12 17:11:44 CEST 2008
Hello,
Can someone help me diagnose why the following fails to retrieve the 'exe' name KillByName?
Some code copied from unnamed webpage:
import sysimport osimport timeimport win32apiimport win32process
def StartAProcess(RunCmd): StartupInfo = win32process.STARTUPINFO() np = win32process.CreateProcess( None, # program RunCmd, # command line None, # process security attributes None, # thread attributes 1, # inherit handles
0, # Don't need anything here. None, # new environment None, # new directory StartupInfo) print "StartAProcess np: " + str(np)
def KillByName(matchstring): processList = win32process.EnumProcesses() print "KillByName processList: " + str(processList) msLower = matchstring.lower() print "KillByName msLower: " + msLower for pid in processList: try: print "KillByName pid: " + str(pid) handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid) print "KillByName handle: " + str(handle) exe = win32api.GetModuleFileName(handle) print "KillByName exe: " + exe print "KillByName oneProcess: handle " + str(handle) + " pid " + str(pid) + " exe " + exe if exe.lower() == mslower: print "KillByName found pid: " + str(pid) #win32api.TerminateProcess(handle,0) #win32api.CloseHandle(handle) except: pass
if __name__ == "__main__": if 2 == len(sys.argv): StartAProcess(sys.argv[1]) time.sleep(10.0) KillByName(sys.argv[1])
Is a handle a handle a handle? The win32api.CreateProcess that started the process returns two (2) handles (hProcess and hThread) and the OpenProcess
call above returns the second of those two handles (the hThread). Is it that I need the 'other' handle as in:
"The result is a tuple of (hProcess, hThread, dwProcessId, dwThreadId)"
to send to win32api.GetModuleFileName(handle)?
The 'try' 'except' catches on the win32api.GetModuleFileName.
Regards, Walter.
_________________________________________________________________
Get more from your digital life. Find out how.
http://www.windowslive.com/default.html?ocid=TXT_TAGLM_WL_Home2_082008
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20080812/28f60b3c/attachment.htm>
More information about the python-win32
mailing list