Solved

Determining if a Program is Running

Posted on 1998-05-05
9
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:alexo
ID: 1173131
Something is screwy here.  I don't remember locking the question.
0
 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

622 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