主程序调用DLL窗体 cannot assign a TFont to a TFont 问题的彻底解决方案
2010-03-08 13:49
681 查看
主程序调用DLL窗体 cannot assign a TFont to a TFont 问题的彻底解决方案
在网上搜索N久,总结并实战出来的经验和大家分享
1、中国的网站链接大多建议的方案是 project->options选项的packages标签页面勾选 'Buidl with runtime packages'
我个人在D7中测试确实能够解决问题。但是程序发布的时候需要带很多bpl包。
2、在国外网站搜索到另一种解决方案,就是通过修改系统的controls.pas和graphics.pas两个文件来彻底解决上述问题。
具体链接地址为:http://tengco.spaces.live.com/Blog/cns!689EE398F7BFBE58!150.entry http://www.delphi3000.com/articles/article_4957.asp?SK=
下面是我的程序做法:
首先、把系统的controls.pas和graphics.pas拷贝到自己的工程目录,并做如下修改
第一步、在controls.pas 的声明部分加入
function GetControlAtom : pointer;
第二步、在controls.pas 的实现部分加入
function GetControlAtom : pointer;
begin
result := @ControlAtom;
end;
第三步、修改graphics.pas的TCanvas.SetFont函数
procedure TCanvas.SetFont(Value: TFont);
begin
FFont.Assign(Value);
end;
给上面的代码加上 try..except保护,编程如下:
procedure TCanvas.SetFont(Value: TFont);
begin
try
FFont.Assign(Value);
except
on Exception do;
end;
end;
第四步、在DLL工程中引入我们修改好的两个文件;
第五步、DLL工程的初始化
procedure myDllProc(dWseason: DWORD);
var
p:^word;
begin
case dWseason of
DLL_PROCESS_ATTACH: begin
oldApp := Application; //保存DLL的Application
oldScr := Screen; //保存DLL的Screen
p := Controls.GetControlAtom;
OldControlAtom := p^;
CoInitialize(nil); //使用了ADO控件所以需要调用此方法
end;
DLL_PROCESS_DETACH: begin
p := Controls.GetControlAtom;
p^ := OldControlAtom;
application := oldApp; //恢复Dll的Application
Screen := oldScr; //恢复Dll的Screen
CoUninitialize(); //使用了ADO控件所以需要调用此方法
end;
end;
end;
procedure InitDll(var app:tapplication; var scr:Tscreen; RealControlAtom:integer);
var
p:^word;
begin
p := controls.GetControlAtom;
if scr <> nil then screen := scr;
application := app;
p^ := RealControlAtom;
end;
library testdll;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
ShareMem,
SysUtils,
Classes,
windows,
Controls in 'Controls.pas',
Graphics in 'Graphics.pas';
exports
InitDll;
begin
DllProc := @myDllProc;
myDllProc(DLL_PROCESS_ATTACH); //DLL入口初始化
end.
主程序调用示例:
...
p := controls.GetControlAtom;
InitDll(application,screen,p^);
...
在网上搜索N久,总结并实战出来的经验和大家分享
1、中国的网站链接大多建议的方案是 project->options选项的packages标签页面勾选 'Buidl with runtime packages'
我个人在D7中测试确实能够解决问题。但是程序发布的时候需要带很多bpl包。
2、在国外网站搜索到另一种解决方案,就是通过修改系统的controls.pas和graphics.pas两个文件来彻底解决上述问题。
具体链接地址为:http://tengco.spaces.live.com/Blog/cns!689EE398F7BFBE58!150.entry http://www.delphi3000.com/articles/article_4957.asp?SK=
下面是我的程序做法:
首先、把系统的controls.pas和graphics.pas拷贝到自己的工程目录,并做如下修改
第一步、在controls.pas 的声明部分加入
function GetControlAtom : pointer;
第二步、在controls.pas 的实现部分加入
function GetControlAtom : pointer;
begin
result := @ControlAtom;
end;
第三步、修改graphics.pas的TCanvas.SetFont函数
procedure TCanvas.SetFont(Value: TFont);
begin
FFont.Assign(Value);
end;
给上面的代码加上 try..except保护,编程如下:
procedure TCanvas.SetFont(Value: TFont);
begin
try
FFont.Assign(Value);
except
on Exception do;
end;
end;
第四步、在DLL工程中引入我们修改好的两个文件;
第五步、DLL工程的初始化
procedure myDllProc(dWseason: DWORD);
var
p:^word;
begin
case dWseason of
DLL_PROCESS_ATTACH: begin
oldApp := Application; //保存DLL的Application
oldScr := Screen; //保存DLL的Screen
p := Controls.GetControlAtom;
OldControlAtom := p^;
CoInitialize(nil); //使用了ADO控件所以需要调用此方法
end;
DLL_PROCESS_DETACH: begin
p := Controls.GetControlAtom;
p^ := OldControlAtom;
application := oldApp; //恢复Dll的Application
Screen := oldScr; //恢复Dll的Screen
CoUninitialize(); //使用了ADO控件所以需要调用此方法
end;
end;
end;
procedure InitDll(var app:tapplication; var scr:Tscreen; RealControlAtom:integer);
var
p:^word;
begin
p := controls.GetControlAtom;
if scr <> nil then screen := scr;
application := app;
p^ := RealControlAtom;
end;
library testdll;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
ShareMem,
SysUtils,
Classes,
windows,
Controls in 'Controls.pas',
Graphics in 'Graphics.pas';
exports
InitDll;
begin
DllProc := @myDllProc;
myDllProc(DLL_PROCESS_ATTACH); //DLL入口初始化
end.
主程序调用示例:
...
p := controls.GetControlAtom;
InitDll(application,screen,p^);
...
相关文章推荐
- 主程序调用DLL窗体cannot assign a class to a class问题的彻底解决方案
- 调用DLL窗体 cannot assign a TFont to a TFont 问题
- 关于Dll中“cannot assign a tfont to a tfont ”问题
- 我在DLL中建立了一个TImage,但调用时出现错误"Cannot assign a TFont to a TFont"
- DLL中报'Cannot assign a TFont to a TFont'的问题
- DLL:cannot assign a tfont to a tfont
- jnative在linux下对c程序的动态链接库的调用问题及解决方案
- net.sf.ezmorph.bean.MorphDynaBean cannotbe cast to xxx 关于JSON对象解析List<XXX>在调用时出错问题
- Delphi程序调用C#.Net编译的DLL并打开窗体(详解)
- c#程序调用c++编写dll需要注意问题
- 64位进程调用32位dll的解决方法 / 程序64位化带来的问题和思考
- C#开发的GDAL程序部署遇到的问题 (无法加载 DLL“gdal_wrap” Unable to load DLL 'gdal_wrap')
- VS2010 "缺少dlmgr.dll 无法继续执行代码。重新安装程序可能会解决此问题"解决方案
- vs2012 调用外部代码问题 打开项目一直加载外部dll是程序中断!!
- jnative在linux下对c程序的动态链接库的调用问题及解决方案
- C#调用C++编写的COM DLL封装库的问题解决方案
- C#调用MFC程序DLL库——MFC项目DLL生成问题、DLL引用链问题、DLL读取配置文件路径问题
- Unable to load DLL 'SQLite.Interop.dll' 问题的解决方案
- 关于VS2010 rcxdti.dll is unable to load rcxdtiui.dll加载失败问题的解决方案
- JAVA 通过 JNA 调用 C程序 dll 函数传入中文参数问题