Func _Singleton($sOccurenceName, $iFlag = 0)
Local Const $ERROR_ALREADY_EXISTS = 183
Local Const $SECURITY_DESCRIPTOR_REVISION = 1
Local $tSecurityAttributes = 0
If BitAND($iFlag, 2) Then
; The size of SECURITY_DESCRIPTOR is 20 bytes. We just
; need a block of memory the right size, we aren't going to
; access any members directly so it's not important what
; the members are, just that the total size is correct.
Local $tSecurityDescriptor = DllStructCreate("byte;byte;word;ptr[4]")
; Initialize the security descriptor.
Local $aRet = DllCall("advapi32.dll", "bool", "InitializeSecurityDescriptor", _
"struct*", $tSecurityDescriptor, "dword", $SECURITY_DESCRIPTOR_REVISION)
If @error Then Return SetError(@error, @extended, 0)
If $aRet[0] Then
; Add the NULL DACL specifying access to everybody.
$aRet = DllCall("advapi32.dll", "bool", "SetSecurityDescriptorDacl", _
"struct*", $tSecurityDescriptor, "bool", 1, "ptr", 0, "bool", 0)
If @error Then Return SetError(@error, @extended, 0)
If $aRet[0] Then
; Create a SECURITY_ATTRIBUTES structure.
$tSecurityAttributes = DllStructCreate($tagSECURITY_ATTRIBUTES)
; Assign the members.
DllStructSetData($tSecurityAttributes, 1, DllStructGetSize($tSecurityAttributes))
DllStructSetData($tSecurityAttributes, 2, DllStructGetPtr($tSecurityDescriptor))
DllStructSetData($tSecurityAttributes, 3, 0)
EndIf
EndIf
EndIf
Local $handle = DllCall("kernel32.dll", "handle", "CreateMutexW", "struct*", $tSecurityAttributes, "bool", 1, "wstr", $sOccurenceName)
If @error Then Return SetError(@error, @extended, 0)
Local $lastError = DllCall("kernel32.dll", "dword", "GetLastError")
If @error Then Return SetError(@error, @extended, 0)
If $lastError[0] = $ERROR_ALREADY_EXISTS Then
If BitAND($iFlag, 1) Then
Return SetError($lastError[0], $lastError[0], 0)
Else
Exit -1
EndIf
EndIf
Return $handle[0]
EndFunc ;==>_Singleton