RSS订阅 | 匿名投稿
您的位置:网站首页 > Dll > 正文

delphi静态和动态调用dll的实例

作者:admin 来源:未知 日期:2010/5/9 15:20:56 人气: 标签:

静态===================

unit main;

interface

uses
ShareMem, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
mmo1: TMemo;
mmo2: TMemo;
btn1: TButton;
btn2: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

Function EncrypKey(Src:String; Key:String):string;stdcall;external 'mydll.dll';
Function UncrypKey(Src:String; Key:String):string;stdcall;external 'mydll.dll';

implementation

{$R *.dfm}


procedure TForm1.btn1Click(Sender: TObject);
var
s,ke:string;
begin
if Boolean(Length(Trim(mmo1.Text))) then
begin
ke:='iloveyou';
s:= Trim(mmo1.Text);
mmo2.Clear;
mmo2.Text:= EncrypKey(s,ke);
end;
end;

procedure TForm1.btn2Click(Sender: TObject);
var
s,ke:string;
begin
if Boolean(Length(Trim(mmo2.Text))) then
begin
ke:='iloveyou';
s:= Trim(mmo2.Text);
mmo1.Clear;
mmo1.Text:= UncrypKey(s,ke);
end;
end;
end.

动态==================

unit main;

interface

uses
ShareMem, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
mmo1: TMemo;
mmo2: TMemo;
btn1: TButton;
btn2: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
type
TDllFunction = Function(Src:String; Key:String):string;stdcall;


procedure TForm1.btn1Click(Sender: TObject);
var
Hmydll: HWND;
mydllfun:TDllFunction;
s,ke:string;
begin
if Boolean(Length(Trim(mmo1.Text))) then
begin
ke:='iloveyou';
s:= Trim(mmo1.Text);
mmo2.Clear;
Hmydll := LoadLibrary('mydll.dll');
@mydllfun := GetProcAddress(Hmydll, 'EncrypKey');
if @mydllfun <> nil then
begin
try
try
mmo2.Text:= mydllfun(s,ke);
except
ShowMessage('error!!');
end;
finally
FreeLibrary(Hmydll);
end;
end;
end;
end;


procedure TForm1.btn2Click(Sender: TObject);
var
Hmydll: HWND;
mydllfun:TDllFunction;
s,ke:string;
begin
if Boolean(Length(Trim(mmo2.Text))) then
begin
ke:='iloveyou';
s:= Trim(mmo2.Text);
mmo1.Clear;
Hmydll := LoadLibrary('mydll.dll');
@mydllfun := GetProcAddress(Hmydll, 'UncrypKey');
if @mydllfun <> nil then
begin
try
try
mmo1.Text:= mydllfun(s,ke);
except
ShowMessage('error!!');
end;
finally
FreeLibrary(Hmydll);
end;
end;
end;
end;

end.
读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址: