Einzelnen Beitrag anzeigen

schwa226

Registriert seit: 4. Apr 2008
400 Beiträge
 
#3

Re: ShellExecuteExW: starten und beenden eines Prozesses

  Alt 27. Aug 2009, 18:53
Delphi-Quellcode:
function ExecAndWait(Filename, Params: String;
                     WindowState: Word = SW_SHOWNORMAL; Wait : Boolean = False): Cardinal;
var
  {$IFDEF UNICODE} ShExecInfoW: SHELLEXECUTEINFOW; {$ENDIF}
  ShExecInfoA: SHELLEXECUTEINFOA;
// MSDN: ShellExecuteEx, ShellExecuteInfo
begin
  Result := 0;
  if (Filename = '') or not FileExists(FileName) then
    exit;
  {$IFDEF UNICODE}
  if Win32IsUnicode then
  begin
    ProgramStarting := True;
    ZeroMemory(@ShExecInfoW, SizeOf(ShExecInfoW));
    ShExecInfoW.Wnd := GetForegroundWindow;
    ShExecInfoW.cbSize := SizeOf(SHELLEXECUTEINFOW);
    ShExecInfoW.fMask := SEE_MASK_NOCLOSEPROCESS;
    ShExecInfoW.lpVerb := 'open';
    ShExecInfoW.lpFile := PWideChar(WideString(Filename));
    ShExecInfoW.lpParameters := PWideChar(WideString(Params));
    ShExecInfoW.lpDirectory := PWideChar(WideString(ExtractFileDir(Filename)));
    ShExecInfoW.nShow := WindowState;

    try
      ShellAPI.ShellExecuteExW(@ShExecInfoW);
      Result := ShExecInfoW.hProcess;
      if Wait then WaitForSingleObject(ShExecInfoW.hProcess, INFINITE);
    finally
      CloseHandle(ShExecInfoW.hProcess);
      ProgramStarting := False;
    end;
  end
  else
  {$ENDIF}
  begin
    ZeroMemory(@ShExecInfoA, SizeOf(ShExecInfoA));
    ShExecInfoA.Wnd := GetForegroundWindow;
    ShExecInfoA.cbSize := sizeof(SHELLEXECUTEINFOA);
    ShExecInfoA.fMask := SEE_MASK_NOCLOSEPROCESS;
    ShExecInfoA.lpVerb := 'open';
    ShExecInfoA.lpFile := PAnsiChar(AnsiString(Filename));
    ShExecInfoA.lpParameters := PAnsiChar(AnsiString(Params));
    ShExecInfoA.lpDirectory := PAnsiChar(AnsiString(ExtractFileDir(Filename)));
    ShExecInfoA.nShow := WindowState;

    try
      ShellExecuteExA(@ShExecInfoA);
      Result := ShExecInfoA.hProcess;
      if Wait then WaitForSingleObject(ShExecInfoA.hProcess, INFINITE);
    finally
      CloseHandle(ShExecInfoA.hProcess);
    end;
  end;
end;
im Result steht somit der Wert von hProcess denn ich dann so beenden will:

TerminateProcess(ExternProgram.hProcess, 1);
Delphi 2010, Update 4 & 5
  Mit Zitat antworten Zitat