• Advertisement

Archived

This topic is now archived and is closed to further replies.

Checking for second instance of app

This topic is 5559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi in C/C++ and Win32, I would like to know how to check for an existing instance of my application so that i can avoid ever having 2 instances of the same app running. Cheers, Lubby BOOL result = cGamedev.net::Search(char *string, ...); if (FALSE == result) printf("NEW POST\n");

Share this post


Link to post
Share on other sites
Advertisement
A common way is to create a named mutex in an application. If an existing mutex with the same name has already been created, GetLastError() returns ERROR_ALREADY_EXISTS. You can use this fact to check if your program is already running. Now if you want to activate your program window, you''ll have to either find the window handle using FindWindow, or store the window handle somewhere. For the latter, I''d store it in shared memory.

Share this post


Link to post
Share on other sites
You could use something called a mutex. Basically its something that windows uses for some thread syncronisation thing. Search for it in the MSDN help.

(pseudocode)
OpenMutex("MyMutexName")
if mutex successfully opened, return // means a prev instance exists
CreateMutex("MyMutexName")

// program code here, blah

ReleaseMutex(mutex-handle)

cheers,
atjs

Share this post


Link to post
Share on other sites
Ahh Sweet, i was up to page 20 searching :-)

Tested implemented and fully functional, thanks a stack for the help.

heres the full working code fyi


  
appstart:
char mutexname[] = "hellomutex";

HANDLE mutexHandle = CreateMutex(NULL, TRUE, mutexname);
if ( ERROR_ALREADY_EXISTS == GetLastError() )
{
// mutex exists - instance already running

return 0;
}

cleanup:
ReleaseMutex(mutexHandle);


Cheers,
Lubby

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if you register your own window class, FindWindow is another alternative available, and it saves you the open handle.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
FindWindow doesn''t give you an atomic operation, so it doesn''t guarantee a single instance (although it will work in most cases)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i guess i don''t see how your point is anything but moot in this situation. please explain?

the way i see it, either there isn''t a window of your class or there are multiple. who cares if there are 1, 2, 3, ..., at that point? if there are more than one, assuming you''ve chosen a good class name and did the proper coding/testing, then another app(s) is using the same class name. the same can be said for named mutexes. they are interprocess communication objects afterall, which are meant to be used in normal situations across disparate processes.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Here''s the problem. Assume two instances of the .exe start, almost at the same time (if you by mistake happen to double-click twice).

Two processes will start, P1 and P2.

P1 calls FindWindow. No window is found, so P1 thinks (correctly) that he''s the first instance. Ok, so he''ll create the window now. But at this time, the NT scheduler puts P1''s main thread to sleep.

Now P2 calls FindWindow. No window is created yet, so P2 thinks (incorrectly) that he''s the first instance.

The scheduler schedules P1''s main thread, and P1 will create the window.

P2 get scheduled, and he will also create the window. Now we have two instances, which is exactly what we wanted to prevent.




A mutex provides a create-method that lets you know if you created the mutex, or if someone else already created the mutex. That method is atomic, so there''s no way both P1 and P2 both would believe they created the mutex. One will be the creator, the other will just open the mutex.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
now that makes sense. mutex it is then. thanks.

Share this post


Link to post
Share on other sites

  • Advertisement