Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: multiple copies of WinBatch duplicate apps applications instance more than one two
To preventing the same copy of a Winbatch script from launching more than once:
Put this at the top of your existing script:
;if the window title already exists, then exit this script If WinExist("Just Once") == @TRUE Then Exit ;Then change the window title for the program you are running to "Just Once" WinTitle("~WBT - ", "Just Once") ;Change the name of the window. message("hello", "world")
Now, if a second copy of the winbatch script tries to run, it will check for the window name, find the window and exit.
Put this at the top of your existing script:
;check to see if the Window Title has been changed yet. ;First time run the name changes ;if the window title already exists, then exit this script If WinExist("Just Once") == @TRUE Then Exit ;Then change the window title for the program you are running to "Just Once" Run("winword.exe", "") WinTitle("Microsoft Word", "Just Once") ;Change the name of the window. Message("Hi", "I'm the only copy")
Usually this is done using mutexs, 2 mutexs with the same name can't exist in the computer.
Try to put something unique that identifies your script as the mutex name, and don't forget to close the mutex before you exit the script.
Try this and you'll see that you can't run 2 hinstances:
kernel32=strcat(dirwindows(1),"kernel32.dll") ERROR_ALREADY_EXISTS=183 mutexname="*mywinbatchappnamev1.x*" hmutex=dllcall(kernel32,long:"CreateMutexA",lpnull,long:0,lpstr:mutexname) if DllLastError()==ERROR_ALREADY_EXISTS ;mutex exist an hinstance is running message("Error","Only 1 hinstance is allowed") exit endif ;mutex does not exist, do processing boxopen("","Processing..") timedelay(10) ;close mutex dllcall(kernel32,long:"CloseHandle",long:hmutex)
I wanted a way to see if a current WBT .EXE is already running, and terminate if so. To do this, I look at the .EXEs of all windows and see if there is two instances of itself, in which case, an instance is already running.
#DefineFunction IsRunningUDF(ThisWin) ;check if this program is already running (if this is the second instance ThisExe=StrUpper(FileRoot(WinExeName(ThisWin))) AllWins=WinItemizeEx("",@TRUE,@FALSE) FoundExeCount=0 AlreadyRunning=@NO for x=1 to ItemCount(AllWins,@TAB) ThisWin=ItemExtract(x,AllWins,@TAB) if StrUpper(ThisExe)==StrUpper(FileRoot(WinExeName(ThisWin))) then FoundExeCount=FoundExeCount+1 if FoundExeCount==2 AlreadyRunning=@YES x=ItemCount(AllWins,@TAB) EndIf Next Return (AlreadyRunning) #EndFunction if IsRunningUDF(WinGetActive())==@YES then Exit
Here's a simple solution (for compiled scripts) using the Process Information Extender. The nice thing about this solution is that it activates the first instance before exiting, which helps a lost user.
Addextender("wwprc44I.dll") Title = 'JustOne' ProcessList = tListProc() myExeName = StrCat(@Tab,'JustOne.exe') a = StrIndexNc(ProcessList,myExeName,1,@FwdScan) b = 0 If a>0 Then b = StrIndexNc(ProcessList,myExeName,a+1,@FwdScan) If b>0 WinActivate(Title) Exit EndIf Message(Title,'Hello')
objWMIService = GetObject("WinMgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") colItems = objWMIService.ExecQuery("Select * from Win32_Process where Caption = 'notepad.exe'") count = colItems.count if count > 1 then Exit ; Another instance of AppName exists – terminate this instance colItems = "" objWMIService = ""
Article ID: W13276
Filename: Prevent more than one WB Copy from Being Run.txt
File Created: 2013:04:01:09:19:08
Last Updated: 2013:04:01:09:19:08