Einzelnen Beitrag anzeigen

Benutzerbild von fkerber
fkerber
(CodeLib-Manager)

Registriert seit: 9. Jul 2003
Ort: Ensdorf
6.723 Beiträge
 
Delphi XE Professional
 
#4

Re: Programm starten ohne Shellexecute

  Alt 31. Aug 2007, 17:10
sh17 und winkel79 stellen in diesem Thread eine Funktion vor, die es auch ermöglicht ein Programm unter Vista (mit aktivierter UAC) zu starten, ohne dass es zu Fehlermeldungen kommt, weil Rechte fehlen:

Delphi-Quellcode:
function ExecAndWait(Filename, Params: Widestring; WindowState: word = SW_SHOWNORMAL): boolean;
var
  ShExecInfoW: SHELLEXECUTEINFOW;
  ShExecInfoA: SHELLEXECUTEINFOA;
  r : Cardinal;
begin
  Result := false;
  if Filename = 'then exit;
  if not WideFileExists(FileName) then
    exit;

  if Win32IsUnicode then
  begin
    ZeroMemory(@ShExecInfoW, SizeOf(ShExecInfoW));
    ShExecInfoW.Wnd := GetForegroundWindow;
    ShExecInfoW.cbSize := sizeof(SHELLEXECUTEINFOW);
    ShExecInfoW.fMask := SEE_MASK_NOCLOSEPROCESS;
    ShExecInfoW.lpVerb := 'open';
    ShExecInfoW.lpFile := PWideChar(Filename);
    ShExecInfoW.lpParameters := PWideChar(Params);
    ShExecInfoW.lpDirectory := PWideChar(WideExtractFileDir(Filename));
    ShExecInfoW.nShow := WindowState;
    Result := ShellExecuteExW(@ShExecInfoW);
  end
  else
  begin
    ZeroMemory(@ShExecInfoA, SizeOf(ShExecInfoA));
    ShExecInfoA.Wnd := GetForegroundWindow;
    ShExecInfoA.cbSize := sizeof(SHELLEXECUTEINFOA);
    ShExecInfoA.fMask := SEE_MASK_NOCLOSEPROCESS;
    ShExecInfoA.lpVerb := 'open';
    ShExecInfoA.lpFile := PChar(AnsiString(Filename));
    ShExecInfoA.lpParameters := PChar(AnsiString(Params));
    ShExecInfoA.lpDirectory := PChar(AnsiString(WideExtractFileDir(Filename)));
    ShExecInfoA.nShow := WindowState;
    Result := ShellExecuteExA(@ShExecInfoA);
  end;
  try
    if Result then
    begin
      if Win32IsUnicode then
        r := WaitForSingleObject(ShExecInfoW.hProcess, INFINITE)
      else
        r := WaitForSingleObject(ShExecInfoA.hProcess, INFINITE);
    end;
  finally
    if Win32IsUnicode then
      CloseHandle(ShExecInfoW.hProcess)
    else
      CloseHandle(ShExecInfoA.hProcess);
  end;
end;
Win32IsUnicode steht dabei für ein globales
(Win32Platform = VER_PLATFORM_WIN32_NT) Weitere Infos gibt es hier:
http://msdn.microsoft.com/library/de...lexecuteex.asp
http://msdn.microsoft.com/library/de...xecuteinfo.asp
Frederic Kerber
  Mit Zitat antworten Zitat