大家帮看看!
//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.

解决方案 »

  1.   

    //你调用DLL函数的方法错误
    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;
      

  2.   

    dll中的函数定义加上stdcall修饰。
      

  3.   

    楼上说的对,我刚才没有仔细看楼主的DLL代码。
    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 关键字只是为了先后兼容,该指令已无用处,并且可能致使其它开发工具发生问题。
      

  4.   

    edit3.Text := inttostr(sMax(50));///到这就出错.为什么.?还是到这出错[Error] Unit1.pas(44): Types of actual and formal var parameters must be identical
    [Fatal Error] Project1.dpr(7): Could not compile used unit 'Unit1.pas'
      

  5.   

    在我这里测试没有你说的情况,还有DLL里面的接口函数的书写是区别大小写的,Max不能写成max,我把我这边的代码贴出来,你看看吧。//DLL代码
    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.