delphi静态和动态调用dll的实例  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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


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

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.
推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.045607089996338 seconds