was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

46 Visitors Online


 
... eine Datei ausführen und auf ihr Ende warten?
Autor: Leifcp
[ Tip ausdrucken ]  

Tip Bewertung (12):  
     


{
  This tip allows you to open any document with its
  associated application (not only exe, com) and wait for it to finish.
}


{
  Dieser Tip ermöglicht es, nicht nur normale Programme, sondern auch Dateien,
  die mit Programmen geöffnet werden, auszuführen und darauf zu warten,
  bis sie beendet sind.
}

uses
  
Shellapi;

function StartAssociatedExe(FileName: stringvar ErrorCode: Cardinal): Boolean;
var
  
Prg: string;
  ProcessInfo: TProcessInformation;
  StartupInfo: TStartupInfo;
begin
  
SetLength(Prg, MAX_PATH);
  Result := False;
  ErrorCode := FindExecutable(PChar(FileName), nil, PChar(Prg));
  if ErrorCode >= 32 then
  begin
    
SetLength(Prg, StrLen(PChar(Prg)));
    FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
    with StartupInfo do
    begin
      
cb := SizeOf(TStartupInfo);
      wShowWindow := SW_SHOW;
    end;
    if CreateProcess(PChar(Prg), PChar(Format('%s %s', [Prg, FileName])),
      nilnil, False, NORMAL_PRIORITY_CLASS, nilnil, StartupInfo, ProcessInfo) then
    begin
      
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
      GetExitCodeProcess(ProcessInfo.hProcess, ErrorCode);
      CloseHandle(ProcessInfo.hProcess);
      CloseHandle(ProcessInfo.hThread);
      Result := True;
    end
    else
      
ErrorCode := GetLastError;
  end;
end;

// Example, Beispiel:

procedure TForm1.Button1Click(Sender: TObject);
var
  
ErrorCode: Cardinal;
begin
  
StartAssociatedExe('c:\test.doc', ErrorCode);
end;

 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners