• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need to write a quick program that'll check for a running process before it opens another program

yukichigai

Diamond Member
I need to do something fairly basic: write a program that will check all the running processes for two executable names, and only will run another program if it can't find either of them. (We're having problems with people opening up multiple copies of a certain program, which isn't good for the program) Now this wouldn't be hard at all if I was doing it under Linux, but I need this to run on Win2k. I'm not sure what the syntax is to output a list of processes and check them in a Windows environment.

Also, bonus points if someone can figure out how to do this in a batch file, rather than requiring me to write and compile an actual program. It's not that important, but it'd be nice.
 
If the program has a specific title that does not change, Windows has an API that allows you to check for a Window with a given Title. You can also look for a given program via class name.

Otherwise, you can get a list of processes running and check for a specific name.

There are Windows APIs that will assist you; look in the MSDN help site for clues.
 
Codeproject .com has a simple to use C++ class for iterating through the process list for .exe names, go there and search for "Enumerating processes" with author "moliate".
 
Use the Tool Help API.

Process32First, Process32Next all have out PROCESSENTRY32 structs which contain szExeFile (name of image)
 
systernals pslist, text based, and works in a batch file. you can batch pslist and pskill to watch for/kill some processes, or to do an if/then statement to only launch the program if it's not already running. Not sure on cmd batch syntax, as I'm a perl guy.
 
Wouldn't it be better to fix the troublesome program? Or if you don't have the source code, bitch at those who do?
 
Does 2K give you access to tasklist, type tasklist /? and let me know. If not you will have to use pslist like nweaver stated. This would be really easy to do in XP. Let me know if tasklist /? doesn't work and I will write something based on PSLIST. 2 options, either it kills the currently running program or it just quits if the processes are running. I know you wanted the second, but just wanted to present the other option which may end up looking more 'fluid' to the end user. Although, I have no idea what the processes are doing.

By the way, if you need to use pslist, it is completely free and no hastle. http://www.sysinternals.com/Utilities/PsList.html
 
Functional Example, this looks to see if notepad.exe and calc.exe are running, if neither aren't it kicks off calc. Replace executeable names as needed and add paths where required then just grab pslist.
 
A late reply, but I thought I'd thank skace profusely for the example code. Slightly modified that was exactly what I needed to do the job. No more 18+ copies of the program opening on that machine. Domo!
 
Back
Top