delphi dll的两种调用方式演示代码
转自:http://blog.csdn.net/trassion/article/details/8439154
unit TestDLL_Pas; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Edit2: TEdit; Button2: TButton; Edit3: TEdit; Edit4: TEdit; CheckBox1: TCheckBox; Button3: TButton; Edit5: TEdit; Edit6: TEdit; Edit7: TEdit; Edit8: TEdit; Button4: TButton; CheckBox2: TCheckBox; Button5: TButton; Button6: TButton; Edit9: TEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; _DLLMoudle: THandle; //动态调用方式、先定义函数、后面在通过Button5Click过程调入个函数接口地址 _GetPassWord:function (Pass:string):string;stdcall; _CheckPassWord:Function(SourcePass:string;EncryPass:string):boolean;stdcall; _StrEncrypt:Function(s: string; key: word): string;StdCall; _StrDecrypt:Function(s: string; key: word): string;StdCall; _GetRegistCode:Function(Pass:string):String;StdCall; _CheckRegistCode:Function(CompanyName,RegistCode:string):Boolean;StdCall; //下面为静态调用方式 function GetPassWord(Pass:string):ShortString;stdcall;external 'MyHRDLL.dll'; function CheckPassWord(SourcePass,EncryPass:string):Boolean;stdcall;external 'MyHRDLL.dll'; function StrEncrypt(s: string; key: word): ShortString;stdcall;external 'MyHRDLL.dll'; function StrDecrypt(s: string; key: word): ShortString;stdcall;external 'MyHRDLL.dll'; function GetRegistCode(Pass:string):ShortString;stdcall;external 'MyHRDLL.dll'; function CheckRegistCode(CompanyName,RegistCode:string):Boolean;stdcall;external 'MyHRDLL.dll'; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin Edit2.Text := GetPassWord(Edit1.Text); end; procedure TForm1.Button2Click(Sender: TObject); var _bool :Boolean; begin _Bool := CheckRegistCode(Edit3.Text,Edit4.Text); CheckBox1.Checked := _bool; end; procedure TForm1.Button3Click(Sender: TObject); begin edit6.Text := GetRegistCode(edit5.Text); end; procedure TForm1.Button4Click(Sender: TObject); begin CheckBox2.Checked := CheckPassWord(Edit7.Text,edit8.Text); end; procedure TForm1.Button5Click(Sender: TObject); begin try _DLLMoudle := Loadlibrary('MyHRDLL.dll'); ShowMessage('初始化成功!!!'); except ShowMessage('初始化失败!!!'); Exit; end; if _DLLMoudle > 32 then begin Try @_GetPassWord:=GetProcAddress(_DLLMoudle,'GetPassWord'); //打开串口 @_CheckPassWord:=GetProcAddress(_DLLMoudle,'CheckPassWord'); //关闭串口 @_StrEncrypt:=GetProcAddress(_DLLMoudle,'StrEncrypt'); //对卡号冲值 @_StrDecrypt:=GetProcAddress(_DLLMoudle,'StrDecrypt'); //删除卡号 @_GetRegistCode:=GetProcAddress(_DLLMoudle,'GetRegistCode'); //终端机清除刷卡记录 @_CheckRegistCode:=GetProcAddress(_DLLMoudle,'CheckRegistCode'); // ShowMessage('DLL装载成功.............'); Except ShowMessage('出错!!DLL打开失败!!不能做其他操作!!'); End end; end; procedure TForm1.Button6Click(Sender: TObject); begin edit9.Text := FormatDateTime('MMDDHHNN',Now); end; end.