Chris Stone
Chris Stone

Reputation: 129

how to check if any program is running in windows using python

I would like to use win32 in python to create 2 functions... 1. A function that checks if a certain application is running. 2. A function that checks if an application is installed...

I have tried the following to check if something is running;

def IsRunning(ProgramName):
    if win32ui.FindWindow(None, ProgramName):
        print("its running")
        return True
    else:
        print("its not running!")

but the findwindow always throws an error if the program is not running before my program ever gets to the else statement and I do not know how to bypass that....

Upvotes: 0

Views: 3724

Answers (1)

Chris Stone
Chris Stone

Reputation: 129

I needed to pass it like this;

def IsRunning(WindowName):
    try:
        if win32ui.FindWindow(None, WindowName):
            print("its running")
            return True
    except win32ui.error:
        print("its not running!")
        return False

You need to put the window title exactly for it to pass the test and return True...

The next thing I want to write is a similar function that uses regular expressions to find any part of a title name...

Brilliant!!! Happy now :)

The only thing with this, is that if the Applications creator decides to change the title of the main window of the program you are trying to test for, it will no longer work... I was hoping for a much more robust way of doing it... i.e. through some kind of unique process code!

in any case this will do for the time being, but if anyone has a more definitive answer please let me know...

Upvotes: 2

Related Questions