Q
ql
Unregistered / Unconfirmed
GUEST, unregistred user!
各位师兄晚上好,在下有个困扰许久的问题,最近本人在学习DLL文件的编写,但遇到这样的问题:<br>我想用DLL文件的形式写个登录窗口,在DLL文件加入了ADOConnection和ADOQuery这样两个控件,但在调用时总是有问题,不知道是什么原因,请高手指点一下谢谢,附带原代码:<br>DLL文件部分:<br>library DLL;<br>uses<br> ShareMem,<br> SysUtils,<br> Classes,<br> FDLL in 'FDLL.pas' {Form1};<br>exports<br> GetDLL; <br>{$R *.res}<br>begin<br>end.<br>——————————————————————————————————<br>unit FDLL;<br>interface<br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, DB, ADODB;<br>type<br> TForm1 = class(TForm)<br> Edit1: TEdit;<br> Edit2: TEdit;<br> Button1: TButton;<br> Button2: TButton;<br> Label1: TLabel;<br> Label2: TLabel;<br> ADOConnection1: TADOConnection;<br> ADOQuery1: TADOQuery;<br> procedure FormCreate(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br> procedure GetDLL(var Op,Pass : string);export;<br>var<br> Form1: TForm1;<br> TmpOp,TmpPass : string;<br> switch : boolean;<br>implementation<br>{$R *.dfm}<br>procedure GetDLL(var Op,Pass : string);<br>begin<br> if switch = false then<br> begin<br> switch := true;<br> form1 := tform1.Create(application);<br> end;<br> if form1.ShowModal = IdOk then<br> begin<br> Op := TmpOp;<br> Pass := TmpPass;<br> switch := false;<br> end;<br>end;<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> try<br> begin<br> adoconnection1.Close;<br> adoconnection1.ConnectionString := 'Provider=MSDASQL.1;' +<br> 'Persist Security Info=False;' +<br> 'User ID=sa;' +<br> 'Data Source=QL;' +<br> 'Mode=ReadWrite;' +<br> 'Initial Catalog=traffic';<br> adoconnection1.LoginPrompt := false;<br> adoconnection1.KeepConnection := true;<br> adoconnection1.Connected := true;<br> adoconnection1.Open;<br> adoquery1.Connection := adoconnection1;<br> with adoquery1 do<br> begin<br> Close;<br> SQL.Clear;<br> SQL.Add('select * from sczy');<br> Open;<br> end;<br> if adoquery1.Bof and adoquery1.Eof then<br> begin<br> application.MessageBox('数据库中无可用信息!','系统信息',16);<br> application.Terminate;<br> end<br> else switch := true;<br> end<br> except<br> application.Terminate;<br> end;<br>end;<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> switch := true;<br> if edit1.Text <> '' then<br> begin<br> TmpOp := edit1.Text;<br> TmpPass := edit2.Text;<br> with adoquery1 do<br> begin<br> Close;<br> SQL.Clear;<br> SQL.Add('select * from sczy where');<br> SQL.Add('(工号 = ' + '''' + trim(TmpOp) + '''' + ') and');<br> SQL.Add('(密码 = ' + '''' + trim(TmpPass) + '''' + ')');<br> Open;<br> end;<br> if adoquery1.Bof and adoquery1.Eof then<br> begin<br> application.MessageBox('数据库中无此工号!','系统信息',16);<br> edit1.Text := '';<br> edit2.Text := '';<br> TmpOp := '';<br> TmpPass := '';<br> edit1.SetFocus;<br> ModalResult := IdNo;<br> end<br> else ModalResult := IdOk;<br> end<br> else<br> begin<br> application.MessageBox('工号窗口不能为空!','系统信息',32);<br> edit1.SetFocus;<br> ModalResult := IdNo;<br> end;<br>end;<br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> switch := true;<br> ModalResult := IdOk;<br>end;<br>end.<br><br>调用程序部分:<br>program ShowDLL;<br>uses<br> ShareMem,<br> Forms,<br> FShowDLL in 'FShowDLL.pas' {Form1};<br>{$R *.res}<br>begin<br> Application.Initialize;<br> Application.CreateForm(TForm1, Form1);<br> Application.Run;<br>end.<br>——————————————————————————————————<br>unit FShowDLL;<br>interface<br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, DB, ADODB;<br>type<br> TForm1 = class(TForm)<br> Label1: TLabel;<br> Label2: TLabel;<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br> TMultiplyNum = procedure(var Op,Pass : string);<br> procedure MyGetDLL;export;<br>var<br> Form1: TForm1;<br> StrOp,StrPass : string;<br> MyMultiplyNum : TMultiplyNum;<br> MyHandle : THandle;<br>implementation<br>{$R *.dfm}<br>procedure MyGetDLL;<br>begin<br> MyHandle := LoadLibrary('DLL.dll');<br> if MyHandle <> 0 then<br> begin<br> @MyMultiplyNum := GetProcAddress(MyHandle,'GetDLL');<br> if (@MyMultiplyNum) <> nil then<br> begin<br> MyMultiplyNum(StrOp,StrPass);<br> FreeLibrary(MyHandle);//此处好像有问题<br> end;<br> end;<br>end;<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> MyGetDLL;<br> label1.Caption := '工号:' + StrOp;<br> label2.Caption := '密码:' + StrPass;<br>end;<br>procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);<br>begin<br> if application.MessageBox('是否要退出系统?','系统信息',32 + MB_YesNo) = IDYes then<br> begin<br> application.Terminate;<br> end;<br>end;<br>end.<br>——————————————————————————————————<br>以上代码需要实现的功能是:<br>1、由应用程序调用DLL文件。<br>2、在DLL文件中输入工号和密码,如果数据库中存在则登录成功。<br>3、将在DLL文件中输入的工号和密码传给可执行文件,并在‘Label1’和‘Label2’中显示。<br>需要达到的要求:<br>1、在DLL文件判断所输入的工号和密码正确前不能返回。<br>2、在数据库没打开或不存在时可以关闭整个程序,包括可执行文件。<br>3、本例采用的是动态调用DLL方式。<br>请高手指点迷津,在下先谢谢了!!!<br>另外小弟也是初学,不知道现在水平到底如何,通过这段程序请大家评判一下。<br>虚心向各位师兄请教。