Archived

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

Prozak

How to detect another instance of our Game

Recommended Posts

Prozak    898
Hi all, I think this one is really simple, but how does one go about detecting another instance of his running in the system? I think it has to do with the parameters passed to main() or winmain() function... ok, thanx for any help, Season''s Greeting,

[Hugo Ferreira][Positronic Dreams][]

Share this post


Link to post
Share on other sites
jenova    122
that''s correct.

int WINAPI WinMain(HISTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int iCmdShow)
{
/* detect previous instance. */
if (!hPrev) {
}
return (0);
}

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Promit    13246
Better way to do the same:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//Check if a previous existence exists
if(hPrevInstance != NULL)
//exit program
return 0;
}


-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Share this post


Link to post
Share on other sites
jenova    122
Martee: true.

use "CreateMutex".

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
jenova    122
sorry. at your application entry call "CreateMutex". the second time this function is called for the same application "CreateMutex" will fail, thus a previous instance of the application exists.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
S1CA    1418
The correct way to do it:

  
HANDLE g_hMutexAppIsRunning;

BOOL DoIExist( LPCTSTR szMutexName )
{
// try to create a mutex (will fail if already one of that name)

g_hMutexAppIsRunning = CreateMutex( NULL, FALSE, szMutexName );

// Return TRUE if existing semaphore opened

if (g_hMutexAppIsRunning != NULL && GetLastError()==ERROR_ALREADY_EXISTS)
{
CloseHandle(g_hMutexAppIsRunning);
return TRUE;
}

// wasn’t found, new one created therefore return FALSE

return (g_hMutexAppIsRunning==NULL);
}



int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode)
{
// check to see if we are already running

if ( DoIExist(_T("Global\\MyGameName")) == TRUE )
{
// Game found running. If it’s in this session restore it

HWND hWndMe = FindWindow("MyGameWindowClassName", "MyGameWindowName");
if (hWndMe)
{
if ( IsIconic(hWndMe) )
{
ShowWindow( hWndMe, SW_RESTORE );
}
SetForegroundWindow( hWndMe );
}
else
{
MessageBox(NULL, _T("Another User is using MyGameName"), "MyGameName", MB_OK);
}

return -1;
}

.
.
.
.
.



That also properly handles fast user switching in Windows XP (i.e. a different user may already have your game open on a DIFFERENT desktop).

MyGameName is obviously the name of your game.

MyGameWindowName is the title of your application window as passed to CreateWindow().

MyGameWindowClassName is the class name of your application window as passed in your WNDCLASS to RegisterClass() [and to CreateWindow].

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites