如何在 Delphi 中编写和调用 DLL

如何在 Delphi 中编写和调用 DLL

❮ 上一节

下一节 ❯

如何在 Delphi 中编写和调用 DLL

以下代码创建一个包含两个函数(Min 和 Max)的 DLL,目的是返回两个整数中较大的一个。

在 Delphi 中启动一个新的 DLL 项目(单击文件 −> 新建,选择 DLL)。

将项目另存为 delhpdll。

按如下所示填写库中的代码。

// Uffe wrote: This is a toy dll to demonstrate the

// use of dll's

//

// The libary export two functions Max and Min

//

// The dll matches the MainProject which is a Delphi

// project which calls the DLL.

//{

// DELPHI WROTE THIS:

// Important note about DLL memory management: ShareMem

// must be the first unit in your library's USES clause

// AND your project's (select Project-View Source) USES

// clause if your DLL exports any procedures or functions

// that pass strings as parameters or function results.

// This applies to all strings passed to and from your

// DLL--even those that are nested in records and classes.

// ShareMem is the interface unit to the BORLNDMM.DLL

// shared memory manager, which must be deployed along

// with your DLL. To avoid using BORLNDMM.DLL, pass

// string information using PChar or ShortString

// parameters.

//}

uses

SysUtils,

Classes;

// Declare stdcall to make interface to other languages

function Min(X, Y: Integer): Integer; stdcall;

begin

if X < Y then Min := X else Min := Y;

end;

function Max(X, Y: Integer): Integer; stdcall;

begin

if X > Y then Max := X else Max := Y;

end;

exports // Make available to calling applications

Min index 1,

Max index 2;

begin

end.

构建并保存 DLL 项目。

(如果您是命令行使用者,那么您只需从命令行执行"dcc32 delhpdll.dpr"即可...这将为您提供相同的 DLL,但没有 IDE 内容...)。

然后您需要一个应用程序来调用 DLL:

启动一个新的"主"应用程序项目。

制作测试 DLL 所需的任何 GUI 控件。

填写如下所示的用于连接 DLL 的源代码。

//Main Applet to demonstrate how to call a dll.

//

// SEARCH PATHS:

// The code makes no special effort to search for the DLL.

// Easiest if everything (including DLL) is in the same

// directory.

//

//DLL CALLING:

// This applet demonstrates both Win API calling and

// external calling (see below)

//

//

// NOTICE: I wasted a lot of time not declaring the

// functions "stdcall" throughout. If you

// declare it "stdcall" in the DLL and not

// in the calling application, then you get hard-to-find

// errors - "external" will crash on you,

// the Win API does not type-check so the function

// just gives "weird" results. (OK this is

// obvious when you think about it .. problem is,

// I didn't)

// Jump to "implementation" section,

// no DLL specs before.

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Edit1: TEdit;

Edit2: TEdit;

Button1: TButton;

Label1: TLabel;

Label2: TLabel;

Button2: TButton;

Label3: TLabel;

Label4: TLabel;

Label5: TLabel;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

// TMaxFun becomes a function variable (think of

// a "pointer to function" without the pointer)

TMaxFun = function(i,j: integer):integer; stdcall;

var

Form1: TForm1;

implementation

{$R *.DFM}

// This declares Max as a function which should be found

// externally, in the dll

function Max(i,j:integer): integer; stdcall; external 'delhpdll.dll';

// This procedure uses the "external" call to the DLL

procedure TForm1.Button1Click(Sender: TObject);

var

i,j: integer;

begin

i := strtoint(Edit1.Text);

j := strtoint(Edit2.Text);

Label1.Caption := inttostr(Max(i,j));

// Easy, eh?

end;

// This calls the DLL directly through the Win API.

// More code, more control.

procedure TForm1.Button2Click(Sender: TObject);

var

i,j,k: integer;

Handle: THandle;

// mmax is a function variable; see type declaration

// of TMaxFun above.

mmax : TMaxFun;

begin

i := strtoint(Edit1.Text);

j := strtoint(Edit2.Text);

// Load the library

Handle := LoadLibrary('DELHPDLL.DLL');

// If succesful ...

if Handle <> 0 then

begin

// Assign function Max from the DLL to the

// function variable mmax

@mmax := GetProcAddress(Handle, 'Max');

// If successful

if @mmax <> nil then

begin

k := mmax(i,j);

Label1.Caption := inttostr(k);

end;

// Unload library

FreeLibrary(Handle);

end;

end;

end.

去吧,运行你的程序。

dll_examples.html

❮ 上一节

下一节 ❯

相关推荐

周二珂个人资料简介 真实年龄是几几年出生的
篑的成语
365365bet

篑的成语

📅 06-29 👁️ 9444
喜马拉雅
365比分官网

喜马拉雅

📅 07-03 👁️ 9433
哪里可以看浙江卫视的直播
日博365怎么样

哪里可以看浙江卫视的直播

📅 07-01 👁️ 1953
电信四兆宽带多少钱
365365bet

电信四兆宽带多少钱

📅 07-02 👁️ 3733
wordpress 淘宝模板怎么用
365比分官网

wordpress 淘宝模板怎么用

📅 06-30 👁️ 9650
游戏成瘾究竟是为啥?本文带你一探究竟
365比分官网

游戏成瘾究竟是为啥?本文带你一探究竟

📅 07-02 👁️ 1525
法国十大著名建筑:法国最具有代表性的十个建筑物
AI 纹身设计器
日博365怎么样

AI 纹身设计器

📅 07-01 👁️ 8835