Page 1 of 1

how/where can i get IProcess.ExitCode ?

Posted: Wed Aug 18, 2010 3:02 pm
by lastOne
Hi,

Below is the code i`m using right now to start a process. What i`ll like is to find out how the process is terminated. It will be great if there is a way to get the exit code in the app_endEvent() procedure.

(a general function used to start applications)

Code: Select all

function app_run(const exeFile, sParam: string; endEventProc: cardinal = 0): Boolean;
var
  mP: IProcess;
begin
  Result := False;

  mP := NewProcess( exeFile, sParam );
  mP.PriorityClass := pcBelowNormal;
  if endEventProc <> 0 then
    mP.Notify(MsgHandlerWindow, endEventProc);

  if mP.IsValid and mP.IsStillRunning then
    Result := True;
end;
( event procedure which is called when the process is terminated )

Code: Select all

procedure Tfrm_runApp.app_endEvent(window, msg: cardinal; wParam, lParam: integer; var result: integer);
var xCode: integer;
begin
  if mP.IsStillValid then begin
    xCode := mP.ExitCode;
    _log('exitCode - ' + intToStr(xCode));
  end;

  _log('aplication terminated');
end;
( calling code ... will be great if i could pass the app_endEvent without AddMsgHandler but my knowledge is limited)

Code: Select all

  mP := app_run( ed_exeFile.Text, '', AddMsgHandler(app_endEvent) );

thank you

Posted: Thu Aug 19, 2010 6:10 am
by madshi
I'm not sure if the notification logic is the right solution for you. Maybe you want to create a secondary thread and in it wait for termination of the process (mP.WaitFor). When WaitFor returns, you can then write directly to the log. However, if you go this way, please be aware that your logging code needs to be perfectly thread safe.

Posted: Thu Aug 19, 2010 7:06 am
by lastOne
actually the logging part in the code posted above is just for testing purposes . I need to know how the process ended and this solution seems the easiest one, the problem is that I don`t know how or where to get that ExitCode

Posted: Thu Aug 19, 2010 7:10 am
by madshi
Well, you can store the IProcess interface in a global variable, but that would be a really nasty solution. I think your best bet is to create a secondary thread, as I said.