function ExecAndWait(
const AFileName, AParams:
String; AShowWindow: Word;
AWarten: Boolean; AUserName, ADomain, APass:
String; AMoniIdx: Integer;
AWindowName:
String;
var AExitCode, APId: DWord;
const ATimeOut: Integer = 0;
const ASetForegroundWnd: Boolean = False): Boolean;
var
SEInfo: TShellExecuteInfo;
LProcInfo: TProcessInformation;
Path:
String;
begin
Result := False;
if GDaProtCreated
then
AddDo('
AFileName AParams: ' + AFileName + '
' + AParams);
try
try
if ExtractFileDir(AFileName) <> '
'
then begin
GetDir(0, Path);
ChDir(ExtractFileDir(AFileName));
end else
GetDir(0, Path);
except end;
try
if AUserName <> '
'
then begin
//Start Process with Logon - raises Exception if fails
daCreateProcessWithLogonW(AUserName, ADomain, APass, AFileName, AParams,
AShowWindow, @LProcInfo);
SetToMoni(LProcInfo.hProcess);
WaitForApp(LProcInfo.hProcess);
CloseHandle(LProcInfo.hProcess);
CloseHandle(LProcInfo.hThread);
Result := True;
end else begin
FillChar(SEInfo, SizeOf(SEInfo), 0);
SEInfo.cbSize := SizeOf(TShellExecuteInfo);
with SEInfo
do begin
fMask := SEE_MASK_NOCLOSEPROCESS
OR SEE_MASK_FLAG_NO_UI;
Wnd := Application.Handle;
lpFile := PChar(AFileName);
if AParams = '
'
then
lpParameters :=
nil
else
lpParameters := PChar(AParams);
nShow := AShowWindow;
end;
if ShellExecuteEx(@SEInfo)
then
begin
// nur zum Test:
if aWarten
then
WaitForSingleObject(SEInfo.hProcess,INFINITE);
WaitForApp(SEInfo.hProcess);
CloseHandle(SEInfo.hProcess);
Result := True;
end else begin
Result := False;
RaiseLastOSError;
end;
end;
//ELSE OF if AUserName <> '' then begin
finally
try
ChDir(Path);
except end;
end;
except
on e:
Exception do
daShowMessage(Format(SPMEErrRunProcess, [ExtractFileName(AFileName)])+#13#10#13#10+
SPMErrMsg+ '
: '+e.
Message+#13#10#13#10+
SPMPath+'
: '+AFileName+#13#10+
SPMParams+'
: '+AParams+#13#10+
SPMStatus+'
: '+IntToStr(AShowWindow)+#13#10+
SPMErrCls+'
: '+e.ClassName);
end;
end;