Single Instance Of A Program

Hi all...

I only want the user to be able to open my application an instance at a time.  Is there an quick and easy way to do this.  my code uses API's with NO MFC.  

Thanks!
b0ot8r123Asked:
Who is Participating?
 
AxterConnect With a Mentor Commented:
jkr, you're too fast.

Most of that code is in both the previously posted link.

I myself, usually mix the executable's name with a constant, so as to make sure there isn't another executable with that name, and to make the code reusable.

const std::string UniqueName = std::string(argv[0]) + "AxterInc_0000";
   HANDLE hSingle;

    hSingle = ::CreateMutex(NULL, TRUE, UniqueName.c_str());
    if (ERROR_ALREADY_EXISTS == GetLastError()) {
          return -1; //Exit main
    }

By mixing the executable name with the company name and an ID, that should give you a unique name, and you can easily reuse the code on another application withing changing the code.
0
 
bkfirebirdCommented:
0
 
bkfirebirdCommented:
you can use the same without MFC also
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
jkrCommented:
Check out http://support.microsoft.com/default.aspx?scid=kb;en-us;243953 ("HOWTO: Limit 32-bit Applications to One Instance Using C++")
0
 
AxterCommented:
If your application has a unique name, you can use argv[0].
Example:
     HANDLE hSingle;

     hSingle = ::CreateMutex(NULL, TRUE, argv[0]);
     if (ERROR_ALREADY_EXISTS == GetLastError()) {
           return -1; //Exit main
     }
0
 
waysideCommented:
There are lots of ways to do this, some work better than others. First you have to decide what a "single instance" means:

- avoiding multiple instances started in the same user session
- avoiding multiple instances started in the same desktop
- avoiding multiple instances started in any session of the same user account
- avoiding multiple instances started on the same machine


The best explanation of the pro's and con's of various methods to do this that I've found (along with the way to handle the above list - in fact I got this list from there) is here:

http://www.flounder.com/nomultiples.htm

It's interesting reading.

0
 
b0ot8r123Author Commented:
Basically I only want the app to open once as I'm listening a socket and don't want the user to beable to open another instance of the app. as it screws up my socket...  I've used this code as suggested (see below).  One last thing.. I've not used the ::CreateMutex function can someone tell me what it is used for. I looked in MSDN but it does not really tell me much. Thanks!


HANDLE hSingle;

hSingle = ::CreateMutex(NULL, TRUE, "myapp.exe");
if (ERROR_ALREADY_EXISTS == GetLastError())
{
  ::MessageBox(NULL,"Already Open","Error",MB_OK);
  ::ExitProcess(0);
}
0
 
itsmeandnobodyelseCommented:
A mutex is an OS resource that can be used for program-program or program-thread communication. Mostly it is used to give one process an exclusive access to somewhat - say a file, socket, database, while all others have to wait until the mutex got unlocked.

In the code above, one tries to create a new 'named' mutex. That will fail if another prog already had done the same.

Regards, Alex
0
 
b0ot8r123Author Commented:
Thanks for all the help !!!
0
 
jkrCommented:
May I ask why you accept an answer that summarizes the link I pointed you to?
0
 
b0ot8r123Author Commented:
Jkr,

I gave the points to Axter as he had already answered the question "http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20270628.html" See  bkfirebird's 1st response.
0
 
AxterCommented:
>>I gave the points to Axter as he had already answered the question
>>"http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20270628.html
>>See  bkfirebird's 1st response.

This is kind of a gray area, but (IMHO) I think it would be more fair for the points to go to the first person posting the link.
Even if I did answer the question in the link posted.
Other wise, it's like double dipping.

Another option you could and can take would be to split the points.
0
 
itsmeandnobodyelseCommented:
>>I gave the points to Axter as he had already answered the question ...

That is indeed a weak argument.

>> May I ask why you accept an answer that summarizes the link I pointed you to?

I believe that a 'link' isn't as good as a sample code. So, i think the questioner's preference should be accepted.

Regards, Alex


0
 
jkrCommented:
>>See  bkfirebird's 1st response.

These are exactly the responses here I do *not* like. The only ones I like even less are EE search URLs being posted.
0
 
b0ot8r123Author Commented:
I used the code that Axter posted in my application so he got the points.  When I referred to "See  bkfirebird's 1st response" I was just trying to reference the first Comment on this thread.  I'm sorry I upset some of you and I appreciate everyone’s comments and help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.