Page 1 of 1

mutex, singleTon

Posted: 2021-Jul-25, 7:20 am
by MigrationUser
27 Dec 2012 10:50
Ed Dyreen


'
I'm about to implement following functions:

Code: Select all

' Set oMutex = mutexUnique_( lsMutex ) 	' Returns a unique file mutex.
' Set oMutex = mutexOpen_( lsMutex ) 	' Attempts to open a file mutex.
' Set oMutex = mutexClose_( loMutex ) 	' Closes a file mutex.
' succes = singleTon_( sMutex, wait )
I can probably do this playing with fileopen for writing and checking the result. But I strongly doubt this is the "correct" way of creating a mutex. In autoIT I would do something like this:

Code: Select all

$handle = DllCall( 'kernel32.dll', 'handle', 'CreateMutexW', 'struct*', $tSecurityAttributes, 'bool', 1, 'wstr', $sOccurenceName )
Thanks,

Last edited by Ed Dyreen (27 Dec 2012 15:57)

----------------------------

#2 27 Dec 2012 12:39
Ed Dyreen


'
Ok so this is the file based mutex I mentioned earlier.

Code: Select all

smutex 	= ""
i 	= 0
iTime 	= 1
'
Do Until IsObject( oFileMutexChild )
	'
	On Error Resume Next
	Set oFileMutex = oFSO.OpenTextFile( "C:\ED\mutex\mutex", iForAppending, True )
	getErr_( Err.Number )
	On Error Goto 0
	'
	If error = 0 Then
		'
		i = i + 1
		On Error Resume Next
		Set oFileMutexChild = oFSO.OpenTextFile( "C:\ED\mutex\mutex" &i, iForAppending, True )
		getErr_( Err.Number )
		On Error Goto 0
		oFileMutex.Close
		'
	End If
	'
	If error <> 0 Then iTime = sleep_( iTime )
	'
Loop
oStdOut.write( vbNewLine &" 'C:\ED\mutex\mutex" &i &"'" )
But I don't like it, I don't like having to write a file just to create a mutex.

sad

Last edited by Ed Dyreen (27 Dec 2012 14:33)

----------------------------

#3 31 Mar 2015 12:50
zoodeef


It's a totally valid approach, but once the singleton is created, there isn't really any ... std::atomic<Singleton*> Singleton::m_instance; std::mutex ...

adfd

----------------------------

#4 22 Jun 2015 15:27
AikonCWD


Use this code

Code: Select all

''Objects
set shell = createObject("wscript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")


''Check the args passed into the script. Specifically looking for the last argument which is the script mutex
Set args = Wscript.Arguments
argsFound = false
For Each arg In args
	argsFound = arg
Next

''If we didn't get any arguments re-run self with a random arg string, this will be the mutex
if argsFound = false then
	runSelf(genRndStr(8))
Else

	''The last argument is the mutex string
	mutex = argsFound
end if 

''remove any other instances of this script
killPastInstances(mutex)



''This sub will kill all instnances of the currently running vbscript that are running under the same interpreter
''but it will not kill it's self
''note, this requires that this script has a uniquite mutex
sub killPastInstances(mutex)

	''Get self name
	scriptName = WScript.ScriptFullName

	Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
	Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process",,48) 
	For Each objItem in colItems 

		if instr(objItem.CommandLine, scriptName) > 0 Then
		  ''If the instance of the script is NOT this instance
		  if not instr(objItem.CommandLine, mutex) > 0 then

		  	''Kill it!
		  	objItem.Terminate()
		  end if
		end if
	Next
end sub


''generates a random string of length "count"
Function genRndStr(Count)
    Randomize
    For i = 1 To Count
        If (Int((1 - 0 + 1) * Rnd + 0)) Then
            genRndStr = genRndStr & Chr(Int((90 - 65 + 1) * Rnd + 65))
        Else
            genRndStr = genRndStr & Chr(Int((57 - 48 + 1) * Rnd + 48))
        End If
    Next
End Function


''re-runs the curernt script with args in cscript if it is running in wscript. current script exits
sub forceCscript
  If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then

	Set args = Wscript.Arguments
	argStr = ""
	For Each arg In args
		argStr = argStr & " " & arg
	Next

    Shell.Run "cscript """ & WScript.ScriptFullName & """" & argStr, 1, False
    WScript.Quit
  End If
end sub



''Runs a new instance of the current script with additional arguments. Current script exits
sub runSelf(extraArgStr)
  
	''Are we runing in C or W script?
	interpreter = "wscript.exe"
	If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
		interpreter = "wscript.exe"
	else
		interpreter = "cscript.exe"
	end if

	''Get current args
	Set args = Wscript.Arguments
	argStr = ""
	For Each arg In args
		argStr = argStr & " " & arg
	Next

	''append new args if required
	if extraArgStr <> "" then argStr = argStr & " " & extraArgStr

    Shell.Run interpreter & " """ & WScript.ScriptFullName & """" & argStr, 1, False
    WScript.Quit
  
end sub
Credits to: kellestine.com/vbscript-mutex/

original thread: https://ss64.org/oldforum/viewtopic.php?id=1636