Solved

Single Instance Of A Program

Posted on 2004-04-14
15
312 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

777 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