Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Single Instance Of A Program

Posted on 2004-04-14
15
Medium Priority
?
321 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 400 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

886 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