0

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....

4
  • For checking if a process is running, I would recommend taking a look at this question. Commented Jan 12, 2015 at 1:10
  • You can use psutil. Here's discussion on that: stackoverflow.com/a/8136371/2326132 what error are you getting? Commented Jan 12, 2015 at 1:11
  • If the problem is that FindWindow throws an error, why not just catch the error? (docs.python.org/2/tutorial/errors.html) Commented Jan 12, 2015 at 2:23
  • I am very new at this and I do not know how to catch an error Commented Jan 15, 2015 at 15:40

1 Answer 1

2

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.