whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

32 Visitors Online


 
...execute a document and wait for it to finish?
Autor: Leifcp
[ Print tip ]  

Tip Rating (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;

 

Rate this tip:

poor
very good


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