在dll中能使用函数的重载吗?为什么调用时会出错呢?(50分)

  • 主题发起人 主题发起人 mas_chen
  • 开始时间 开始时间
M

mas_chen

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟在做一个DLL文件,发现运行时有问题,把内容贴出如下,请高手不吝赐教 ,谢过!
dll文件:
library winop;
uses
SysUtils,
Classes;
function copyfiles(sourcedir,sourcefile1,destdir,destfile1:string):boolean;overload;stdcall;
var sflag,dflag:boolean;
begin
sflag:=false;
dflag:=false;
chdir(sourcedir);
if fileexists(sourcefile1) then sflag:=true;
chdir(destdir);
if fileexists(destfile1) then dflag:=true;
if sflag and dflag then result:=true
else result:=false;
end;
function copyfiles(sourcedir,sourcefile1,sourcefile2,destdir,destfile1,destfile2:string):boolean;overload;stdcall;
var sflag,dflag:boolean;
begin
sflag:=false;
dflag:=false;
chdir(sourcedir);
if fileexists(sourcefile1) and fileexists(sourcefile2) then sflag:=true;
chdir(destdir);
if fileexists(destfile1) and fileexists(destfile2) then dflag:=true;
if sflag and dflag then result:=true
else result:=false;
end;
function copyfiles(sourcedir,sourcefile1,sourcefile2,sourcefile3,destdir,destfile1,destfile2,destfile3:string):boolean;overload;stdcall;
var sflag,dflag:boolean;
begin
sflag:=false;
dflag:=false;
chdir(sourcedir);
if fileexists(sourcefile1) and fileexists(sourcefile2) and fileexists(sourcefile3) then sflag:=true;
chdir(destdir);
if fileexists(destfile1) and fileexists(destfile2) and fileexists(destfile3) then dflag:=true;
if sflag and dflag then result:=true
else result:=false;
end;
exports
copyfiles(sourcedir,sourcefile1,destdir,destfile1:string),
copyfiles(sourcedir,sourcefile1,sourcefile2,destdir,destfile1,destfile2:string),
copyfiles(sourcedir,sourcefile1,sourcefile2,sourcefile3,destdir,destfile1,destfile2,destfile3:string);
begin
end.
调用时使用静态调用,可结果却是错误的 怎么回事?
 
不行呀 换了类型没有用
 
是搞错了开始 于是查了帮助 在帮助上这样写到
When you export an overloaded function or procedure from a dynamically loadable library, you must specify its parameter list in the exports clause. For example,

exports
Divide(X, Y: Integer) name 'Divide_Ints',
Divide(X, Y: Real) name 'Divide_Reals';

On Windows, do not include index specifiers in entries for overloaded routines.
楼主现在应该知道了吧
 
我难道必须给重载的函数重命名? 那我重载还有意思吗
 
由帮助 恐怕你的这么改


//dll部分

library Project1;

uses
SysUtils,
Classes,
Dialogs;

{$R *.res}

procedure f(a: integer); overload; stdcall;
begin
Showmessage(inttostr(a));
end;

procedure f(b: Shortstring); overload; stdcall;
begin
Showmessage(b);
end;


exports
f(a: integer) name 'f1',
f(b: Shortstring) name 'f2';
begin
end.


//调用部分

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

procedure f(a: integer); stdcall; external 'Project1.dll' name 'f1'; overload;
procedure f(b: Shortstring); stdcall; external 'Project1.dll' name 'f2'; overload

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
f('sfsdfa');
end;

end.


另外注意 你的参数中有String这样的类形 这个类型在dll做参数传递可能不安全
建议使用PChar Shortstring 或者是Sharemem单元
 
只能设个别名导出 然后使用name项再实现重载 也许只能这个样子了
 
哦 太感谢你了!
 
你要不提这个问题 我以前也注意不到 呵呵
 
交个朋友吧 以后多向你请教 qq?
 

Similar threads

I
回复
0
查看
595
import
I
I
回复
0
查看
319
import
I
I
回复
0
查看
553
import
I
后退
顶部