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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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