How to detect another instance of our Game
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,
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.
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.
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.
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.
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.
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.
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.
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.
The correct way to do it:
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement