特别奇怪的Dll释放问题,FreeLibrary就会出错.......
dll单元
uses
SysUtils,
Classes;
ndll in 'ndll.pas';
{$R *.res}
exports
x;
begin
end.
unit ndll;
interface
uses
SysUtils, Classes, Dialogs;
function x(s:string):string;stdcall;
implementation
function x(s:string):string;
begin
Result:='ok';
end;
end.
调用单元
unit untmain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TX = function (s:string):string;stdcall;
TForm1 = class(TForm)
Button3: TButton;
procedure Button3Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button3Click(Sender: TObject);
var
xx:TX; Lib: THandle;
begin
Lib := SafeLoadLibrary('dll.dll');
try
if Lib <> 0 then
@xx := GetProcAddress(Lib, 'x');
if not (@xx = nil) then
if xx('a') = 'ok' then
showmessage('ok');
finally
FreeLibrary(Lib); // 为什么为释放不掉,一释放就会出现地址访问错误
end;
end;
end.