delphi编写的dll,pb调用返回乱码

hanker1314520 2011-12-16 09:59:24
以下是我delphi编写的dll,用Pchar输出
library testDll;
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ToolWin, StdCtrls,ComObj;


{$R *.res}
function getComput(inputStr:pchar; out outStr:pchar):integer;stdcall;export;
begin
try
outstr := AllocMem(255);
strcopy(outstr,'woshizhong');
result := 0;
showmessage(outstr) ;
finally
FreeMem(outstr);
end;
end;
exports
getComput;
begin

end.
pb声明:
function integer getComput(string inputStr, ref String outStr) LIBRARY "testDll.dll"
pb调用:
string ls_outstr
integer li_net
ls_outstr = space(256)
li_net = getComput('oooo',ls_outstr)
messagebox(string(li_net), ls_outstr)

出现乱码,如delphi用shortstring输出,则不会,但是会偏移位
...全文
471 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Magni2012 2014-04-12
  • 打赏
  • 举报
回复
感谢xhz2000,解决了乱码的问题
都先生 2011-12-20
  • 打赏
  • 举报
回复
肯定很数据类型有关。
语言之间互相调用的时候,数据类型是最容易出问题的。
需要看好这些问题。
缘中人 2011-12-20
  • 打赏
  • 举报
回复
强烈建议用kaikai_kk的方法,pchar就可以了。
inputStr:pchar; outStr:pchar
xhz2000 2011-12-20
  • 打赏
  • 举报
回复
这是因为shortstring 类型第一个字节存放的是该字符的长度,应该会偏移一位;=》【偏移一字节】
xhz2000 2011-12-20
  • 打赏
  • 举报
回复
以下是我delphi编写的dll,用Pchar输出
library testDll;
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ToolWin, StdCtrls,ComObj;


{$R *.res}
function getComput(inputStr:pchar; out outStr:pchar):integer;stdcall;export;
begin
try
outstr := AllocMem(255);
strcopy(outstr,'woshizhong');
result := 0;
showmessage(outstr) ;
finally
FreeMem(outstr);
end;
end;
exports
getComput;
begin

end.
pb声明:
function integer getComput(string inputStr, ref String outStr) LIBRARY "testDll.dll"
pb调用:
string ls_outstr
integer li_net
ls_outstr = space(256)
li_net = getComput('oooo',ls_outstr)
messagebox(string(li_net), ls_outstr)

出现乱码,如delphi用shortstring输出,则不会,但是会偏移位

第一delphi的定义应该是:

function getComput(inputStr:pchar; outStr:pchar):integer;stdcall;export;
var
vString: String;
begin
try
vString := 'woshizhong';
move(PChar(vString)^, outstr^, length(vString));
result := 0;
except
result := -1;
end;
exports
getComput;
begin

end.

第二、PB调用就是按你写的就可以了

第三、如delphi用shortstring输出,则不会,但是会偏移位

这是因为shortstring 类型第一个字节存放的是该字符的长度,应该会偏移一位;






yslwl 2011-12-19
  • 打赏
  • 举报
回复
曾加ShareMem看看
kaikai_kk 2011-12-19
  • 打赏
  • 举报
回复
改为调用时分配内存,不是在dll里面分配,修饰词Out不要;

function getComput(inputStr:pchar; outStr:pchar):integer;stdcall;export;
begin
strcopy(outstr,inputStr);
result := 100;
end;

delphi调用,PB的自己改一下:
var
k:integer;
P:pchar;
begin
p:=stralloc(1024);
try
k:=getComput('测试',P);
showmessage(Inttostr(k)+','+P);//返回'100,测试'
finally
strdispose(p);
end;
end;
funxu 2011-12-19
  • 打赏
  • 举报
回复
另外LZdll的返回值的分配和释放在调用函数处理,你这样写会有问题
funxu 2011-12-19
  • 打赏
  • 举报
回复
没用过pb的路过,不过乱码通常是几个问题造成的
1 stdcall和cdecl
2 ansi和unicode
3 delphi中使用了string字符串

从你的代码看 把这句换成move例如
sKey := 'woshizhong';
iKey := length(sKey) + 1;
GetMem(outStr, iKey);
Move(sKey[1], outStr^, iKey);
山东蓝鸟贵薪 2011-12-19
  • 打赏
  • 举报
回复
我以前与遇到这样的问题,
应该就是你的DLL中的接口没有做好呀
你可以贴出来,让我们大家帮你看看呀
hanker1314520 2011-12-19
  • 打赏
  • 举报
回复
sKey := 'woshizhong';
iKey := length(sKey) + 1;
GetMem(outStr, iKey);
Move(sKey[1], outStr^, iKey);

按照楼上的写法还是出现乱码,不知道怎么做好呢?真是好想没有办法?
hanker1314520 2011-12-18
  • 打赏
  • 举报
回复
怎么没有人回答呢?现在没有人做delphi了吗

5,388

社区成员

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

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