Jump to content
  • Advertisement

Archived

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

nPawn

Any way to see if program is already running?

This topic is 5592 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

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
Advertisement
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

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!