Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Determining if a Program is Running

Posted on 1998-05-05
9
Medium Priority
?
262 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 100 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

715 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