可以啊,你新建一个DLL Wizard,然后把函数写在里面,注意要声明调用的函数名称,要不别人没办法使用! 简单的求和实例:{Dll部分} library addnum;{ 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;{$R *.res}//在dll中定义求和函数 function addnums(num1,num2:integer):integer;stdcall; begin result:=num1+num2; //返回两个整数的和 end; exports //引出求和函数 addnums name 'addm'; //求和函数的别名
begin end.
{程序调用} unit main;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Button1: TButton; Label1: TLabel; Label2: TLabel; Label3: TLabel; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation //主程序在调用该DLL时,首先声明要调用的函数 function addnums(num1,num2:integer):integer;stdcall;external 'addnum.dll' name 'addm'{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); var zs1,zs2,sum:integer; begin zs1:=strtoint(edit1.Text); zs2:=strtoint(edit2.Text); sum:=addnums(zs1,zs2); //调用求和函数计算结果,无法引用别名'addm' edit3.Text:=inttostr(sum); end;end.
简单的求和实例:{Dll部分}
library addnum;{ 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;{$R *.res}//在dll中定义求和函数
function addnums(num1,num2:integer):integer;stdcall;
begin
result:=num1+num2; //返回两个整数的和
end;
exports //引出求和函数
addnums name 'addm'; //求和函数的别名
begin
end.
{程序调用}
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
//主程序在调用该DLL时,首先声明要调用的函数
function addnums(num1,num2:integer):integer;stdcall;external 'addnum.dll' name 'addm'{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
zs1,zs2,sum:integer;
begin
zs1:=strtoint(edit1.Text);
zs2:=strtoint(edit2.Text);
sum:=addnums(zs1,zs2); //调用求和函数计算结果,无法引用别名'addm'
edit3.Text:=inttostr(sum);
end;end.
因为有个没有实现的b函数,所以我不知道如何做成dll
客户调用a函数的时候把客户自己写的b函数当成参数传递给a函数就行了。
type
TfuncB = function(X: Integer): Integer; stdcall;function A(n1, n2, n3: Integer; FuncB: TfuncB): Integer; stdcall;
begin
...
blah := FuncB(...);
...
end;在dll中导出,把文档写清楚了,使用者自然就知道该怎么用了