上次说到使用插件来实现应用程序功能的扩充。但是后来实现的时候发现一个问题。就是在DLL中的窗体实现加载的时候,居然会报告一个错误:
Can't Assign A TFont To A TFont,经过多方的查找,找到如下资料:
21. Can I pass objects to a DLL? (Also: Why does it say 'Cannot assign a TFont to a TFont'?)
Yes, you can, but there's a catch. The DLL will not use the same classes as the main program even when compiled from the same source. Objects will look the same, but their classes will not compare equal. That's why assigning a TFont value from the DLL to a TFont property in the main program (or the other way around) doesn't work: the Assign procedure is looking for the main program's TFont class, and never recognises the DLL's TFont object.
Using packages instead of normal DLLs solves this problem. Packages use the main program's classes. This is the preferred solution.
意思是说在DLL中的类和其宿主程序中的类是不同的,哪怕他们的编译代码相同,他们也是不同的类型,说最好的解决方案是使用 Delphi的包。
另一个方法是:
In this you have to edit graphics.pas found in /source/vcl.
First find the code below
procedure TCanvas.SetFont(Value: TFont);
begin
FFont.Assign(Value);
end;
and add a try..except statement like so
procedure TCanvas.SetFont(Value: TFont);
begin
try
FFont.Assign(Value);
except
on Exception do;
end;
end;
then recompile.
修改Delphi的Graphics的TCanvas的SetFont过程,在那添加一个异常处理。这样则可跳过处理,但是似乎不是什么好的解决方案。
无奈之下,只好采用Delphi的包来实现插件。但是采用包的同时,我却发现一个问题,在反复的
LoadPackage和UnLoadPackage的时候,程序则会出现错误!