显式调用dll的问题,编译运行成功,在freelibrary时出错 ( 积分: 50 )

  • 主题发起人 主题发起人 稍微有点帅
  • 开始时间 开始时间

稍微有点帅

Unregistered / Unconfirmed
GUEST, unregistred user!
1:dll文件:
library Project1;
uses
SysUtils,
Dialogs,
Classes;
{$R *.res}
procedure showmymessage(AHandle:THandle);stdcall;
begin
showmessage('ok');
end;

function compare(AHandle:THandle;input:integer):string;stdcall;
var display:string;
begin
if input>10 then display:='>10' else
display:='<10';
result:=display;
end;

exports
showmymessage,compare;
begin
end.
2:调用dll的单元文件:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TShowyaomessage = procedure (AHandle:THandle);stdcall;
Tmycompare = function (AHandle:THandle;input:integer):string;stdcall;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
dllhandle:THandle;
showyaomessage:TShowyaomessage;
begin
dllhandle:=loadlibrary('project1.dll');
try
if dllhandle=0 then exit;
@showyaomessage:=getprocaddress(dllhandle,'showmymessage');
if assigned(@showyaomessage) then showyaomessage(application.Handle);
finally
freelibrary(dllhandle);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
dllhandle:THandle;
mycompare:Tmycompare;
begin
dllhandle:=loadlibrary('project1.dll');
try
if dllhandle=0 then exit;
@mycompare:=getprocaddress(dllhandle,'compare');
if assigned(@mycompare) then label1.Caption:=mycompare(application.Handle,strtoint(edit1.Text));
finally
freelibrary(dllhandle);
end;
end;
end.
现在的问题是,调用第一个过程没问题,调用第二个函数时,在freelibrary时出错,label1.caption的值也改变了,请问大家这是怎么回事!请各位不吝赐教,谢谢!
 
1:dll文件:
library Project1;
uses
SysUtils,
Dialogs,
Classes;
{$R *.res}
procedure showmymessage(AHandle:THandle);stdcall;
begin
showmessage('ok');
end;

function compare(AHandle:THandle;input:integer):string;stdcall;
var display:string;
begin
if input>10 then display:='>10' else
display:='<10';
result:=display;
end;

exports
showmymessage,compare;
begin
end.
2:调用dll的单元文件:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TShowyaomessage = procedure (AHandle:THandle);stdcall;
Tmycompare = function (AHandle:THandle;input:integer):string;stdcall;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
dllhandle:THandle;
showyaomessage:TShowyaomessage;
begin
dllhandle:=loadlibrary('project1.dll');
try
if dllhandle=0 then exit;
@showyaomessage:=getprocaddress(dllhandle,'showmymessage');
if assigned(@showyaomessage) then showyaomessage(application.Handle);
finally
freelibrary(dllhandle);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
dllhandle:THandle;
mycompare:Tmycompare;
begin
dllhandle:=loadlibrary('project1.dll');
try
if dllhandle=0 then exit;
@mycompare:=getprocaddress(dllhandle,'compare');
if assigned(@mycompare) then label1.Caption:=mycompare(application.Handle,strtoint(edit1.Text));
finally
freelibrary(dllhandle);
end;
end;
end.
现在的问题是,调用第一个过程没问题,调用第二个函数时,在freelibrary时出错,label1.caption的值也改变了,请问大家这是怎么回事!请各位不吝赐教,谢谢!
 
把String类型换成PCHAR类型应该就OK了。
在DELPHI写DLL的时候尽量不要使用String类型,这个类型会导致很多内存方面的问题。
 
后退
顶部