Allowing an .exe to run only once at any one time

Hello all!

Ok I am completely new to C++ (as in this week) and need some help with this if possible.

Below is some basic code taken from Dev-C++ which just prints "Hello world" and then stops waiting for a key to be pushed to exit.

But when it is compiled into an .exe, I need it to only ever be allowed to run once at any one time. That is, if it is already a running process in task manager it must not run. But if it is not running already, then yes it is ok to run. So it can never run more than once at any one time.

Can this be achieved does anyone think? If so, please be gentle (!) and help me understand how to code this in if you would be so kind. Again, I know pretty much nothing at this stage.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main (int argc, char *argv[])
{
  cout << "Hello World!" << endl;
  cout << "Press ENTER to continue..." << endl; 
  cin.get();
  return 0;
}


Many thanks for your time

Gadget Man! :)

Last edited on
This is an OS-Dependent way. You should look into Windows Mutexes on windows.

Here's a nice example from http://support.microsoft.com/kb/243953
This:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class CLimitSingleInstance
{
protected:
  DWORD  m_dwLastError;
  HANDLE m_hMutex;

public:
  CLimitSingleInstance(TCHAR *strMutexName)
  {
    //Make sure that you use a name that is unique for this application otherwise
    //two apps may think they are the same if they are using same name for
    //3rd parm to CreateMutex
    m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early
    m_dwLastError = GetLastError(); //save for use later...
  }
   
  ~CLimitSingleInstance() 
  {
    if (m_hMutex)  //Do not forget to close handles.
    {
       CloseHandle(m_hMutex); //Do as late as possible.
       m_hMutex = NULL; //Good habit to be in.
    }
  }

  BOOL IsAnotherInstanceRunning() 
  {
    return (ERROR_ALREADY_EXISTS == m_dwLastError);
  }
};


Now, in your main function:

1
2
3
4
5
int main(int argc, char * argv[])
{
    CLimitSingleInstance lsi(TEXT("Global\\{INSERT YOUR GUID HERE}"));
    if(lsi.IsAnotherInstanceRunning()) { /* There's another window already open! */ }
}

You can generate a guid in many ways. VS has a GUID generator, or go here:
http://www.guidgenerator.com/online-guid-generator.aspx

It will look like:
TEXT("Global\\{abcdefgh-ijkl-mnop-qrst-uvwxyzabcdef}")
Being new to C++, I have no idea what any of that answer meant. I guess it's hard for people who know what they're talking about to put themselves into the shoes of people who are still on the first step. But thanks anyway.

Meanwhile, I found a good solution for anyone who needs it: use VB instead, on VB 2012, or 2012 or 2005 etc. They have a tick box "allow one instance" which does it all for you with no coding. Couldn't be simpler.

Hope this helps someone else out there.

"gadget50"
Being new to C++, I have no idea what any of that answer meant. I guess it's hard for people who know what they're talking about to put themselves into the shoes of people who are still on the first step. But thanks anyway.


If you dont understand it , it means you have not acquired the basic tools yet, and hence you should be working on that instead.
Last edited on
Topic archived. No new replies allowed.