how/where can i get IProcess.ExitCode ?

delphi package - easy access to kernel objects etc.

how/where can i get IProcess.ExitCode ?

Postby lastOne » Wed Aug 18, 2010 3:02 pm

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

Postby madshi » Thu Aug 19, 2010 6:10 am

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

Postby lastOne » Thu Aug 19, 2010 7:06 am

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

Postby madshi » Thu Aug 19, 2010 7:10 am

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


Return to madKernel

Who is online

Users browsing this forum: No registered users and 1 guest