> Does anyone know of the Linux equivalent to CreateProcess() (in MS
Windows)
> for running an application/command?

I wrote the following function a couple of years ago for use with D5 and
Kylix.  It waits for the process to finish but you can remove that code.  It
took me days to get it right.

function RunProgramWait(const App: String): LongInt;
var AString: String): LongInt;
{$IFDEF WIN32}
var
  ExitCode  : DWord;
  Security  : TSecurityAttributes;
  StartInfo : TStartUpInfo;
begin
  Result := -1;
  with Security do
  begin
    nlength              := SizeOf(TSecurityAttributes);
    binherithandle       := true;
    lpsecuritydescriptor := nil;
  end;
  FillChar(StartInfo,Sizeof(StartInfo),#0);
  with StartInfo do
  begin
    cb          := SizeOf(StartInfo);
    dwFlags     := STARTF_USESHOWWINDOW;//+STARTF_USESTDHANDLES
    wShowWindow := SW_HIDE;
  end;
  if
CreateProcess(nil,PChar(App),@Security,@Security,True,NORMAL_PRIORITY_CLASS+
CREATE_NEW_PROCESS_GROUP,nil,nil,StartInfo,ProcessInfo) then
  begin
    repeat
      Application.ProcessMessages;
      WaitForSingleObject(ProcessInfo.hProcess,250);
      GetExitCodeProcess(ProcessInfo.hProcess,ExitCode);
    until ExitCode <> STILL_ACTIVE;
    Result := ExitCode;
  end;
  CloseHandle(ProcessInfo.hProcess);
  CloseHandle(ProcessInfo.hThread);
{$ELSE} // kylix
var
  ExitCode : DWord;
  ParamList: TStringList;
  Param: Array of PChar;
  p: SmallInt;
begin
  ParamList := TStringList.Create;
  ParamList.CommaText := App;
  ParamList.Text := StringReplace(ParamList.Text,#1#129,'"',[rfReplaceAll]);
  SetLength(Param,ParamList.Count+1);
  for p := 0 to ParamList.Count-1 do
    Param[p] := PChar(ParamList.Strings[p]);
  param[ParamList.Count] := nil;
  Pid := fork;
  if Pid < 0 then
    Result := -1
  else if Pid = 0 then
  begin
    Result := execvp(Param[0], PPChar(@Param[0]));
    Halt;
  end
  else begin
    repeat
      sleep(250);
      Application.ProcessMessages;
      Result := WaitPid(Pid, @ExitCode, WNOHANG);
    until Result > 0;
    if WIFEXITED(ExitCode) then Result := WEXITSTATUS(Exitcode)
    else if WIFSIGNALED(Exitcode) then Result := 2;
  end;
  ParamList.Free;
{$ENDIF}
end;


Regards,

Ross Levis.
http://www.stationplaylist.com

_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to