function RunAsAdmin(ParentForm:TForm;Programm:
String;Parameter:String='
';Directory:String='
';WaitForProgramm:Boolean=False):Boolean;
var ShellInfo : TShellExecuteInfo;
ProcessResult : DWord;
begin
Result := False;
if FileExists(Programm)
then
begin
FillChar(ShellInfo,SizeOf(ShellInfo),0);
ShellInfo.cbSize := SizeOf(ShellInfo);
ShellInfo.Wnd := ParentForm.Handle;
ShellInfo.fMask := SEE_MASK_FLAG_DDEWAIT
OR SEE_MASK_FLAG_NO_UI
OR SEE_MASK_NOCLOSEPROCESS;
ShellInfo.lpVerb := '
runas';
ShellInfo.lpFile := PChar(Programm);
if Parameter <> '
'
then
ShellInfo.lpParameters := PChar(Parameter);
if Directory = '
'
then
Directory := ExtractFilePath(Programm);
ShellInfo.lpDirectory := PChar(Directory);
ShellInfo.nShow := SW_SHOWNORMAL;
Result := ShellExecuteEx(@ShellInfo);
if WaitForProgramm
AND Result
AND (ShellInfo.hProcess <> 0)
then
begin // Es soll gewartet werden UND Erfolgreich gestartet UND Handle bekommen
try
ParentForm.Enabled := False;
while GetExitCodeProcess(ShellInfo.hProcess,ProcessResult)
AND (ProcessResult = STILL_ACTIVE)
do
begin
Application.ProcessMessages;
Sleep(2);
end;
finally
CloseHandle(ShellInfo.hProcess);
ParentForm.Enabled := True;
end;
end;
end;
end;