Archived

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

nPawn

Any way to see if program is already running?

Recommended Posts

nPawn    110
In windows, I want to make sure that the player doesn''t try to load up the program twice, i''d rather have it make the already running program pop to the front or something. Is there a good way to check if your program is already running when they double click to load it up?

Share this post


Link to post
Share on other sites
Burning_Ice    122
well you can always check for an existing window with FindWindow
and, from the MSDN:
"If you need to detect whether another instance already exists, create a uniquely named mutex using the CreateMutex function. CreateMutex will succeed even if the mutex already exists, but the function will return ERROR_ALREADY_EXISTS. This indicates that another instance of your application exists, because it created the mutex first."



Runicsoft -- latest attraction: obfuscated Brainfuck Interpreter in SML
( This post was made entirely from re-cycled electrons )

Share this post


Link to post
Share on other sites
S1CA    1418

BOOL DoIExist( LPCTSTR szMutexName )
{
HANDLE hMutexAppIsRunning = CreateMutex( NULL, FALSE, szMutexName );

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

return FALSE;
}


blah WinMain( ...blah... )
{

if ( DoIExist(_T("Global\\MyCoolGame")) == TRUE )
{
HWND hWndMe = FindWindow("MyCoolGameWindowClassName", "MyCoolGameWindowName");
if (hWndMe)
{
if ( IsIconic(hWndMe) )
{
ShowWindow( hWndMe, SW_RESTORE );
}
SetForegroundWindow( hWndMe );
}
else
{
MessageBox(NULL, _T("Another user is using MyCoolGame"), "MyCoolGame", MB_OK);
}

return -1;
}



// YOUR GAME GOES HERE
}



As well as detecting an instance of the app already running, it detects when the app is minimised and restores it, also when someone has used XP fast user switching to change user while the game is running.


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

Share this post


Link to post
Share on other sites