Solved

Single Instance Of A Program

Posted on 2004-04-14
15
316 Views
Last Modified: 2010-05-18
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!
0
Comment
Question by:b0ot8r123
  • 4
  • 3
  • 3
  • +3
15 Comments
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10827978
0
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10827998
you can use the same without MFC also
0
 
LVL 86

Expert Comment

by:jkr
ID: 10828030
Check out http://support.microsoft.com/default.aspx?scid=kb;en-us;243953 ("HOWTO: Limit 32-bit Applications to One Instance Using C++")
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 30

Expert Comment

by:Axter
ID: 10828054
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
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 10828128
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
 
LVL 14

Expert Comment

by:wayside
ID: 10828365
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
 

Author Comment

by:b0ot8r123
ID: 10828983
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10830473
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
 

Author Comment

by:b0ot8r123
ID: 10850200
Thanks for all the help !!!
0
 
LVL 86

Expert Comment

by:jkr
ID: 10850216
May I ask why you accept an answer that summarizes the link I pointed you to?
0
 

Author Comment

by:b0ot8r123
ID: 10870448
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
 
LVL 30

Expert Comment

by:Axter
ID: 10871228
>>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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10871330
>>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
 
LVL 86

Expert Comment

by:jkr
ID: 10872587
>>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
 

Author Comment

by:b0ot8r123
ID: 10872873
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

830 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