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

55 Visitors Online


 
...run a program or open an associated file?
Autor: Marc
Homepage: http://www.idev.ch
[ Print tip ]  

Tip Rating (42):  
     


{ Open a file or starts a programm (without parameters) }

procedure OpenFile(FileName: string);
var
  
c: array[0..800] of Char;
begin
  
StrPCopy(c, FileName);
  ShellExecute(Application.Handle, 'open', c, nilnil, SW_NORMAL);
end;

{ Starts a programm with commandline parameters }

procedure OpenProgram(prog, params: string);
var
  
c, p: array[0..800] of Char;
begin
  
StrPCopy(c, prog);
  StrPCopy(p, params);
  ShellExecute(Application.Handle, 'open', c, p, nil, SW_NORMAL);
end;

{ Starts a program and wait until its terminated:
  WindowState is of the SW_xxx constants }

function ExecAndWait(const FileName, Params: string;
  WindowState: Word): Boolean;
var
  
SUInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CmdLine: string;
begin
  
{ Enclose filename in quotes to take care of
    long filenames with spaces. }
  
CmdLine := '"' + FileName + '"' + Params;
  FillChar(SUInfo, SizeOf(SUInfo), #0);
  with SUInfo do
  begin
    
cb := SizeOf(SUInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := WindowState;
  end;
  Result := CreateProcess(nil, PChar(CmdLine), nilnil, False,
    CREATE_NEW_CONSOLE or
    
NORMAL_PRIORITY_CLASS, nil,
    PChar(ExtractFilePath(FileName)),
    SUInfo, ProcInfo);
  { Wait for it to finish. }
  
if Result then
    
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end;

{ Execute a complete shell command line and waits until terminated. }

function ExecCmdLineAndWait(const CmdLine: string;
  WindowState: Word): Boolean;
var
  
SUInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
begin
  
{ Enclose filename in quotes to take care of
    long filenames with spaces. }
  
FillChar(SUInfo, SizeOf(SUInfo), #0);
  with SUInfo do
  begin
    
cb := SizeOf(SUInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := WindowState;
  end;
  Result := CreateProcess(nil, PChar(CmdLine), nilnil, False,
    CREATE_NEW_CONSOLE or
    
NORMAL_PRIORITY_CLASS, nil,
    nil {PChar(ExtractFilePath(Filename))},
    SUInfo, ProcInfo);
  { Wait for it to finish. }
  
if Result then
    
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end;

{ Execute a complete shell command line without waiting. }

function OpenCmdLine(const CmdLine: string;
  WindowState: Word): Boolean;
var
  
SUInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
begin
  
{ Enclose filename in quotes to take care of
    long filenames with spaces. }
  
FillChar(SUInfo, SizeOf(SUInfo), #0);
  with SUInfo do
  begin
    
cb := SizeOf(SUInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := WindowState;
  end;
  Result := CreateProcess(nil, PChar(CmdLine), nilnil, False,
    CREATE_NEW_CONSOLE or
    
NORMAL_PRIORITY_CLASS, nil,
    nil {PChar(ExtractFilePath(Filename))},
    SUInfo, ProcInfo);
end;

 

Rate this tip:

poor
very good


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