Search the Community
Showing results for tags 'unique hardware id mutex'.
-
Similar to other functions around the Forum, this uses _WinAPI_UniqueHardwareID instead to create a unique id based on the user's hardware. If you're familiar with _Singleton or _SingletonPID, then you will understand how this works. Function: #include <APIDiagConstants.au3> #include <StringConstants.au3> #include <WinAPIDiag.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _SingletonHWID ; Description ...: Enforce a design paradigm where only one instance of the script may be running with using a unique id based on the user's hardware. ; Syntax ........: _SingletonHWID($sINIFilePath[, $iFlag = 0]) ; Parameters ....: $sINIFilePath - Filepath of the INI file to store the unique id. ; $iFlag - [optional] Optional parameters. Default is 0. ; 0 - Exit the script with the exit code -1 if another instance already exists. ; 1 - Return the PID of the main executable and without exiting the script too. ; Return values .: Success - 0 No other process is running. ; Failure - The PID of the main executable. ; Author ........: guinness with initial ideas by Valik for _Singleton & KaFu for _EnforceSingleInstance. ; Remarks .......: Requires WinAPIEx.au3 & APIConstants.au3. ; Example .......: Yes ; =============================================================================================================================== Func _SingletonHWID($sINIFilePath, $iFlag = 0) Local $sOccurenceName = IniRead($sINIFilePath, '_SingletonID_', 'GUID', Default) If $sOccurenceName = Default Then $sOccurenceName = _WinAPI_UniqueHardwareID(BitOR($UHID_MB, $UHID_BIOS, $UHID_HDD)) IniWrite($sINIFilePath, '_SingletonID_', 'GUID', $sOccurenceName) EndIf Local $iExtension = StringInStr($sINIFilePath, '.', Default, -1) - 1, _ $sUniqueName = $sINIFilePath If $iExtension Then $sUniqueName = StringLeft($sUniqueName, $iExtension) EndIf Local $iBackslash = StringInStr($sUniqueName, '\', Default, -1) If $iExtension Then $sUniqueName = StringTrimLeft($sUniqueName, $iBackslash) EndIf $sOccurenceName &= '_' & $sUniqueName Local $hAutoItWnd = WinGetHandle($sOccurenceName) If @error Then AutoItWinSetTitle($sOccurenceName & @AutoItPID) $hAutoItWnd = WinGetHandle($sOccurenceName & @AutoItPID) AutoItWinSetTitle($sOccurenceName) Local Const $hControl = ControlGetHandle($hAutoItWnd, '', 'Edit1') ControlSetText($hAutoItWnd, '', $hControl, ControlGetText($hAutoItWnd, '', $hControl) & @CRLF & _ '|SINGLETON_HWID:' & @AutoItPID & @CRLF) Else If $iFlag = Default Then $iFlag = 0 If BitAND($iFlag, 1) Then ; $SINGLETON_HWID $iReturn = Int(StringRegExp(ControlGetText($hAutoItWnd, '', ControlGetHandle($hAutoItWnd, '', 'Edit1')) & @CRLF & _ '|SINGLETON_HWID:0' & @CRLF, _ '\|SINGLETON_HWID:(\d+)\R', $STR_REGEXPARRAYGLOBALMATCH)[0]) Else Exit -1 EndIf EndIf Return $iReturn EndFunc ;==>_SingletonHWIDExample use of Function: #include <MsgBoxConstants.au3> Example() Func Example() If @Compiled Then Local $iSingleton = _SingletonHWID('myUniqueName', 1) If Not $iSingleton Then MsgBox($MB_SYSTEMMODAL, 'PID: ' & @AutoItPID, 'This is the first instance of the program running: ' & $iSingleton) Else MsgBox($MB_SYSTEMMODAL, 'PID: ' & @AutoItPID, 'There is another instance running. This PID is: ' & $iSingleton) EndIf Else MsgBox($MB_SYSTEMMODAL, 'PID: ' & @AutoItPID, 'Please compile the code and run the executable twice to see the two different messages.') EndIf EndFunc ;==>ExampleAnother approach >>