Solved

Determining if a Program is Running

Posted on 1998-05-05
9
211 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Something is screwy here.  I don't remember locking the question.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

by:Egore
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The autograder hits again!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

771 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

13 Experts available now in Live!

Get 1:1 Help Now