delphi编写的dll,pb调用返回乱码 怎么没有人回答呢?现在没有人做delphi了吗 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我以前与遇到这样的问题,应该就是你的DLL中的接口没有做好呀你可以贴出来,让我们大家帮你看看呀 没用过pb的路过,不过乱码通常是几个问题造成的1 stdcall和cdecl 2 ansi和unicode3 delphi中使用了string字符串从你的代码看 把这句换成move例如 sKey := 'woshizhong'; iKey := length(sKey) + 1; GetMem(outStr, iKey); Move(sKey[1], outStr^, iKey); 另外LZdll的返回值的分配和释放在调用函数处理,你这样写会有问题 sKey := 'woshizhong';iKey := length(sKey) + 1;GetMem(outStr, iKey);Move(sKey[1], outStr^, iKey);按照楼上的写法还是出现乱码,不知道怎么做好呢?真是好想没有办法? 改为调用时分配内存,不是在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; 以下是我delphi编写的dll,用Pchar输出library testDll;usesWindows, 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;beginend.pb声明:function integer getComput(string inputStr, ref String outStr) LIBRARY "testDll.dll"pb调用:string ls_outstr integer li_netls_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;beginend.第二、PB调用就是按你写的就可以了第三、如delphi用shortstring输出,则不会,但是会偏移位这是因为shortstring 类型第一个字节存放的是该字符的长度,应该会偏移一位; 这是因为shortstring 类型第一个字节存放的是该字符的长度,应该会偏移一位;=》【偏移一字节】 强烈建议用kaikai_kk的方法,pchar就可以了。inputStr:pchar; outStr:pchar 感谢xhz2000,解决了乱码的问题 数据库太大,处理缓慢,如何解决? 如何获得ListView的第n行第m列的值? 求助!寻找自定义格式!!!!??? 请教Delphi 一个小问题!! 我的Form不能移动了,忘了设置那个参数了. 简单问题:怎么样可以在ADODataSet中获取任意记录? 在线等,马上揭贴!! 邮件的怪事 谁有远程通讯软件? 寻delphi 老师一名! 招标了:“车辆与运输管理系统”寻求异地兼职开发,请报价! 关于ocx插件重新安装的问题 请问小写怎么转换成大写呢?要可逆的
应该就是你的DLL中的接口没有做好呀
你可以贴出来,让我们大家帮你看看呀
1 stdcall和cdecl
2 ansi和unicode
3 delphi中使用了string字符串从你的代码看 把这句换成move例如
sKey := 'woshizhong';
iKey := length(sKey) + 1;
GetMem(outStr, iKey);
Move(sKey[1], outStr^, iKey);
iKey := length(sKey) + 1;
GetMem(outStr, iKey);
Move(sKey[1], outStr^, iKey);按照楼上的写法还是出现乱码,不知道怎么做好呢?真是好想没有办法?
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;
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;
beginend.
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;
beginend.第二、PB调用就是按你写的就可以了第三、如delphi用shortstring输出,则不会,但是会偏移位这是因为shortstring 类型第一个字节存放的是该字符的长度,应该会偏移一位;
inputStr:pchar; outStr:pchar