delphi - 如何实现Delphi的ToolsAPI的IOTAProjectCompileNotifier?

标签 delphi toolsapi

我正在使用 Delphi XE IDE。我创建了一个通知程序来实现 IOTACompileNotifier。在IDE中安装专家后。当我编译我的项目时,代码运行良好。通知程序正在为 ProjectCompileStarted 工作。

第二次编译项目时,Delphi IDE提示:

[Fatal Error] Access violation at address 21B7FBED in module 'delphicoreide150.bpl'. Read of address 00000000

虽然我的表现看起来很奇怪:

var i: integer;
begin
  i := Project.ProjectBuilder.AddCompileNotifier(TProjectCompileNotifier.Create);
  Project.ProjectBuilder.RemoveCompileNotifier(i);
end;

在通知程序中。我只是想显示 ProjectBuilder 的添加和删除编译通知程序似乎无法正常运行,无论我如何使用。

请告知我应该如何实现 IOTAProjectCompileNotifier。

谢谢。

以下是完整的源代码:

type
  TProjectCompileNotifier = class(TInterfacedObject, IOTAProjectCompileNotifier)
  protected
    procedure AfterCompile(var CompileInfo: TOTAProjectCompileInfo);
    procedure BeforeCompile(var CompileInfo: TOTAProjectCompileInfo);
    procedure Destroyed;
  end;

  TCompileNotifier = class(TInterfacedObject, IOTACompileNotifier)
  protected
    procedure ProjectCompileStarted(const Project: IOTAProject; Mode: TOTACompileMode);
    procedure ProjectCompileFinished(const Project: IOTAProject; Result: TOTACompileResult);
    procedure ProjectGroupCompileStarted(Mode: TOTACompileMode);
    procedure ProjectGroupCompileFinished(Result: TOTACompileResult);
  end;

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

var i: integer;

initialization
  i := (BorlandIDEServices as IOTACompileServices).AddNotifier(TCompileNotifier.Create);
finalization
  (BorlandIDEServices as IOTACompileServices).RemoveNotifier(i);
end.

最佳答案

我想我可以回答这个问题。我没有 XE,所以我似乎没有 IOTAProjectCompileNotifier。但是,我的 ToolsAPI 单元中的其他 AddNotifier 方法建议将其声明为:

function AddNotifier(const ANotifier: IOTAProjectCompileNotifier): Integer;

您可以这样调用此例程:

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

问题是没有任何东西引用TProjectCompileNotifier.Create返回的接口(interface)。您需要这样做,如下所示:

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;

您需要在初始化/最终化代码中执行同样的操作。

我认为这确实应该被视为接口(interface)引用计数实现中的错误。已经discussed here on Stack Overflow很多次。

关于delphi - 如何实现Delphi的ToolsAPI的IOTAProjectCompileNotifier?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5439666/

相关文章:

delphi - debugAPI中的框架是什么?

delphi - FreePascal 版 Indy 稳定吗?

delphi - 如何随着鼠标光标显示动态文本

delphi - Delphi中如何通过ToolsAPI获取模块的结构?

delphi - 自动将单元添加到项目中

delphi - 如何使用OTA(开放工具API)指示Delphi IDE刷新修改后的语法高亮颜色?

delphi - 如何在 MS Windows XP 中检索 Unicode CSV 剪贴板数据?

Delphi 6 和 Indy SSL 连接不工作

Delphi:表单可以自行释放吗?