Solved

Determining if a Program is Running

Posted on 1998-05-05
9
221 Views
Last Modified: 2010-04-10
I am writing a Windows C++ program using MSVC++5.0 Professional Edition.  Right now my code is basically just:

#include <windows.h>
int WinMain .......

And then about five lines of code in the body.  What I need to do is add a few more lines of code to determine if a program is already running or not.  I need some code that will check to see if a program is running or not.
0
Comment
Question by:Egore
9 Comments
 
LVL 1

Expert Comment

by:Odie
ID: 1173128
I think this should do the trick:

#define MutexName "My wonderfull program"

  HANDLE Mutexy;
 

  Mutexy = CreateMutex(NULL, true, MutexName);
  if (GetLastError() != 0) { // already running

    ReleaseMutex( Mutexy );
    return -1; // exit the program, for we're already running

  } else {
    // continue with the program
  }

0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1173129
Can you spare another 5 points?  If so, check out http://www.experts-exchange.com/topics/comp/lang/cplusplus/Q.10049682
nietod and myself gave that question a really good workout.  I think we got all the bases covered.
0
 
LVL 1

Expert Comment

by:Blondie050798
ID: 1173130
When you say you want to determine if a program is running, do you mean 'any program' or another instance of your application?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1173131
Something is screwy here.  I don't remember locking the question.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:Egore
ID: 1173132
When I say determining if a program is running, I mean, I specify a program (like Solitaire) and it tells me if it is running.  I know how to check for another instance of my own program...
0
 
LVL 1

Expert Comment

by:Blondie050798
ID: 1173133
If the application you want to find always begins its title bar text with a specific sub-string, the GetWindowText API function can be applied to each running window to detect the presence of that application. The following code demonstrates this procedure and works whether or not the application is visible or has a document open. This technique can also be applied to any application that uses a top-level window that starts its title bar text with a known sub-string.
0
 
LVL 1

Expert Comment

by:Blondie050798
ID: 1173134
Ooops...missed out the code...well using GetWindowText is too trivial anyway... :-)  Let me know if this isn't good enough
0
 
LVL 11

Expert Comment

by:alexo
ID: 1173135
What you need is the FindWindow() function (if you know the window title or the class).  Otherwise there is the toolhelp functions that deal directly with processes (it is Win95 only, NT uses a different method).  I strongly recommend FindWindow().
0
 
LVL 11

Expert Comment

by:alexo
ID: 1173136
The autograder hits again!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now