在线等待:一个带返回值的函数,请高手指正!

skile 2003-08-30 11:23:13
unit Unit1;

interface
function ReturnSz(var TmpKh : longint):Integer; export;
implementation
function ReturnSz(var TmpKh : longint):Integer;
begin
tmpkh:=12345; //向返回变量赋值是出错。
result :=0;
end;
end.

...全文
91 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
FrameSniper 2003-08-30
  • 打赏
  • 举报
回复
你加上stdcall约定试试!应该和这个没有关系!
FrameSniper 2003-08-30
  • 打赏
  • 举报
回复
创建一个DLL库,库文件和单元文件代码如下:

------------------------------库文件--------------------------------------
library Project1;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,
Classes,
Unit11 in 'Unit11.pas';

{$R *.res}

exports
ReturnSZ;

begin
end.
-----------------------------DLL单元文件--------------------------------
unit Unit1;

interface

function ReturnSZ(var TmpKh : LongInt):Integer; Export;

implementation

function ReturnSZ(var TmpKh : LongInt):Integer;
begin
Tmpkh:=12345;
Result :=0;
end;

end.
------------------------------------------------------------------------

然后Ctrl+F9编译生成DLL文件,然后写调用程序。创建一个Application,然后在主窗体上放一个Button1,进入OnCreate事件,全部代码如下:

------------------------------------------------------------------------
unit Unit2;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.DFM}

function ReturnSz(var X:LongInt):Integer;External 'Project1.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
Y:LongInt ;
begin
Y:=10;
ReturnSZ(Y);
ShowMessage(IntToStr(Y));
end;

end.

------------------------------------------------------------------------

编译、运行,没有任何错误!!!!!!!!!!!!!!!!!
skile 2003-08-30
  • 打赏
  • 举报
回复
unit Unit2;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.DFM}
function ReturnSz(var x:longint):integer;external 'project1.dll';
procedure TForm1.Button1Click(Sender: TObject);
var y:longint ;
begin
y:=10;
returnsz(y);
showmessage(inttostr(y));
end;

end.

这是调用上面DLL的代码!
zjqyb 2003-08-30
  • 打赏
  • 举报
回复
应该不会有错,可能是其他地方出错!!!
贴出来
zwlippi 2003-08-30
  • 打赏
  • 举报
回复
啊,来晚了,问题解决了就行!
Drate 2003-08-30
  • 打赏
  • 举报
回复
好了呀,那就好了
zjqyb 2003-08-30
  • 打赏
  • 举报
回复
stdcall
ZyxIp 2003-08-30
  • 打赏
  • 举报
回复
嘻..蹭点分.

skile 2003-08-30
  • 打赏
  • 举报
回复
现在又好了,^_^。过会了解帖!

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧