Prevent 2 copies of the same executaable.

BlitzMax Forums/BlitzMax Beginners Area/Prevent 2 copies of the same executaable.

pangyan(Posted 2005) [#1]
How do I prevent the same executable being executed twice (for example if the player double clicks twice on the icon for the program?


Robert Cummings(Posted 2005) [#2]
This is cool for me too - hows it done? preferbly cross-platform (that is if the mac even lets you)


Difference(Posted 2005) [#3]
For windows:
Const ERROR_ALREADY_EXISTS            = 183

Extern "win32"
	Function CreateMutex:Int(lpMutexAttributes :Byte Ptr, bInitialOwner, lpName : Byte Ptr) = "CreateMutexA@12" 
	Function GetLastError:Int() = "GetLastError@0"
End Extern


Function PreventPrevInstance()

	Local ret = CreateMutex(Null,Null ,"IAMTHEONLYONE".ToCstring())

		If getlasterror () = ERROR_ALREADY_EXISTS 
	
			Notify "You can only start one copy of this program at the same time. ~nThis copy closes now"
			End
		EndIf
		
End Function 



' test program

PreventPrevInstance()

' keep this dialog open to test
Notify "There can be only one..."




taxlerendiosk(Posted 2005) [#4]
Excellent! What about passing the args to the currently-running version? So I could hack it so, e.g., if I click on a ".bmx" file while MaxIDE is already running, it doesn't start a new MaxIDE but the old one loads in the file instead?


Perturbatio(Posted 2005) [#5]
I wonder if you could send the args as a DragDrop event.


EOF(Posted 2005) [#6]
You might want to have a look at this I've posted in the tutorials too:

http://www.blitzmax.com/Community/posts.php?topic=53709

It *should* work for all 3 systems.


Robert Cummings(Posted 2005) [#7]
Thank you, I think this will be perfect.