求教delphi dll 返回字符串

来源:百度知道 编辑:UC知道 时间:2024/05/15 15:36:17
dll函数代码如下:
function BaseEncode(ss:PChar;Res:PChar):Integer;stdcall;
begin
StrCopy(Res,ss);
StrCat(Res,'_00000');
Result := StrLen(Res);
end;
exports
BaseEncode;

调用代码如下:
function BaseEncode(ss:Pchar; Res:PChar):Integer;stdcall;
external '../ChangCode/changecode.dll';

procedure TForm1.Button2Click(Sender: TObject);
var
ss:PChar;
Res:PChar;
Num:Integer;
begin
ss := 'good!';
Res := StrAlloc(80);
Num := BaseEncode(PChar(ss), PChar(Res));
Edit1.Text := Res;
Edit2.Text := IntToStr(Num);
StrDispose(ss);
StrDispose(Res);
end;

提示错误:Invalid pointer operation.

但结果可以正确显示。
是这个问题
Edit1.Text := string(Res);
改了就可以。
小弟一直用JAVA,DELPHI刚开始用,谢谢啦。

Num := BaseEncode(PChar(ss), PChar(Res));是这一行出现问题吗?试着修改为:Num := BaseEncode(ss, Res);
如果是Edit1.Text := Res;这一行,则修改为 Edit1.Text := string(Res);

你说明白啊,哪个地方报错?
StrDispose, 这个你不能使用,
Description

StrDispose is provided for backward compatibility only. StrDispose disposes of a string on a heap that was previously allocated with StrAlloc or StrNew.

If Str is nil, StrDispose does nothing.需要和StrAlloc或StrNew一起使用。
去掉最后2行,就可以了。