(一)通过向导DLL Wizard新建一个动态链接库,取名为:DLLPro.dpr。说明:当在DLL工程文件中使用了String类型时,要有 uses ShareMem ,不过建议使用PChar类型。其代码如下: library DLLPro; //编译生成DLLPro.dll文件。 uses ActiveX, Forms, Windows, SysUtils, Classes, DLLUnt in ‘DLLUnt.pas‘; {$R *.res} var DllApp:TApplication; //全局变量DLLApp默认初始化,即,DLLApp:=Application. procedure CreateForm1(app:TApplication;parentform: TForm); export;stdcall; var Form1: TForm1; begin CoInitialize(nil); try Application:=app; Form1:=Tform1.Create(parentform); form1.MyParentForm:= parentform; //MyParentForm在DLLUnt.pas中定义为公共变量 form1.myparentApplication:=app; //MyParentApplication在DLLUnt.pas中定义为公共变量 Form1.Show; finally CoUninitialize; end; end; procedure ExitDll(Reason:Integer); begin if Reason = DLL_PROCESS_DETACH then // DLL_PROCESS_DETACH值在DLL死亡时执行。 begin Application:=DllApp; end; end; exports CreateForm1; begin DllApp:=Application; //该语句可以省略。 DllProc:=@ExitDll; //变量DllProc是SysUtils单元的一个Pointer类型的变量。 end. <===============================================================================> (二)创建DLL(动态链接库)的单元文件,取名为:DLLUnt.pas,该单元文件有与其配套的窗体文件DLLUnt.dfm;有一个ADOConnection1组件,必须定义在TForm1类中,否则编译时出错:Access Violence其部分代码如下: unit DLLUnt; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, Buttons,ADODB,DB; type TForm1 = class(TForm) Panel0: TPanel; GroupBox1: TGroupBox; Panel1: TPanel; Panel2: TPanel; ……………… CheckBox891: TCheckBox; Edit1: TEdit; SpeedButton1: TSpeedButton; ADOConnection1: TADOConnection; procedure Edit1KeyPress(Sender: TObject; var Key: Char); procedure SpeedButton1Click(Sender: TObject); procedure Edit1Change(Sender: TObject); private { Private declarations } public { Public declarations } MyParentForm: TForm; MyParentApplication: TApplication; end; var Form1: TForm1; implementation {$R *.dfm} ……………… procedure TForm1.SpeedButton1Click(Sender: TObject); var query:TADOquery; Count,I,RedNum,J,CheckNum:integer; RedName,LabName:string; begin if (edit1.Text=‘‘)or(length(edit1.Text)<7)or(StrToFloat(edit1.Text)<2008001) then begin showmessage(‘Error‘); exit; end; query:=Tadoquery.Create(nil); query.Connection:=ADOConnection1; query.Close; query.SQL.Clear; query.SQL.Add(‘select * from gyWinNum where Id>=‘+quotedStr(edit1.Text)+‘ order by Id ‘); query.Open; Count:=1; CheckNum:=0; while (Not query.Eof)and(Count<28) do ……………… end. <===============================================================================> (三)创建项目主窗体的工程文件,取名:MainForm.dpr,其代码如下: program MainPro; //编译生成MainPro.exe文件。 uses Forms, MainUnt in ‘MainUnt.pas‘ {MainForm}; {$R *.res} begin Application.Initialize; Application.CreateForm(TMainForm, MainForm); Application.Run; end. <================================================================================> (四)创建项目主窗体的单元文件,取名:MainUnt.pas,其代码如下: unit MainUnt; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,ADODB; //由于被调文件DLLPro.dll中含义数据集控件,在这里必须要有声明。 type TMainForm = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var MainForm: TMainForm; implementation {$R *.dfm} procedure CreateForm1(app: TApplication;parentform: TForm);stdcall; external‘DLLPro.dll‘; procedure TMainForm.Button1Click(Sender: TObject); begin CreateForm1(Application,self); end; end. <================================================================================> 另外:创建项目主窗体的单元文件,取名:MainUnt.pas,其代码也可以如下: unit MainUnt; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,adodb; type TMainForm = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; T_CreateForm1=procedure(app: TApplication;parentform: TForm);stdcall; //定义T_CreateForm1类型 var MainForm: TMainForm; implementation {$R *.dfm} procedure TMainForm.Button1Click(Sender: TObject); var dllHandle: THandle; Procaddr: FarProc; CreateForm1: T_CreateForm1; begin dllHandle:=LoadLibrary(‘DLLPro‘); Procaddr:=GetProcAddress(dllHandle,‘CreateForm1‘); if Procaddr <> nil then begin CreateForm1:=procaddr; //过程名,标识它的入口地址,是过程指针。 CreateForm1(Application,self); end; end; end. 到此为止,结束。 好文要顶 关注我 收藏该文