#include <string.au3>
#include <GUIConstants.au3>


Run("regedit /s D:\SoftM_Start\Reg\Alkor_Suite_T0.reg")


Dim $useridout
Dim $userid
Dim $useridstd
Dim $useridSoftm
Dim $password
Dim $userpassver
Dim $userpassent
Dim $userpassreg
Dim $NameAS400
Dim $DLL
Dim $Temp
Dim $FehlerCode
Dim $okbutton
Dim $msg
Dim $SecurityHandle
Dim $error

Dim $DLL2
Dim $DLL3
Dim $SecHandle
Dim $ErrHandle
Dim $rc
Dim $securityhandle
Dim $errorhandle
Dim $sSystem
Dim $listhandle

Dim $Test
Dim $Test2
Dim $Test3
Dim $Test4
Dim $Test5


$NameAS400 = RegRead("HKEY_CURRENT_USER\Software\SoftM\Global\Parametersteuerung", "System")
$useridout = RegRead("HKEY_CURRENT_USER\Software\IBM\Client Access Express\CurrentVersion\Environments\My Connections\"& $NameAS400 &"\Communication", "User ID")
$useridstd = RegRead("HKEY_CURRENT_USER\Software\IBM\Client Access Express\CurrentVersion\Environments\My Connections\"& $NameAS400 &"\Communication", "Signon Mode")
$useridsoftm = RegRead("HKEY_CURRENT_USER\Software\SoftM\Global\Environment", "StandardUser")
$userpassreg = RegRead ( "HKEY_CURRENT_USER\Software\IBM\Client Access Express\CurrentVersion\Environments\My Connections\"& $NameAS400 &"\Communication" ,"User PW")
RegWrite ( "HKEY_CURRENT_USER\Software\IBM\Client Access Express\CurrentVersion\Environments\My Connections\"& $NameAS400 &"\Communication" ,"Signon Mode", "REG_DWORD", 1)

$sSystem = $NameAS400

If ($useridsoftm = "" and $useridout = "") or ($useridsoftm = "" and $useridout <> "") Then
	;Opt("GUIOnEventMode", 1) ; Wechsle in den OnEvent Modus
	GUICreate("Anmeldung AS/400", 200, 200)
	GUICtrlCreateLabel("AS/400 UserName:", 42, 35, 131, 27)
	$userid = GUICtrlCreateInput("", 42, 50, 121, 21)
	GUICtrlCreateLabel("AS/400 Passwort:", 42, 85, 111, 17)
	$password = GUICtrlCreateInput("", 42, 100, 121, 21)
	$okbutton = GUICtrlCreateButton("OK", 70, 145, 60)
 
	
	GUISetState(@SW_SHOW)
	While 1
		$nMsg = GUIGetMsg()
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				Exit
			Case $okbutton
				_OKUserPasswort($NameAS400, GUICtrlRead($userid), GUICtrlRead($password))
				GUISetState(@SW_HIDE)
				ExitLoop
		EndSwitch
	WEnd
Else
	if $NameAS400<>"" and $userpassreg And $useridout<>"" Then
		_OKUserPasswort($NameAS400,$useridout,_StringEncrypt(0,$userpassreg,"passwortencrypten",2))    ;Ich hoffe ich habe die Variablen sinngemäss benutzt.
	EndIf
EndIf
If $useridsoftm <> "" Then
	;UserName aus SoftM Suite-Einstellungen ist der führende und wird im IBM Client Access Registry eingetragen/überschrieben
	RegWrite ( "HKEY_CURRENT_USER\Software\IBM\Client Access Express\CurrentVersion\Environments\My Connections\"& $NameAS400 &"\Communication" ,"User ID", "REG_SZ", $useridsoftm)
	$userpassent = _StringEncrypt ( 0, $userpassreg, "passwortencrypten" , 2)
	;As/400-Logon in Windoes-Cach schreiben
	ShellExecute("C:\Program Files\IBM\Client Access\cwblogon", " " & $NameAS400 & " /u " & $useridsoftm & " /p " & $userpassent)
EndIf





$DLL = DllOpen("cwbsy.dll")
$DLL2 = DllOpen("cwbsv.dll")
$DLL3 = DLLOpen("cwbco.dll")


MsgBox(4096,"hallo"," Hier kommts Username: " & GUICtrlRead($userid))
MsgBox(4096,"hallo"," Hier kommts Passwort: " & guictrlread($password))
MsgBox(4096,"hallo"," Hier kommts AS400: " & $NameAS400)

;$Temp = DLLCall($DLL, "Int", "cwbSY_CreateSecurityObj", "Int", $1SecHandle)
;$Temp2 = DLLCall($DLL, "Int", "cwbSY_SetSys", "Int", $1SecHandle, "String", $NameAS400)
;$Temp3 = DLLCall($DLL, "Int", "cwbSY_Logon", "Int", $1SecHandle)
;$Temp4 = DLLCall($DLL2, "Int", "cwbSY_CreateErrHandle", "Int", $1ErrHandle)
;$Temp5 = DLLCall($DLL, "Int", "cwbSY_LogonUser", "Int", $1SecHandle, "String", GUICtrlRead($useridin), "String", GUICtrlRead($userpass), "Int", $1ErrHandle)


		$Temp = _cwbCO_CreateSystem($sSystem, $securityhandle)
        $Temp2 = _cwbCO_SetUserIDEx($securityhandle, $userid)
        $Temp3 = _cwbCO_SetPassword($securityhandle, $password)
        $Temp4 = _cwbCO_Signon($securityhandle, $errorhandle)
		$Temp5 = _cwbCO_CreateSysListHandle($listhandle, $errorhandle)
        ;checkDBUser = rc



MsgBox(4096,"hallo"," Hier kommts CreateSystem Temp: " & $Temp)
MsgBox(4096,"hallo"," Hier kommts SetUserIDEx Temp: " & $Temp2)
MsgBox(4096,"hallo"," Hier kommts SetPassword Temp: " & $Temp3)
MsgBox(4096,"hallo"," Hier kommts Signon Temp: " & $Temp4)
MsgBox(4096,"hallo"," Hier kommts Sys Handle: " & $Temp5)


Func _cwbCO_CreateSystem($sSystem, ByRef $securityhandle)
	$Test = DLLCall($DLL3, "Int", "cwbCO_CreateSystem", $sSystem, $securityhandle)
	MsgBox(4096,"hallo"," Hier kommts createsystem signon Func: " & $Test)
	Return $Test
EndFunc

Func _cwbCO_Signon(ByRef $securityhandle, $errorhandle)
	$Test2 = DLLCall($DLL3, "Int", "cwbCO_Signon", $securityhandle, $errorhandle)
	MsgBox(4096,"hallo"," Hier kommts create signon Func: " & $Test2)
	Return $Test2
EndFunc

Func _cwbCO_SetUserIDEx(ByRef $securityhandle, $userid)
	$Test3 = DLLCall($DLL3, "Int", "cwbCO_SetUserIDEx", $securityhandle, $userid)
	MsgBox(4096,"hallo"," Hier kommts setuseridex signon Func: " & $Test3)
	return $Test3
EndFunc

Func _cwbCO_SetPassword(ByRef $securityhandle, $password)
	$Test4 = DLLCall($DLL3, "Int", "cwbCO_SetPassword", $securityhandle, $password)
	MsgBox(4096,"hallo"," Hier kommts set password signon Func: " &$Test4)
	return $Test4
EndFunc

Func _cwbCO_CreateSysListHandle($listhandle, $errorhandle)
	$Test5 = DLLCall($DLL3, "Int", "cwbCO_CreateSysListHandle", $listhandle, $errorhandle)
	MsgBox(4096,"hallo"," Hier kommts set sys list handle Func: " & $Test5)
	return $Test5
EndFunc



Run("D:\Program Files\SoftM AG\SoftM AG Suite\smumenu.exe")






Func _OKUserPasswort($NameAS400, $useridin, $userpass)
	;As/400-Logon in Windoes-Cach schreiben
	ShellExecute("C:\Program Files\IBM\Client Access\cwblogon.exe", " " & $NameAS400 & " /u " & $useridin & " /p " & $userpass,@SW_HIDE)
	;UserName + UserPassword (verschlüsselt) in Regestri von IBM Client Access schreiben
	RegWrite ( "HKEY_CURRENT_USER\Software\IBM\Client Access Express\CurrentVersion\Environments\My Connections\"& $NameAS400 &"\Communication" ,"User ID", "REG_SZ", $useridin)
	$userpassver = _StringEncrypt(1, $userpass, "passwortencrypten", 2)
	RegWrite ( "HKEY_CURRENT_USER\Software\IBM\Client Access Express\CurrentVersion\Environments\My Connections\"& $NameAS400 &"\Communication" ,"User PW", "REG_SZ", $userpassver)
	;ShellExecute($run, '', '', '', @SW_HIDE)
EndFunc ;==>_start  


