Hello again, lucamar. Thanks for your reply. To get the PID of a program you can use function system.GetProcessID().
I created a little demo for the idea in
http://wiki.freepascal.org/Main_Loop_Hooks#Pipes_and_Process_Termination:
program test2;
{$mode objfpc}{$H+}
uses InterfaceBase,LCLIntf; // need Package "LCLBase"
type
TMyClass = Class(TObject)
procedure ChildExitEvent(AData: PtrInt; AReason: TChildExitReason; AInfo: dword); // of object;
end;
procedure TMyClass.ChildExitEvent(AData: PtrInt; AReason: TChildExitReason; AInfo: dword);
{my Exit-Procedure: must be a method of a Class, because it's usage in
AddProcessEventHandler() is declared as "procedure ... of object"}
begin
writeln('ChildExitEvent: ', AData, ' ', AReason, ' ', AInfo);
end;
var MyClass: TMyClass;
procedure install_ExitProc;
const AData = 0;
var pPEH: PProcessEventHandler; {=pointer}
h: THandle;
begin
h:=system.GetProcessID;
writeln('handle=', h);
pPEH:=AddProcessEventHandler(h, @MyClass.ChildExitEvent, AData);
end;
begin
writeln('hello');
MyClass:=TMyClass.Create;
MyClass.ChildExitEvent(33,cerSignal,55); // only test if this method works
install_ExitProc;
readln;
MyClass.Free;
end.
It compiles with Lazarus 1.8.4 (FPC 3.0.4) but when I start it on Windows 7 (32 bit) I get:
An unhandled exception occurred at $00413F33:
EAccessViolation: Access violation
$00413F33 ADDPROCESSEVENTHANDLER, line 44 of ./include/lclintf.inc
$0040180D INSTALL_EXITPROC, line 5779 of test2.pas
$00401881 main, line 35 of test2.pas
On Linux (Ubuntu 18.04 64 bit) I get:
An unhandled exception occurred at $000000000043387D:
EAccessViolation: Access violation
$000000000043387D line 44 of include/lclintf.inc
$0000000000400DF0 line 28 of test2.pas
$0000000000400E82 line 35 of test2.pas
Line 44 of include/lclintf.inc is:
function AddProcessEventHandler(AHandle: THandle; AEventHandler: TChildExitEvent;
AData: PtrInt): PProcessEventHandler;
begin
Result:=WidgetSet.AddProcessEventHandler(AHandle, AEventHandler, AData);
end;
I ran the program with the debugger, but that did not help me more. I tried older versions of Lazarus and FPC but got the same result, beside that FPC 2.6.4 says also "Runtime Error 210", which means "Object not initialized: When compiled with range checking on, a program will report this error if you call a virtual method without having called its object's constructor". But as far as I can see, the only object is MyClass and that is initialized...
I attached my little demo project. Can anybody help to avoid the Access violation?