StPendl
May 8, 2011
=Allowing only one instance of your program=//[[user:StPendl|1304860064]]////[[user:StPendl|1304860330]]// [[toc]] ---- ==The Basics== Every now and then one wants to make sure that only one instance of his application is running to prevent problems with shared resources when multiple instances are run concurrently. This is called **mut**ually **ex**clusive access, in short Mut-Ex or Mutex. MSDN has a nice article about [[@http://msdn.microsoft.com/en-us/library/ms684266(VS.85).aspx|Mutex Objects]], which you should read to get a general understanding of the subject. ==The API functions involved== ===The CreateMutex function=== The first function is the one we use to create a mutext, which is the [[@http://msdn.microsoft.com/en-us/library/ms682411(VS.85).aspx|CreateMutex Function]] It receives the security attributes, initial owner and name of our mutex object to create. We do not want to grant other processes or threads access to our mutex, so we use NULL for the security attributes. We want to be the initial owner of the mutex to prevent subsequent instances of our program to be run, so we set that to TRUE = 1. We select a unique name for our mutex to have it easily separated from other mutex objects. Here is the code to do all of this. [[code format="lb"]] ' inherit the default security attributes lpMutexAttributes = _NULL ' optain initial ownership by setting this to 1 bInitialOwner = 1 ' use a unique name for the mutex lpName$ = "LBMutExDemo" calldll #kernel32, "CreateMutexA",_ lpMutexAttributes as ulong,_ bInitialOwner as long, _ lpName$ as ptr,_ hMutex as ulong [[code]] ===The GetLastError function=== Thefirstsecond function is the one we use tocreate a mutext,check if our mutext is already in use, which is the [[@http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx|GetLastError Function]] Itreceivesreturns an error indicating that thesecurity attributes, initial owner and name of ourmutexobject to create.is already present, which is ERROR_ALREADY_EXISTS = 183.WeIf the mutex already exists, we donotwantto grant other processes or threads access toourmutex, so we use NULL for the security attributes. We wantprogram tobenotify theinitial owneruser ofthe mutex to prevent subsequent instances of our program to be run, so we set that to TRUE = 1. We select a unique name for our mutex to have it easily separated from other mutex objects.this situation and end gracefully. Here is the code to do all of this. [[code format="lb"]] 'inheritget thedefault security attributes lpMutexAttributes = _NULLlast error, so we know if there is already an instance running calldll #kernel32, "GetLastError",_ LastError as ulong 'optain initial ownership by setting this to 1 bInitialOwnercheck if there is already an instance running (ERROR_ALREADY_EXISTS =1183) if LastError = 183 then calldll #kernel32, "CloseHandle",_ hMutex as ulong,_ result as long' use a unique name for the mutex lpName$ = "LBMutExDemo"' exit if there is already an instance running notice "Exiting!"; chr$(13);_ "Another instance of this program is already running!" end end if [[code]]calldll #kernel32, "CreateMutexA",_ lpMutexAttributes as ulong,_ bInitialOwner as long, _ lpName$ as ptr,_ hMutex as ulong [[code]]==Third Part Title== Text here. [[code format="lb"]] 'code here [[code]]