#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    1
    Rep Power
    0

    How to determine if a win32 process is running?


    I am a newbie to python. Can someone share some code that determines if a process on a win32 box is running?

    example:

    If notpad.exe program is running then:
    kill notepad.exe
    return 1
    else
    return 0

    thanks for the help.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11
    take a look at the process.py script at http://rgruet.free.fr/ or just download his rgutils.zip file.

    although a little involved, play around with the process.py file and you'll be able to kill off notepad like never before It also helps that notepad is used as an example in the test() def

    also checkout the python cookbook as you can enumerate through running processes... i have the code somewhere just a sec... here it is. I altered it a bit but it shows the current running processes, as well as any switches or parametres that they run with.

    Code:
    import _winreg
    
    aReg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
    try:
        target = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
        print "reading from", target
        aKey = _winreg.OpenKey(aReg, target)
        print "#","\tname","\tvalue"#,"\ttype"
        
        for i in range(1024):
            try:
                name,value,type = _winreg.EnumValue(aKey, i)
                #type is always 1, so?
                print i,"\t",name,"\t",value#,"\t",type
            except EnvironmentError:
                print"you have",i,"tasks starting at login"
                break
    finally:
        #print "running closekey's"
        _winreg.CloseKey(aKey)
        _winreg.CloseKey(aReg)
    who knows. might be useful.
    hope that helps

    sf2k
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11
    http://tgolden.sc.sabren.com/python/wmi_cookbook.html

    this creates and terminates notepad's (what's up with notepad examples?) using his wmi module.

    saw in the docs something about win32process.Terminate() but hard to find examples. (hmm might be in the wmi eh?)

    hope that helps
    sf2k

IMN logo majestic logo threadwatch logo seochat tools logo