I tried to make an event handler out of a normal procedure, like I always did for TNotifyEvent, but this doesn't seem to work with TGetStrProc. The handler receives an empty string parameter.
program ProcAsTGetStrProc;
uses
System.Classes, Winapi.Windows, System.SysUtils;
type
TMyObject = class
strict private
_onLog: TGetStrProc;
procedure _log(const msg: string);
public
procedure DoTheWork();
property OnLog: TGetStrProc read _onLog write _onLog;
end;
procedure mbox(msg: string);
begin
MessageBox(0, PWideChar(msg), 'Test', 0);
end;
procedure TMyObject.DoTheWork();
begin
_log('Doing the work');
end;
procedure TMyObject._log(const msg: string);
begin
mbox(Format('TMyObject._log: "%s"', [msg]));
if Assigned(_onLog) then _onLog(msg);
end;
procedure ProcLogging(const msg: string);
begin
mbox(Format('ProcLogging: "%s"', [msg]));
end;
function MakeMethod(Data, Code: Pointer): TMethod;
begin
Result.Data := Data;
Result.Code := Code;
end;
var
obj: TMyObject;
begin
obj := TMyObject.Create();
try
obj.OnLog := TGetStrProc(MakeMethod(nil, @ProcLogging));
obj.DoTheWork();
finally
obj.Free();
end;
end.
Expected output
TMyObject._log: "Doing the work"
ProcLogging: "Doing the work"
Actual output
TMyObject._log: "Doing the work"
ProcLogging: ""
What could be wrong here?