Mattias's code uses WinExecute these days you should use Createproccess or Shellexecute.
But if all you want to do is launch a DOS program you can do it with the suppied library 'fmxutils' located in demos/doc/filemanex. this has an easy to use function
ExecuteFile(const FileName, Params, DefaultDir: string;
ShowCmd: Integer): THandle;
simple as:-
ExecuteFile(FileName, Params or '', DefaultDir or '',0);
You only need the CreateProccess type of solution if your Delphi code needs to wait until the DOS code has finished
for example if the DOS code writes a file and you then want to check that it has been created.
There have been several posts recently with CreateProcess type solutions if that is what you need.
Be careful though some of the example won't always work as they are.
DOS programs differ a lot in their behavour.
Some require that you set the 'Working Directory' parameter of 'CreateProccess' not all the handlers I have seen do this.
Some dont like it if you try to pass the executable as P1 and parameters as P2 but will work if you pass the filename and parameters together as the second parameter of waitproccess.
A word on 'Close on exit'
If you look at the PIF dialog for a DOS progam you will see a close on exit checkbox I tried without sucess to find a way to set this from a Delphi Program. However if you call the DOS executable from batch file and make the last line of that file 'CLS' that will force a close on exit.
A possible problem here is that sometime you might want to write the Batchfile on the fly for example if you are dynamiclly setting parameters.
if you use a Stringlist to do this (saves overhead) it dosent work.
I found that using a Memo component to store the text and setting that last line to 'CLS' does work, so write your batchfile in a memo!
This is my own version of the CreateProcess Handler
apologies to whoever wrote the original.
function WaitProcess(Filename: TFilename; Params: string; Working: string;
Timeout: integer; Visibility: integer): string;
var StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
WaitResult: integer;
begin
Result := 'OK';
ZeroMemory(@StartupInfo, SizeOf(TStartupInfo));
with StartupInfo do
begin
cb := SizeOf(TStartupInfo);
dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
wShowWindow := Visibility;
end;
if fileexists(filename) or fileexists(params) then
begin
if (CreateProcess(nil, Pchar(Filename +' '+ Params), Nil, Nil,
False, NORMAL_PRIORITY_CLASS,
Nil, pchar(working), StartupInfo, ProcessInfo))
then
begin
WaitResult := WaitForSingleObject(ProcessInfo.hProcess, Timeout);
if WaitResult = WAIT_TIMEOUT then
begin
result := 'Timeout has occured';
TerminateProcess(ProcessInfo.hprocess, 1);
end;
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end
else
result := 'Createproccess failed ' + filename + ' '+ Params+ ' '+inttostr(getlasterror);
end
else
result := 'Process file does not exist';
end;
Steve..