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.

相关阅读 >>

Delphi 中压缩流和解压流的应用

Delphi xe android]获取屏幕的物理分辨率

Delphi idhttp登录网站教程代码

embarcadero开源项目赞助

Delphi建立快捷方式的函数:createshortcut()

如何将string变量赋值给pchar变量

Delphi idhttpserver的使用方法

Delphi webbrowser 查找对话框

Delphi 利用Delphi监视注册表的变化

Delphi 10 seattle的android应用程序中使用参数启动服务

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...