DELPHI 2007 动态链接库DLL断点调试
马根峰
( 广东联合电子服务股份有限公司, 广州 510300)
1 Delphi 几个经典版本简介
Delphi从1995年的 1.0版本,发展到现在的最新的XE3版本,历经N多版本,但最为经典的几个版本个人觉得应属 7.0、2007和 2010。
Delphi 7.0应该是Delphi用户最多的版本。
Delphi 2007是功能就不多说了,归根结底一句话,它是 AnsiString的最后一个版本,在Delphi 2007中,string类型映射为 AnsiString ,char类型映射为 AnsiChar,Pchar类型映射为 PAnsiChar。所以DELPHI低版本的程序可以较轻松地迁移到DELPHI 2007版本。Delphi 2007也是Delphi程序员很容易上手的晚期版本。
从Delphi2009开始起,到现在的
Delphi XE3为止,都是 unicode 版本。String 类型映射为 UnicodeString 而不是 AnsiString ,Char 类型映射为 WideChar,PChar 类型映射为 PWideChar。
由于Delphi 7.0、2007和 2010在界面上乃至功能上的一些变化,所以在动态链接库DLL断点调试上,有较大的变化。在今后几天的时间中,笔者会以三篇文章来分别详细地介绍Delphi 7.0、2007和 2010这三个版本中的DLL断点调试技术。
本篇文章来详细地介绍 Delphi 2007中的动态链接库DLL断点调试技术。 2
DELPHI 2007 DLL断点设置与DLL调试
在DELPHI 7.0以及以前的版本中,动态链接库的调试方法如下:
点击菜单Run-->Parameters.打开Run Parameters窗口,在Host application中选中宿主程
序,如图1所示。
1
图1 打开Run Parameters窗口,在Host application中选中宿主程序
设置图中断点,然后点击F9或者Run—Run来运行宿主程序Delphi2007_Dll_Debug.exe,但发现断点无效,如图2所示:
2
图2 设置断点后,运行宿主程序 Delphi2007_Dll_Debug.exe,断点无效
点击Project-->Options..,在Project Options窗口中,点击Compiler,选中Debug information,如图3所示:
3
图3 在Project Options窗口中,点击Compiler,选中Debug information
点击Project-->Options..,在Project Options窗口中,点击Linker, 可以看到Include TD32 debug Info和Include remote debug symbols两项为非选中状态,如图4所示:
4
图4 Include TD32 debug Info和Include remote debug symbols两项为非选中状态。
选中Include TD32 debug Info和Include remote debug symbols,如图5所示:
图5 选中Include TD32 debug Info和Include remote debug symbols。
5
然后点击F9或者Run—Run来运行宿主程序Delphi2007_Dll_Debug.exe,但发现断点生效,如图6所示:
图6 运行宿主程序,发现断点生效
在Delphi2007_Dll_Debug.exe,输入 1和2 后点击铵钮“=”,如图7所示:
6
图7 在Delphi2007_Dll_Debug.exe,输入 1和2 后点击铵钮“=”
进入DLL断点调试,如图8所示:
图8 进入DLL的断点调试
7
3
例子中的宿主
程序及DLL程序代码
-------宿主程序代码----- unit UDllDebug; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Buttons, Contnrs , ActiveX, StrUtils ; type
TDll_Add=function(int_1,int_2:integer):integer;stdcall; TfrmDllDebug = class(TForm) Edit1: TEdit; Edit2: TEdit; Label1: TLabel; Edit3: TEdit; BtnAdd: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure BtnAddClick(Sender: TObject); private
{ Private declarations } public
{ Public declarations }
HInst:Thandle; FDll_Add:TFarProc; functionDll_Add:TDll_Add;
//aForeThread:MuliThread; end;
8
var
frmDllDebug: TfrmDllDebug;
implementation
{$R *.dfm}
procedure TfrmDllDebug.FormCreate(Sender: TObject); begin
hinst:=loadlibrary('Magenf_Detai