大家帮看看!
//dll
library Lianxi;
uses
SysUtils,
Classes;
function max(x: Integer): Integer; export;
begin
Result := X;
end;function min(x, y: Integer): Integer; export;
begin
if X < Y then Min := X else Min := Y;
end;
exports
Min index 1,
Max index 2;
{$R *.res}begin
end.
//exe
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Max = function(var X: Integer): Integer;stdcall;
var
Form1: TForm1;
aptr: TFarproc;
lhnd: THandle;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
lhnd := LoadLibrary('Lianxi.dll');
aptr := GetprocAddress(lhnd, 'Max');
edit3.Text := inttostr(Max(50));///到这就出错.为什么.?
FreeLibrary(lhnd);
end;end.
//dll
library Lianxi;
uses
SysUtils,
Classes;
function max(x: Integer): Integer; export;
begin
Result := X;
end;function min(x, y: Integer): Integer; export;
begin
if X < Y then Min := X else Min := Y;
end;
exports
Min index 1,
Max index 2;
{$R *.res}begin
end.
//exe
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Max = function(var X: Integer): Integer;stdcall;
var
Form1: TForm1;
aptr: TFarproc;
lhnd: THandle;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
lhnd := LoadLibrary('Lianxi.dll');
aptr := GetprocAddress(lhnd, 'Max');
edit3.Text := inttostr(Max(50));///到这就出错.为什么.?
FreeLibrary(lhnd);
end;end.
procedure TForm1.Button1Click(Sender: TObject);
var
sMax: Max;
begin
lhnd := LoadLibrary('Lianxi.dll');
if lhnd > 0 then
begin
try
aptr := GetprocAddress(lhnd, 'Max');
if aptr <> nil then
begin
sMax := Max(aptr);
edit3.Text := inttostr(sMax(50));///到这就出错.为什么.?
end;
finally
FreeLibrary(lhnd);
end;
end;
end;
DLL里面函数的声明要用到stdcall,例如:
function max(x: Integer): Integer; export;
应改为
function max(x: Integer): Integer; stdcall;
并且
exports
Min index 1,
Max index 2;
应该为
exports
Min,Max;
index 关键字只是为了先后兼容,该指令已无用处,并且可能致使其它开发工具发生问题。
[Fatal Error] Project1.dpr(7): Could not compile used unit 'Unit1.pas'
library Lianxi;uses
SysUtils,
Classes;function Max(x: Integer): Integer; stdcall;
begin
Result := X;
end;function Min(x, y: Integer): Integer; stdcall;
begin
if X < Y then Min := X else Min := Y;
end;exports
Min,Max;
{$R *.res}begin
end.
//主程序代码
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
Max = function(x: Integer): Integer; stdcall; TForm1 = class(TForm)
Button1: TButton;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
aptr: TFarproc;
lhnd: THandle;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
sMax: Max;
begin
lhnd := LoadLibrary('Project2.dll');
if lhnd > 0 then
begin
try
aptr := GetprocAddress(lhnd, 'Max');
if aptr <> nil then
begin
sMax := Max(aptr);
edit3.Text := inttostr(sMax(50));///到这就出错.为什么.?
end;
finally
FreeLibrary(lhnd);
end;
end;
end;end.