delphi dll php调用,delphi之调用外部dll中的函数

你的位置: 鸟儿网络 > 菜鸟编程 > delphi之调用外部dll中的函数 分早绑定和晚绑定两种。

早绑定的代码如下:

[code=delphi]

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

//MB 函数的声明:

function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall;

implementation

{$R *.dfm}

{调用外部 DLL 中的函数,譬如调用系统 user32.dll 中的 MessageBoxA}

//function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer;

// stdcall; external user32 name 'MessageBoxA';

{其中 user32 是 Delphi 定义的常量 'user32.dll',可以直接写成:}

//function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer;

// stdcall; external 'user32.dll' name 'MessageBoxA';

{name 后面说明函数的真实名字}

{external 子句说明单元载入时就加载函数,也就是早绑定;如果晚绑定需要用 LoadLibrary}

{stdcall 指令表示参数传递是从右到左(Pascal则反之),不通过CPU寄存器传递}

{4个参数的数据类型可以使用对应的 Delphi 数据类型,譬如:}

//function MB(hWnd: LongWord; lpText, lpCaption: PChar; uType: LongWord): Integer;

// stdcall; external 'user32.dll' name 'MessageBoxA';

{或者是:}

//function MB(hWnd: Cardinal; lpText, lpCaption: PChar; uType: Cardinal): Integer;

// stdcall; external 'user32.dll' name 'MessageBoxA';

{如果函数在此单元声明后,需要给其他单元调用,需要先在 interface 区声明:}

//function MB(hWnd: Cardinal; lpText, lpCaption: PChar; uType: Cardinal): Integer;

// stdcall;

{本例已经这样做了,如果要测试其他几种情况,需要先注释掉它}

{然后在 implementation 区,说明函数的来源:}

function MB; external 'user32.dll' name 'MessageBoxA';

//调用测试:

procedure TForm1.Button1Click(Sender: TObject);

var

t,b: PChar;

begin

t := '标题';

b := '内容';

MB(0,b,t,0);

end;

end.[/code]

晚绑定的例子:

[code=delphi]

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

//晚绑定,也就是动态调用外部函数主要用以下三个命令:

//LoadLibrary:获取 DLL

//GetProcAddress:获取函数

//FreeLibrary:释放

//定义一个过程类型,参数要和需要的函数一致

TMB = function(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall;

TForm1 = class(TForm)

Button1: TButton;

procedure Button1Click(Sender: TObject);

procedure FormCreate(Sender: TObject);

procedure FormDestroy(Sender: TObject);

private

MB: TMB; {声明函数 MB}

inst: LongWord; {声明一个变量来记录要使用的 DLL 句柄}

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

begin

inst := LoadLibrary('user32.dll');

if inst <> 0 then

MB := GetProcAddress(inst, 'MessageBoxW');

// MB := GetProcAddress(inst, 'MessageBoxA'); {Delphi 2009 之前的版本用这句}

end;

//调用测试:

procedure TForm1.Button1Click(Sender: TObject);

var

t,b: PChar;

begin

t := '标题';

b := '内容';

MB(0, b, t, 0);

end;

procedure TForm1.FormDestroy(Sender: TObject);

begin

FreeLibrary(inst); {记得释放}

end;

end.[/code]

创建并调用dll

[code=delphi]

//通过 DLL Wizard 建立:

library TestDLL;

uses

SysUtils,

Classes,

Dialogs;

{$R *.res}

//建立过程

procedure Test;

begin

ShowMessage('TestDLL.Test');

end;

//输出

exports

Test;

begin

end.

//在其他工程调用,如果不在一个工程组,需要在相同目录下、System32下或指定路径;

//声明可以在实现区或接口区,这里的函数名要一致,甚至大小写。

//调用测试:

procedure Test; external 'TestDLL.dll';

procedure TForm1.Button1Click(Sender: TObject);

begin

Test;

end;[/code] 继续浏览有关 DELPHI 的文章

分享到

提交评论

Hi,您需要填写昵称和邮箱! 昵称昵称 (必填)

邮箱邮箱 (必填)

网址网址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值