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!

Global $g_bAutoStart = True ; Wenn True, wird dieses Script bei der Benutzeranmeldung automatisch ausgeführt!

; hhexew.exe starten und mit AdlibRegister alle 3 Sekunden (3000 ms) prüfen, ob der ProcessExists "hhpxprojwinframe.exe" existiert.
Run("C:\sog\tools\bin\hhexew.exe -asstartcond -hhproj sogerp -hhfirm 01 hhpxProjWinFrame.exe -mskvar mini -nas -liz vstore","C:\sog\tools\bin\")

AdlibRegister(_CheckAutoStart, 15000) ; Funktion _CheckAutoStart alle 15000 ms aufrufen.
AdlibRegister(_CheckProcess, 3000)    ; Funktion _CheckProcess alle 3000 ms aufrufen.

While 1
	Sleep(100)
WEnd

; Prüfen ob Autostart de/aktiviert werden soll.
Func _CheckAutoStart()
	Local Static $sShortcut = @StartupDir & StringRegExpReplace(@ScriptName, '(.+)\..+', '\\\1\.lnk')
	; Soll dieses Script bei der Benutzeranmeldung automatisch ausgeführt werden?
	If $g_bAutoStart Then
		If Not FileExists($sShortcut) Then
;~ 			ConsoleWrite('> FileCreateShortcut("'&$sShortcut&'")' & @CRLF)
			FileCreateShortcut(@ScriptFullPath, $sShortcut)
		EndIf
	Else
		If FileExists($sShortcut) Then
;~ 			ConsoleWrite('! FileDelete("'&$sShortcut&'")' & @CRLF)
			FileDelete($sShortcut) ; Nein, kein Autostart... die Verknüpfung im @StartupDir wieder löschen!
		EndIf
	EndIf
EndFunc  ;==>_CheckAutoStart

; Prüfen ob 'hhpxprojwinframe.exe' bereits vom User @UserName ausgeführt wird.
Func _CheckProcess()
	Local $aProcessUser, $aProcessList = ProcessList('hhpxprojwinframe.exe')

	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 Return ; 'hhpxprojwinframe.exe' wird bereits vom User @UserName ausgeführt!
		Next
	EndIf

	Shutdown($SD_LOGOFF) ; (0) = Logoff - 'hhpxprojwinframe.exe' wird momentan nicht vom User @UserName ausgeführt!
EndFunc  ;==>_CheckProcess
