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 xe10 android 界面设计-个人心得

Delphi xe 如何实现("再按一次退出") 然后退出程序

Delphi用命令行加载驱动

Delphi取代sleep的延时代码

Delphi 键盘记录源代码

Delphi png异形窗口

Delphi base64单元encddecd的修改

Delphi 正则表达式起步

Delphi中组件label、edit、tag、memo、richedit

Delphi实现文件的拖放功能

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



打赏

取消

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

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

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

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

评论

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