求助---动态链接库dll(100分)

  • 主题发起人 主题发起人 libo2000
  • 开始时间 开始时间
L

libo2000

Unregistered / Unconfirmed
GUEST, unregistred user!
搞不懂为什么不能成功!

//project1.dll 源代码----->>>>

library project1;

uses
// Question:为什么使用了ShareMem还是不能用String?

// 即使使用了sharemem,但在定义函数时,
// 函数的参数和返回值还是不能使用String,只能使用shortstring,
// 如果强行使用string,调用该.dll时候还是回出错误。
// 但是有没有sharemem也都可以编译成project1.dll。
ShareMem,
SysUtils,
Classes;

{$R *.res}
function cheng_max(x,y:double):double;
begin
if x>=y then
begin
result:=x;
end
else begin
result:=y;
end;
end;

function cheng_split(splitStr,s:shortstring;number:integer=1):shortstring;
var i,posnum,count:integer;s2:string;
begin
if number<1 then number:=1;
count:=length(splitStr);
s2:=copy(s,length(s),count);
posnum:=pos(splitstr,s2);
if posnum=0 then s:=s+splitStr;
for i:=1 to number do
begin
posnum:=pos(splitStr,s);
if posnum >1 then
begin
s2:=copy(s,1,posnum-1);
end
else begin
s2:='';
end;
delete(s,1,posnum+count-1);
end;
result:=s2;
end;

// 导出表
exports
cheng_max index 1,
cheng_split index 2;
begin
end.



//test.exe 源代码 ------->>>>>>>
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
function cheng_split(splitstr,s:shortstring;number:integer=1):shortstring;stdcall;external 'dll/project1.dll';
function cheng_max(x,y:double):double;stdcall;external 'dll/project1.dll';
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
function cheng_split2(splitStr,s:shortstring;number:integer=1):shortstring;
var i,posnum,count:integer;s2:string;
begin
// 该函数的定义完全等同于project1.dll中定义的cheng_split()函数
// ...
end;

begin

// Question:为什么调用dll函数cheng_split()错误?


// 调用project.dll中定义的函数cheng_split() 得出错误结果!:
label1.Caption :='错误结果'+cheng_split(edit3.Text ,edit1.Text ,strtoint(edit2.Text ));

// 使用button1click定义的函数cheng_split2() 得出正确结果!:
label2.Caption :='正确结果'+cheng_split2(edit3.Text ,edit1.Text ,strtoint(edit2.Text ));

// 调用project.dll中定义的函数cheng_max() 得出正确结果:
label3.Caption :='正确结果'+floattostr(cheng_max(1,3));

end;

end.
 
function cheng_max(x,y:double):double; stdcall;
你定义是时候没加stdcall;
这和声明的时候不一致,delphi默认的调用方式是cdecl,这样就导致出错,
我试了一下,可以用String类型的参数和返回值
 
应该是上面的问题。
 
thank zm30 ,jsxjd

再申明一下使用ShareMem就ok了!
program Project1;
uses
ShareMem,
 
后退
顶部