function RunProcess(ExePath:
String;Param:
String;Username,Domain,Password:
String):Cardinal;
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
var TokenHandle:Cardinal;
begin
Result:=0;
//Existiert dieses File?
if not FileExists(ExePath)
then
raise Exception.CreateFmt('
File "%s" existiert nicht!',[ExePath]);
//User anmelden und Token erhalten
if not LogOnUser(PChar(Username),PChar(Domain),PChar(Password),LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,TokenHandle)
then
raise Exception.CreateFmt('
LogonUser fehlgeschlagen! Errorcode: %d',[GetLastError]);
//StartupInfo-Struktur initialisieren
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
//Werte zuweisen
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW
or STARTF_FORCEONFEEDBACK;
StartupInfo.wShowWindow := SW_SHOW;
//Prozess starten
if not CreateProcessAsUser(
TokenHandle,
@ExePath[1],
NIL,
NIL,
NIL,
True,
NORMAL_PRIORITY_CLASS,
NIL,
NIL,
StartupInfo,
ProcessInfo)
then
raise Exception.CreateFmt('
CreateProcessAsUser für "%s" fehlgeschlagen! Errorcode: %d',[ExePath,GetLastError]);
//Auf Beendigung warten
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
//Exitcode des Prozesses ermitteln
GetExitCodeProcess(ProcessInfo.hProcess, Result);
//Handles freigeben
if ProcessInfo.hProcess <> 0
then
CloseHandle(ProcessInfo.hProcess);
if ProcessInfo.hThread <> 0
then
CloseHandle(ProcessInfo.hThread);
end;