关于dll调用出错的问题?(50分)

  • 主题发起人 梦菲斯
  • 开始时间

梦菲斯

Unregistered / Unconfirmed
GUEST, unregistred user!
dll 中有一个函数,用于大小转换!
但是我在调用时,就会出这个错误“Invalid pointer operation”不知是何故!出错后接着运
行,可以看出dll中的函数已被调用成功!
 
把代码贴出一些来才能看出来呀.
 
library ConvertMoney;

{ 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;

{$R *.res}

function ResultMoney(const s:Real):String;
var
i:Integer;
r:Real;
ts,ss,tt,t:String;
begin
if abs(s)>0 then
begin
r:=int(abs(s)*1000)/1000;
ts:=format('%f',[r]);
if Length(ts)>15 then
begin
Result:='数字太大';
end
else
begin
t:='';
for i :=1 to Length(ts) do
begin
if ts<>'.' then
begin
case ts of
'0':ss:='零';
'1':ss:='壹';
'2':ss:='贰';
'3':ss:='叁';
'4':ss:='肆';
'5':ss:='伍';
'6':ss:='陆';
'7':ss:='柒';
'8':ss:='捌';
'9':ss:='玖';
end;
case Length(ts)-i+1 of
1: tt:='分整';
2: tt:='角';
3: tt:='';
4: tt:='元';
5: tt:='拾';
6: tt:='佰';
7: tt:='仟';
8: tt:='万';
9: tt:='拾';
10: tt:='佰';
11: tt:='仟';
12: tt:='亿';
13: tt:='拾';
14: tt:='佰';
15: tt:='仟';
end;
t:=t+ss+tt;
end;
end;
Result:=t;
end;
end
else
Result:='零';
end;

exports ResultMoney index 1

begin
end.
//下面是调用
unit Untest;

interface

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

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

var
Form1: TForm1;

implementation
function ResultMoney(const s:Real):String;external'ConvertMoney.dll';

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.Text :=ResultMoney(89.11);
end;

end.
 
在动态连接库之间传递参数最好不要用string,否则必须引用sharemem单元,而且在调用端和Dll
端都要引用,并且必须是第一个,比如uses Sharemem,windows.....,这里Sharemem必须是
第一个!!

如果不用string ,可以用pchar类型来代替.
 
另外调用方式最后用stdcall方式.
 
在编译DLL是会出一个警告!是什么意思!Symbol 'INDEX' is specific to a platform
 
呵呵,用
function ResultMoney(const s:Real):pchar;
不要用String类型,否则肯定出错
 
我已经换成PCHAR了!
 
去掉index 1
 
Invalid pointer operation
这个错误不关DLL的事,后来我将这个函数放到单独的程序中,运行[完成退出时,同样
会出这个提示,请哪个大哥帮忙看一下!谢谢!
 
谢谢诸位,问题已解决!
 
顶部