Files Delphi

Title: run a program or open an associated file?
{ 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, nil, nil, 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), nil, nil, 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), nil, nil, 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), nil, nil, False,
CREATE_NEW_CONSOLE or
NORMAL_PRIORITY_CLASS, nil,
nil {PChar(ExtractFilePath(Filename))},
SUInfo, ProcInfo);
end;