Solved

Single Instance Of A Program

Posted on 2004-04-14
15
307 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now