[python-win32] GetModuleFileName requires Process or Thread handle?

Tim Golden mail at timgolden.me.uk
Tue Aug 12 21:18:17 CEST 2008


Walter Decker wrote:
> Can someone help me diagnose why the following fails to retrieve the 
> 'exe' name KillByName?

Do you mind if I sidestep your question altogether
and point out that this is one of those things
which WMI makes a fair bit easier? Obviously,
if what you want is an insight into Win32
process handles and so on, then say so, and
I'll try to oblige. But just in case you
really want to kill a named process, try this:

<code>
import os, sys
import time
import wmi

c = wmi.WMI (find_classes=False)

def kill_by_name (name):
   for process in c.Win32_Process (Caption=name):
     process.Terminate ()

def start_a_process (name):
   c.Win32_Process.Create (CommandLine=name)

if __name__ == '__main__':
   start_a_process (sys.argv[1])
   time.sleep (5)
   kill_by_name (sys.argv[1])

</code>


TJG


More information about the python-win32 mailing list