我正在使用 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/