0

I'm trying to make a program that won't let you continue if a certain program is open. In this particular case, if Cheat Engine.exe is open, you cannot continue.

I thought the code would be something like this:

def openedprogram():
with open('Cheat Engine.exe', 'r') as f:
    print("You have Cheat Engine Open! Close and uninstall it before continuing!")
else:
    continue()

This presents a few errors, I tried taking out the "Else:", and I got an error saying Cheat Engine.exe isn't a valid directory. Any help will be appreciated! Thanks.

  • 1
    Do you mean "if a certain software was already running"? – furins Jan 28 '13 at 00:47
  • 3
    You don't really want to **open** the file, but rather see if a given **process** is running. Opening, in programming, is done when you need to read/write the *contents* of the file. You may want to rephrase your question to make that clearer. Also, make sure to explain throughly why this isn't a duplicate of [this question](http://stackoverflow.com/questions/7787120/python-check-if-a-process-is-running-or-not) – loopbackbee Jan 28 '13 at 00:47

2 Answers2

1

This is nearly entirely impossible to do (see antivirus and anti hack injection) but you can for the most part see if the process name shows up in your process list.

Somthing like this will get the processes active at the moment

import os
import psutil # http://code.google.com/p/psutil/
import time

def get_procs():
    while 1:
        procs = psutil.get_process_list()
        procs = sorted(procs, key=lambda proc: proc.name)

        for proc in procs:
            yield proc.name
Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
0

Your question asks one thing (is a program "running") while your code checks for another thing (can you successfully open a file to read it) and your error message and the text of your question is discussing yet another thing (is a given executable "installed).

Until you understand the distinctions among these different questions you cannot possibly write a program to do what you intend (whatever that actually is).

Opening a file tells you whether a file by a given name exists in a given location with permissions that allow you to access it. That's useless for trying to prevent a "cheat" utility from being used while your program/game is running.

You might have some, very limited, success by checking the PATH to see if the "cheat.exe" program is anywhere thereon. However, that would be pretty lame. The intelligence and educational bar to renaming the program and keeping it in some directory that's NOT on the PATH is pretty low. I figure the average 10 year old playing games on MS Windows will probably solve that puzzle (hack around that strategy) in about five minutes or less ("Achievement unlocked!").

Programs such as "World of Warcraft" have rather sophisticated "warden" techniques which attempt to prevent programs (such as "Glider" or or "Shadowbot") from running while WoW is open. Such programs can, of course, be installed in any directory, under any name, and can, while running, rename themselves in the process listing. These "cheat" programs use essentially the same OS facilities that a debugger uses (the ptrace interface under MacOS X and whatever its analog is under MS Windows). These allow programs like gdb and strace to "peek" into another program's address space and "poke" values thereto. So they work by peeking at locations where the program stores certain model information about the user interface (essentially where a target is located, for example) and poking events into the buffers where the OS would normally place keyboard and mouse events (thus simulating user interactions and automating them).

The "warden" can, conceivably, try to scan the process table for certain signatures (similar to an anti-virus) and could even attempt to use similar "debugging" interfaces to look for traces of the verbotten programs.

However, ultimately this is not a game you'll win without the support of the hardware (something like the infamous TPM --- trusted platform module) which is currently not available to game authors in commodity consumer markets (general purpose PCs and Macs, for example).

But hey, have fun trying.

Jim Dennis
  • 17,054
  • 13
  • 68
  • 116