How to implement IOTAProjectCompileNotifier of Delphi's ToolsAPI?

前端 未结 3 1173
醉梦人生
醉梦人生 2021-01-07 05:32

I am using Delphi XE IDE. I create a notifier to implement IOTACompileNotifier. After install the expert in the IDE. The code works fine when I compile my project. The n

3条回答
  •  执笔经年
    2021-01-07 06:03

    I think I might be able to answer this. I don't have XE and so I don't appear to have IOTAProjectCompileNotifier. However, the other AddNotifier methods in my ToolsAPI unit suggest it will be declared as:

    function AddNotifier(const ANotifier: IOTAProjectCompileNotifier): Integer;
    

    You call this routine this way:

    i := Project.ProjectBuilder.AddCompileNotifier(TProjectCompileNotifier.Create);
    

    The problem is that nothing takes a reference to the interface returned by TProjectCompileNotifier.Create. You need to do so, like this:

    procedure TCompileNotifier.ProjectCompileStarted(const Project: IOTAProject; Mode: TOTACompileMode);
    var
      i: integer;
      Intf: IOTAProjectCompileNotifier;
    begin
      Intf := TProjectCompileNotifier.Create;
      i := Project.ProjectBuilder.AddCompileNotifier(Intf);
      Project.ProjectBuilder.RemoveCompileNotifier(i);
    end;
    

    You'd need to do likewise in the initialization/finalization code.

    I believe that this really should be considered a bug in the interface reference counting implementation. It has been discussed here on Stack Overflow many times.

提交回复
热议问题