Delphi 11.1中FastReports+TeeChart嵌入DLL图表操作报错求助
问题:FastReports图表在DLL中触发访问违例错误
错误现象
添加或编辑FastReports图表时触发访问违例:
exception message : Access violation at address 092E95C7 in module 'NEWREPORTDLL.DLL'. Write of address 00000000. main thread ($5c54): 092e95c7 +007 NEWREPORTDLL.DLL System 18097 +5 TObject.InitInstance 096bcdc4 +010 NEWREPORTDLL.DLL Winapi.GDIPAPI 7277 +1 TGdiplusBase.NewInstance 092e9ba3 +007 NEWREPORTDLL.DLL System 19251 +5 @ClassCreate 096d0df6 +00e NEWREPORTDLL.DLL Winapi.GDIPOBJ 4497 +0 TGPGraphics.Create 09b9088c +010 NEWREPORTDLL.DLL VCLTee.TeeGDIPlus 1274 +1 TGDIPlusCanvas.RecreateGraphics 09b909a8 +048 NEWREPORTDLL.DLL VCLTee.TeeGDIPlus 1305 +6 TGDIPlusCanvas.GetGraphics 09b90dee +0d6 NEWREPORTDLL.DLL VCLTee.TeeGDIPlus 1468 +20 CheckGraphics 09b90ea6 +0ae NEWREPORTDLL.DLL VCLTee.TeeGDIPlus 1504 +31 TGDIPlusCanvas.InitWindow 09af5050 +0a8 NEWREPORTDLL.DLL VCLTee.TeeProcs 2243 +12 InnerDraw 09af517c +010 NEWREPORTDLL.DLL VCLTee.TeeProcs 2279 +7 TCustomTeePanel.Draw 09af64f6 +0ae NEWREPORTDLL.DLL VCLTee.TeeProcs 3322 +19 TCustomTeePanel.NonBufferDraw 09af61c9 +02d NEWREPORTDLL.DLL VCLTee.TeeProcs 3209 +3 TCustomTeePanel.DrawToMetaCanvas 09ba996f +1a3 NEWREPORTDLL.DLL frxChart 1032 +26 TfrxChartView.Draw 0978e70c +044 NEWREPORTDLL.DLL frxClass 12836 +2 TfrxReportComponent.InteractiveDraw
添加图表系列时出现类似错误:
exception message : Access violation at address 092E95C7 in module 'NEWREPORTDLL.DLL'. Write of address 00000000. main thread ($5c54): 092e95c7 +007 NEWREPORTDLL.DLL System 18097 +5 TObject.InitInstance 096bcdc4 +010 NEWREPORTDLL.DLL Winapi.GDIPAPI 7277 +1 TGdiplusBase.NewInstance 092e9ba3 +007 NEWREPORTDLL.DLL System 19251 +5 @ClassCreate 096d3c88 +010 NEWREPORTDLL.DLL Winapi.GDIPOBJ 6665 +0 TGPFont.Create 09b91f62 +23a NEWREPORTDLL.DLL VCLTee.TeeGDIPlus 2020 +53 TGDIPlusCanvas.DoChangedFont 09ad1958 +070 NEWREPORTDLL.DLL VCLTee.TeCanvas 5547 +19 TTeeCanvas.AssignFontSize 09ad198c +018 NEWREPORTDLL.DLL VCLTee.TeCanvas 5552 +1 TTeeCanvas.AssignFont 09af54c9 +025 NEWREPORTDLL.DLL VCLTee.TeeProcs 2616 +4 TCustomTeePanel.TryAssignFont 09b99697 +07b NEWREPORTDLL.DLL VCLTee.TeeGalleryPanel 695 +8 TGalleryChart.SetTitleFontSize
DLL源码
library NewReportDLL; uses ShareMem, System.SysUtils, System.Classes, Winapi.Windows, Winapi.Messages, System.Variants, System.IOUtils, Vcl.Graphics, VCL.Themes, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, uGlobals in 'uGlobals.pas', uDMRPT in 'uDMRPT.pas' {DMRPT: TDataModule}, ufrmRptTest in 'ufrmRptTest.pas' {frmReport}; {$R *.res} function RunReport( cmd: PAnsiChar ):integer; var frmReport: TfrmReport; begin Application.CreateForm(TDMRPT, DMRPT); // create datamodule Application.CreateForm(TfrmReport, frmReport); // create form try frmReport.ShowModal; finally freeandnil(frmReport); freeandnil( DMRpt ); end; end; Exports RunReport name 'RunReport'; end.
环境信息
- Delphi 11.1
- FastReports 2023.1.3 Standard
- Delphi 11.1自带TeeChart版本
- 尝试安装Steema TeeChart VCL/FMX v2023 Standard失败
解决建议
1. 正确初始化/释放GDI+
TeeChart依赖GDI+,DLL中必须显式初始化GDI+,否则会导致对象创建失败。修改DLL代码:
library NewReportDLL; uses ShareMem, System.SysUtils, System.Classes, Winapi.Windows, Winapi.Messages, System.Variants, System.IOUtils, Vcl.Graphics, VCL.Themes, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Winapi.GDIPAPI, Winapi.GDIPOBJ, // 添加GDI+单元 uGlobals in 'uGlobals.pas', uDMRPT in 'uDMRPT.pas' {DMRPT: TDataModule}, ufrmRptTest in 'ufrmRptTest.pas' {frmReport}; var GDIPlusToken: DWORD; {$R *.res} function RunReport( cmd: PAnsiChar ):integer; var frmReport: TfrmReport; begin if Application = nil then Application := TApplication.Create(nil); // 确保Application存在 Application.CreateForm(TDMRPT, DMRPT); Application.CreateForm(TfrmReport, frmReport); try frmReport.ShowModal; finally freeandnil(frmReport); freeandnil(DMRpt); if Application <> nil then Application.Free; // 释放DLL创建的Application end; end; Exports RunReport name 'RunReport'; begin // 初始化GDI+ GdiplusStartup(GDIPlusToken, nil, nil); end. finalization // 释放GDI+ GdiplusShutdown(GDIPlusToken); end.
2. 确保主程序与DLL内存管理器一致
主程序必须第一单元引用ShareMem,否则跨模块的内存分配/释放会导致访问违例。主程序的dpr文件开头要添加:
program MainApp; uses ShareMem, // 必须放在第一个位置 Vcl.Forms, ...
3. 检查TeeChart组件初始化
在DLL的Form或DataModule的OnCreate事件中,添加TeeChart初始化代码:
uses VCLTee.TeeProcs; procedure TDMRPT.DMRPTCreate(Sender: TObject); begin TeeInit; // 初始化TeeChart环境 end;
4. 修复Steema TeeChart安装问题
如果自带TeeChart存在兼容性问题,解决安装失败:
- 关闭所有Delphi进程,以管理员身份运行Steema安装程序。
- 确认安装包对应Delphi 11.1版本,避免版本不匹配。
- 手动安装:打开Steema的组件包文件(通常是
TeeVCLXX.dpk),在Delphi中编译并安装。
5. 避免重复创建全局对象
检查DLL和主程序中是否有重复注册的FastReports或TeeChart组件,确保DLL中的组件注册与主程序不冲突。
内容的提问来源于stack exchange,提问作者C Sharp Newbie
相关产品推荐
相关产品推荐

