Skip to main content
gamedev.net
🔒 Locked

application detection

Started by Raxvan Aug 23, 2005 at 7:11 AM 3 replies 650+ views
Original Post
Raxvan
Raxvan
hi all I am creating a project and i am just at the beginning. if a have the game already opened and i accidental enter again the game is there a way to detect the first game application and stop the second application or give a warning message?
arm
arm
If your platform is Windows and your application creates a window you can use FindWindow() from the Win32 API to search for an active window of your application and terminate the program if one is found.

Example:

int main(){  if(FindWindow("MyWindowClassName", NULL))  {    MessageBox(0, "Application already running", "Error", 0);    return(0);  }   else  {    // Create window here  }}


If your program does not create a window, you could always create a named mutex and look for that instead. I suppose even creating a global atom would do although an object that is automatically cleaned up by the OS is probably best in case your app crashes and leaves something behind.


arm.
stylin
stylin
Alternatively, you can tell Windows to create a mutex for you when your program starts. If you try and run your program again without exiting, you will fail to create the mutex, since you already created it. If it fails, you know there is another instance of your program running.
Quote:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createmutex.asp
If the function fails, the return value is NULL. To get extended error information, call GetLastError.

If the mutex is a named mutex and the object existed before this function call, the return value is a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Raxvan
Raxvan
tnx
you all are very helpful

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.