• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

Limiting an application to only one instance

I need to know how to prevent more than one instance of my application from being loaded at the same time. If a user attempts to load the application while an instance is already running, the currently running instance should be brought up instead. Can anyone tell me how to do this?
0
jhanson040697
Asked:
jhanson040697
1 Solution
 
kanniCommented:
Hi

You can find solutions with sample code in the codeguru.com site.
0
 
kanniCommented:
Hi

Get all the active application window handles,
Compare the Window names with your app name,
If any of the Window name is same as your app names then bring the window to top.
0
 
fkuruczCommented:
there is a more simple solution using a named mutex (see CMutex or as kanni said an example at codeguru.com)

bye
  feri
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
fkuruczCommented:
Ok, ok,
its not that complicated:

in your initialization module insert the following code:

// Check for existence of second Instance, and keep Mutex open
HANDLE hRunOnceMutex;
hRunOnceMutex = CreateMutex(NULL, FALSE, "EsKannNurEinenGeben");
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
    // some code to exit your program
}

you need not to call "CloseHandle(hRunOnceMutex)", because the mutex is freed automatically if your program exits.

bye,
  feri

0
 
wyy_cqCommented:
fkurucz 's solution is common way in win32.
0
 
jhanson040697Author Commented:
The example on codeguru.com worked very well for me.
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.

Join & Write a Comment

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now