[C++] Find out if a program is running?

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

[C++] Find out if a program is running?

Post by toasty2 »

How can I check if a program is running (on Windows) with C++? Some places suggest FindWindow or something like that, but all those suggestions are on newsgroups back in 1999 and 2000. I am using Dev-C++ (seems to support a most of VC++'s libraries). What method can I use to tell if a certain program is running? I am writing a monitoring app.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Use mutexes.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

toasty2 wrote:What method can I use to tell if a certain program is running?
Do you want to monitor yopur own program (that you can modify) or do you want to monitor arbitrary programs?
toasty2 wrote:Some places suggest FindWindow or something like that, but all those suggestions are on newsgroups back in 1999 and 2000
FindWindow is still a valid function.
You might also be interested in EnumProcesses.

see also:
http://msdn2.microsoft.com/en-us/library/ms633499.aspx
http://msdn2.microsoft.com/en-us/library/ms682629.aspx
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Alternatively you can look into: Process32First() and related functions...

This is perhaps of interest as well:

http://www.alexfedotov.com/articles/enumproc.asp

Cheers :)
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

volka wrote:Do you want to monitor yopur own program (that you can modify) or do you want to monitor arbitrary programs?
No, I will be monitoring other programs (a few server apps). I plan on running a loop that every 30 seconds does whichever method I decide to use to check if the programs are running. Thanks everyone, I'll try using a few of these methods.

Edit: What library is FindWindow in?
feyd, I'm confused by CreateMutex, what is it exactly?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Hang on, you're checking programs you don't control? Enumerate the processes then.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

toasty2 wrote:Edit: What library is FindWindow in?
But win32 services usually do not have windows. In fact you need to specifically allow services to interact with the desktop.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

How do I do that?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

e.g in the service control panel. But that only allows a service to interact with the desktop. It does not force a service app to create a window. Services usually do not have windows.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

With the first method at http://www.alexfedotov.com/articles/enumproc.asp,
how do I check for a particular exe? How do I utilize the code?
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

toasty2 wrote:With the first method at http://www.alexfedotov.com/articles/enumproc.asp,
how do I check for a particular exe? How do I utilize the code?
My best advice to you, would be to ask this question over at the codeproject.com a community of dedicated Windows developers. There is likely an article floating around somewhere.

p.s-Why are you using Dev-C++

There is a free version of Visual C++ called Express. Far better for working on Windows apps than BloodShed. Unless you have plans on porting to Linux, in which case use the wxWidgets library and BloodShed.

http://msdn.microsoft.com/vstudio/express/visualc/

Cheers :)
Post Reply