dll内容
求阶乘)
uses
SysUtils,
Classes;
{$R *.RES}
function nn(n:integer):integer;stdcall;
var i:integer;
begin
result:=1;
for i:=1 to n do result:=result*i;
end;
exports
nn index 1 name 'nx';
begin
end.
主程序调用:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
type
tintfunction=function(n:integer):integer;stdcall;
procedure TForm1.Button1Click(Sender: TObject);
var
hinst:thandle;
fpointer:tfarproc;
myfunct:tintfunction;
begin
hinst:=loadlibrary('2.dll');//生成dll的名称
if hinst>0 then
try
fpointer:=getprocaddress(hinst,'nx');
if fpointer<>nil then
begin
myfunct:=tintfunction(fpointer);
myfunct(strtoint(edit1.text));
edit2.text:=inttostr(myfunct(strtoint(edit1.text)));
showmessage(edit1.text+'的阶乘是:'+#13+edit2.text);
end
else
showmessage('dll not found');
finally
freelibrary(hinst);
end
else
showmessage('library not found');
end;
end.//以上程序只需加入需要的edit,button即可