Original Post
🔒 Locked
application detection
Advertisement
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:
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.
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.
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
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement