delphi调用C++dll,多个返回值处理
function wsf_getJyxx(soap_endpoint:string;username:string;password:string;zsbh:string;ret:pchar):Integer; stdcall; external 'pimsservice.dll'; //函数声明
delphi中函数如下:
var
soap_endpoint : string;
zsbh : string;
username : string;
password : string;
iReturn : Integer;
ret : Array[0..2000] of Char;
begin
soap_endpoint := 'http://1.0.0.225:8080/PIMSService/service/DataService/';
zsbh := 'DL09T00026';
username := 'admin';
password := 'admin';
iReturn := wsf_getJyxx(soap_endpoint,username,password,zsbh,@ret[0]);
showmessage(ret);
end;
为什么返回值为乱码,是不是数组内存不够?小弟初次接触delphi,望各位大虾指教
[解决办法]
情况有很多
c++里喜欢用指针,把你的string类型先改为pchar试试
[解决办法]
C++声明
const int __stdcall wsf_getJyxx(char *soap_endpoint, char* username, char* password, char* zsbh,char* ret);
function wsf_getJyxx(soap_endpoint, username,password, zsbh, ret:pchar ):integer; stdcall;
[解决办法]
感觉Lz的接收代码没错,但是传参用的string是delphi特有的,和其他语的string类型不同的,LZ确定c代码里收到的传入参数正确么?
5L的声明可都是pchar类型啊
[解决办法]
soap_endpoint值传的对不对?
从地址构造形式上看是使用java、xfire实现的WS,去掉soap_endpoint地址最后一个/试试。
[解决办法]
function wsf_getJyxx(soap_endpoint,username,password,zsbh:pchar; var ret:pchar):Integer; stdcall; external 'pimsservice.dll'; //函数声明
delphi中函数如下:
var
soap_endpoint : string;
zsbh : string;
username : string;
password : string;
iReturn : Integer;
ret : Array[0..2000] of Char;
testret:pchar;
retstr:string;
begin
soap_endpoint := 'http://1.0.0.225:8080/PIMSService/service/DataService/';
zsbh := 'DL09T00026';
username := 'admin';
password := 'admin';
GetMem(testret,1024);
try
iReturn := wsf_getJyxx(pchar(soap_endpoint),pchar(username),pchar(password),pchar(zsbh),testret);
retstr:=testret;
finally
FreeMem(testret);
end;
showmessage(retstr);
end;
--------------------
试试按地址传递参数看看行不行。
[解决办法]
给字符数组分配下内存空间
GetMem(ret,2000);
[解决办法]
VC 用 char *
delphi 用 Pchar 对应
应该没问题,我用过。
[解决办法]
前面4个参数应该没问题吧,都是Pchar即可
后一个ret看你的调用,应该是返回的字符类型的数组指针
试试这样:
function wsf_getJyxx(soap_endpoint, username,password, zsbh:Pchar var ret:array of Pchar):integer;...
使用
var
i:integer;
P:array of PChar;
begin
setlength(P,20); //设置数组长度20,注意不能少于函数wsf_getJyxx体内的
for i:=low(P) to high(P) do //初始化; 初始化就已经分配内存,不须要另外分配
P[i]:='';
i:=wsf_getJyxx('A','A','A','A',P);//调用,A改成具体的值
showmessage(P[0]); //访问任一个看看P[0]到P[20]
setlength(P,0);
end;
另:如果知道数组长度,直接定义就好,不用动态
[解决办法]
zsbh:string;ret:pchar):
这个定义成成var ret:pchar