这个是DLL的代码:
library demo;
uses
SysUtils,
Classes;
{$R *.res}
function zhang(Z:integer):integer;stdcall;
begin
result:=5;
end;
function qi(Q:integer):integer;Stdcall;
begin
result:=2;
end;
exports
zhang,
qi;
begin
end.
这个是动态调用上面的DLL
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
PVFN = Function (para : Integer) : Integer;stdcall; //如果不加stdcall 则在释放时出现地址访问错误
TMyfunc =function(Z:integer):integer;stdcall;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ResultInt:integer;
MyFunc: TMyFunc;
Moudle: THandle;
begin
Moudle := Loadlibrary('c:/Demo.dll');
try
if moudle>0 then begin
@MyFunc := GetProcAddress(Moudle,'zhang');
if not(@MyFunc = nil) then
ResultInt:=MyFunc(3)
else
showmessage('error');
showmessage(intTostr(Resultint));
end
else
showmessage('error');
finally
FreeLibrary(Moudle);
end;
// closehandle(moudle);
end;
end.
这个是静态调用上面的DLL
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function zhang(Z:integer):integer;far external 'demo.dll';
function qi(Q:integer):integer;far external 'demo.dll';
procedure TForm1.Button1Click(Sender: TObject);
var
zhangqi:integer;
begin
zhangqi:=zhang(2);
showmessage(inttostr(zhangqi));
end;
procedure TForm1.Button2Click(Sender: TObject);
var
zhangqi:integer;
begin
zhangqi:=qi(3);
showmessage(inttostr(zhangqi));
end;
end.