Opt('MustDeclareVars', 1)

#Region    ;************ Includes ************
;~ #include <WinAPI.au3>  ; AutoIt <= 3.3.14.2
#include <WinAPIProc.au3> ; AutoIt >= 3.3.14.3
#include <Misc.au3> ; _Singleton
#EndRegion ;************ Includes ************

; Anstelle von @AppDataDir kannst du z.B. auch @UserName nehmen...
_Singleton(@AppDataDir) ; Dieses Script beenden, falls bereits eine Instanz mit diesem $sOccurrenceName (@AppDataDir) läuft!

_CheckProcess()

; Prüfen ob 'hhpxprojwinframe.exe' bereits vom User @UserName ausgeführt wird.
Func _CheckProcess()
	; hhexew.exe starten
	Run("C:\sog\tools\bin\hhexew.exe -asstartcond -hhproj sogerp -hhfirm 01 hhpxProjWinFrame.exe -mskvar mini -nas -liz vstore","C:\sog\tools\bin\")

	; Alle 3 Sekunden (3000 ms) prüfen, ob der ProcessExists "hhpxprojwinframe.exe" existiert.
	While 1
		Sleep(3000)

		Local $sProcessName = 'hhpxprojwinframe.exe', $aProcessUser, $aProcessList = ProcessList($sProcessName)

		If UBound($aProcessList) Then
			For $i = 1 To $aProcessList[0][0] Step 1
				$aProcessUser = _WinAPI_GetProcessUser($aProcessList[$i][1])
				If Not @error And $aProcessUser[0] = @UserName Then
					MsgBox(64, '_CheckProcess()', "'"&$sProcessName&"' mit der PID "&$aProcessList[$i][1]&" wird bereits vom User "&@UserName&" ausgeführt!", 2)
					ContinueLoop ; 'hhpxprojwinframe.exe' wird bereits vom User @UserName ausgeführt!
				EndIf
			Next
		EndIf

		MsgBox(64, '_CheckProcess()', "'"&$sProcessName&"' wird nicht vom User "&@UserName&" ausgeführt - der User "&@UserName&" wird nun abgemeldet!", 3)
;~ 		Local $iShutdown = Shutdown($SD_LOGOFF) ; (0) = Logoff - 'hhpxprojwinframe.exe' wird momentan nicht vom User @UserName ausgeführt!
		Local $iShutdown = Shutdown(BitOR($SD_FORCE, $SD_LOGOFF)) ; (0) = Logoff - 'hhpxprojwinframe.exe' wird momentan nicht vom User @UserName ausgeführt!
		MsgBox(64, '_CheckProcess()', '$iShutdown = ' & $iShutdown & @CRLF)
	WEnd
EndFunc  ;==>_CheckProcess
