Opt('MustDeclareVars', 1)

; Initialize my error handler
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")

#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. Falls nicht, wird der User abgemeldet!!!
Func _CheckProcess()
	Local $bRetrieve = _ProcessRetrieve('hhpxprojwinframe.exe')
	If @error Then Exit MsgBox(16, @ScriptName, 'Error by _ProcessRetrieve("hhpxprojwinframe.exe") - Objekt konnte nicht erstellt werden!')
	If Not $bRetrieve Then Shutdown($SD_LOGOFF) ; (0) = Logoff - 'hhpxprojwinframe.exe' wird momentan nicht vom User @UserName ausgeführt!
EndFunc  ;==>_CheckProcess

Func _ProcessRetrieve($sProcessName, $sHostName = @ComputerName, $sUserName = @UserName)
    Local $sOwner, $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sHostName & "\root\cimv2")
    If Not IsObj($objWMIService) Then Return SetError(1, 0, False)

    Local $colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_Process WHERE Name = "' & $sProcessName & '"')
    For $objItem in $colItems
        $objItem.GetOwner($sOwner)
        If Not @error Then
			ConsoleWrite($objItem.Name & ":" & $objItem.ProcessId & @TAB & $sOwner & @CRLF)
			If $sOwner = $sUserName Then Return True
		EndIf
    Next

    Return False
EndFunc  ;==>_ProcessRetrieve

; This is my custom error handler
Func MyErrFunc()
  ; Die Msgbox kannst du auskommentieren, wichtig ist nur, das mit SetError(1) @error auf 1 gesetzt wird!
  Msgbox(0,'AutoItCOM Test','We intercepted a COM Error !'       & @CRLF  & @CRLF & _
			 'err.description is  : ' & $oMyError.description    & @CRLF & _
			 'err.windescription  : ' & $oMyError.windescription & @CRLF & _
			 'err.number is       : ' & '0x' & Hex($oMyError.number, 8) & @CRLF & _
			 'err.lastdllerror is : ' & $oMyError.lastdllerror   & @CRLF & _
			 'err.scriptline is   : ' & $oMyError.scriptline     & @CRLF & _
			 'err.source is       : ' & $oMyError.source         & @CRLF & _
			 'err.helpfile is     : ' & $oMyError.helpfile       & @CRLF & _
			 'err.helpcontext is  : ' & $oMyError.helpcontext _
			)

  SetError(1)  ; to check for after this function returns
Endfunc  ;==>MyErrFunc
