DLL调用出错!(50分)

  • 主题发起人 主题发起人 xiao_ping
  • 开始时间 开始时间
X

xiao_ping

Unregistered / Unconfirmed
GUEST, unregistred user!
我的这个DLL是用来做人民币大小写转换的。调用时能完成其功能,但关闭调用DLL的程序时出现
runtime error 217 at 00413a88.我原先以为是string 参数有问题,但后来换成pchar
后还是同样的错误??下面贴出源码...那位大侠帮忙看一下。

//Dll
library rmb;

{ 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
sharemem,SysUtils,
Classes,
uniport in 'uniport.pas';

{$R *.res}
const
t_num: array[0..9] of string[2] = ('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
t_unit1: array[0..4] of string[2] = ( '圆', '万', '亿','万', '亿');
t_unit2: array[0..2] of string[2] = ( '拾', '佰', '仟');
t_unit3: array[0..1] of string[2] = ( '角', '分');


function SmallToBig( szCurrency: string ):string;stdcall
var dotPos:integer;
szInt:string
// 小写的整数部分
szFlt:string
// 小写的小数部分
sInt:string
// 大写的整数部分
sFlt:string
// 大写的小数部分
old_inx_num :integer;
inx_num :integer;
inx_unit :integer;
i: integer;
bOnlyFlt:Boolean;
begin
// getmem(szCurrency,256);
dotPos := Pos('.', szCurrency);
if dotPos <> 0 then
begin
szInt := Copy(szCurrency, 0, dotPos - 1 )
//分开来转换!
szFlt := Copy(szCurrency, dotPos + 1, 2);
end
else
begin
szInt := szCurrency;
szFlt := '';
end;

// freemem(szCurrency);

if ( ( szInt = '0' ) and (( szFlt = '00' ) or ( szFlt = '')) ) then
begin
result := '人民币零圆整';
exit;
end;

if ( szInt = '0' ) then bOnlyFlt := true
// 如果整数部分为零


if ( (Length(szFlt) <> 2) and ( dotPos <> 0)) then
// 小数点后必须有两位或者根本没有
begin
result := '';
exit;
end;

if ( not bOnlyFlt ) then // 如果整数部分为零,则不对其进行处理
for i := 0 to Length(szInt) - 1 do
begin
inx_num := StrToInt( szInt[ Length(szInt) - i ] );
if i mod 4 = 0 then
begin
inx_unit := i div 4;
if ( inx_num > 0 ) then
sInt := t_num[ inx_num ] + t_unit1[ inx_unit ] + sInt
else
sInt := t_unit1[ inx_unit ] + sInt;
end
else
begin
inx_unit := i mod 4 - 1;
if inx_num > 0 then
sInt := t_num[ inx_num ] + t_unit2[ inx_unit ] + sInt
else
if old_inx_num > 0 then
sInt := t_num[ inx_num ] + sInt;
end;
old_inx_num := inx_num;
end;

//处理小数部分
if ( ( szFlt <> '' ) and ( szFlt <> '00' ) ) then
begin
for i := 0 to 1 do
begin
inx_num := StrToInt(szFlt[i + 1]);
if (inx_num > 0) then
sFlt := sFlt + t_num[inx_num] + t_unit3
else
if ( i <> 1) then // 如果"角"为0 ("分"是 0, 则忽略)
if ( not bOnlyFlt ) then // 如果整数部分此时不为零
sFlt := sFlt + t_num[inx_num];
end;
result := '人民币' + sInt + sFlt;
end
else
begin
result := '人民币' + sInt + '整';
end
end;
exports SmallToBig;
end.


//接口部分
unit uniport;

interface


{$IFNDEF RMB}
function SmallToBig (szCurrency:string):string
stdcall;
{$ENDIF}
implementation
{$IFNDEF RMB}
function SmallToBig
external 'rmb.dll' name 'SmallToBig';
{$ENDIF}

end.

//调用部分
unit test;

interface

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

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

var
Form1: TForm1;

implementation

uses uniport;
{$R *.dfm}

//function SmallToBig(szCurrency:string):string
stdcall external 'rmb.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption :=SmallToBig(edit1.Text);
end;

end.


 
最好使用pChar作为参数传递数值,而不是在Function后传递。
否则,在那里申请的空间,又在那里释放呢?
 
问题解决了,是的操作系统有问题,我在其他的机子上试时是没有问题!
D版终究是不太可靠!!
 
接受答案了.
 
后退
顶部