how/where can i get IProcess.ExitCode ?

delphi package - easy access to kernel objects etc.
Post Reply
lastOne
Posts: 9
Joined: Wed Sep 03, 2008 4:50 am
Contact:

how/where can i get IProcess.ExitCode ?

Post 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
madshi
Site Admin
Posts: 10338
Joined: Sun Mar 21, 2004 5:25 pm

Post 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.
lastOne
Posts: 9
Joined: Wed Sep 03, 2008 4:50 am
Contact:

Post 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
madshi
Site Admin
Posts: 10338
Joined: Sun Mar 21, 2004 5:25 pm

Post 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.
Post Reply