Ticket #1680: OneInstance.au3

File OneInstance.au3, 753 bytes (added by willichan, 14 years ago)
Line 
1#cs ----------------------------------------------------------------------------
2 Script Function:
3        Include this at the beginning to make sure only one instance runs
4#ce ----------------------------------------------------------------------------
5#include-once
6
7If _MutexExists(@ScriptName) Then
8    Exit
9EndIf
10
11Func _MutexExists($sOccurenceName)
12    Local $ERROR_ALREADY_EXISTS = 183, $handle, $lastError
13
14    $sOccurenceName = StringReplace($sOccurenceName, "\", ""); to avoid error
15    $handle = DllCall("kernel32.dll", "int", "CreateMutex", "int", 0, "long", 1, "str", $sOccurenceName)
16
17    $lastError = DllCall("kernel32.dll", "int", "GetLastError")
18    Return $lastError[0] = $ERROR_ALREADY_EXISTS
19
20EndFunc  ;==>_MutexExists
21