[python-win32] Re: Getting path+executable associated with a process

Gabriel Reis gabrielcnr at gmail.com
Thu Feb 10 14:48:37 CET 2005


Hi again,

thanks for the tips. I didn't use EnumProcessModules, but I got the
solution as shown below:

import win32api, win32con, win32process

def IsRunning(filename):
    processes = win32process.EnumProcesses()    # get PID list
    for pid in processes:
        try:
            handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS,
False, pid)
            exe = win32process.GetModuleFileNameEx(handle, 0)
            if exe.lower() == filename.lower():
                return True
        except:
            pass
    return False


This function gets a 'filename' (full path to the executable file) 
and compares it to each process' executable name. It is very useful to
determine wheter a program is running or not.
E.g.: IsRunning('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
should return True, if Firefox browser is opened.

Thanks again,

Gabriel Reis
Reason Tecnologia S.A. - http://www.reason.com.br/
Florianopolis, SC, Brazil





On Wed, 9 Feb 2005 15:23:28 -0500, Roger Upole <rwupole at msn.com> wrote:
> These functions in win32process should be able to get that for you:
> OpenProcess, EnumProcessModules and GetModuleFileNameEx
> The executable will be the first module returned from EnumProcessModules.
>        Roger
> 
> 
> Gabriel Reis wrote:
> > Hi,
> >
> > I want to get the executable filename and its path from a process.
> > Imagine that I have an application installed in two different
> > directories. The rule is: the user can't run an instance of this
> > application if another instance, from the *same* directory, is already
> > running.
> >
> > I've read PyWin32 documentation and browsed this maillist's history
> > and I couldn't find anything specifically. With the method
> > MakeCounterPath I can get information (items) such as Memory, Process
> > ID or % CPU... so I am wondering if I can get the executable path too,
> > once I have the PID.
> >
> > Best regards,
> >
> > Gabriel Reis
> _______________________________________________
> Python-win32 mailing list
> Python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>


More information about the Python-win32 mailing list