dll中的函数调用没有问题,释放dll也没有问题,但是在关闭应用程序后,就弹出错误。
错误信息:Invalid pointer operation,应该是无效指针地址,但是我却一直没有发现错误.谢谢大家了!...
代码如下:
/*****************dll code********************/
library DemoDll;uses
  ShareMem,
  SysUtils,
  Forms,
  Windows,
  Controls,
  ActnList,
  Dialogs,
  Classes;{$R *.res}    function Add( a : Integer; b : Integer ) : Integer; stdcall;
    begin
        Result := a + b;
    end;  exports   
    Add; 
  begin   
  end. /****************** app code*****************/
unit DemoFomr;interfaceuses
  ShareMem,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form2: TForm2;implementation{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
    dllHandle : THandle;
    InvokeFunc : function( a : Integer; b : Integer ) : Integer; stdcall;
    a : Integer;
begin
    dllHandle := LoadLibrary( 'E:\TestProject\DemoDelphi\BDEAM\DemoDll\DemoDll.dll' );
    if dllHandle = 0 then
    begin
        ShowMessage( '调用类库失败.' );
        exit;
    end;
    @InvokeFunc := GetProcAddress( dllHandle,PChar( 'Add' ) );
    if @InvokeFunc = nil then
    begin
        ShowMessage( '调用函数失败.' );
        exit;
    end;
    a := InvokeFunc( 5,5 );
    ShowMessage( IntToStr( a ) );
    @InvokeFunc := nil;
    FreeLibrary( dllHandle );
end;end.

解决方案 »

  1.   

    @InvokeFunc   :=   GetProcAddress(   dllHandle,PChar(   "Add "   )   ); 
    这一句会不会有问题??InvokeFunc   :   function(   a   :   Integer;   b   :   Integer   )   :   Integer;   stdcall; 
            a   :   Integer; 
    变量会不会有冲突???
      

  2.   

    看不出有什么问题,调试一下也是正常,不过欠缺考虑一处
      if   @InvokeFunc   =   nil   then
      begin
        ShowMessage(   '调用函数失败. '   );
        FreeLibrary(dllHandle);        //需要卸载DLL
        exit;
      end
    end;
      

  3.   

            dllHandle   :=   LoadLibrary(   "E:\TestProject\DemoDelphi\BDEAM\DemoDll\DemoDll.dll "   ); 
        try
              if   dllHandle   =   0   then 
              begin 
                      ShowMessage(   "调用类库失败. "   ); 
                      exit; 
              end; 
              @InvokeFunc   :=   GetProcAddress(   dllHandle,PChar(   "Add "   )   ); 
              if   @InvokeFunc   =   nil   then 
              begin 
                      ShowMessage(   "调用函数失败. "   ); 
                      exit; 
              end; 
              a   :=   InvokeFunc(   5,5   ); 
              ShowMessage(   IntToStr(   a   )   ); 
              @InvokeFunc   :=   nil; 
            finally
              FreeLibrary(   dllHandle   ); 
            end;