Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

Determining if a Program is Running

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
Egore
Asked:
Egore
1 Solution
 
OdieCommented:
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
 
alexoCommented:
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
 
Blondie050798Commented:
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.

 
alexoCommented:
Something is screwy here.  I don't remember locking the question.
0
 
EgoreAuthor Commented:
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
 
Blondie050798Commented:
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
 
Blondie050798Commented:
Ooops...missed out the code...well using GetWindowText is too trivial anyway... :-)  Let me know if this isn't good enough
0
 
alexoCommented:
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
 
alexoCommented:
The autograder hits again!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now