DLL程序被调用的时候前面十次左右能正常显示,后面会产生几次乱码,以后就一直为空,请问大概是什么原因? ( 积分: 200 )

  • 主题发起人 主题发起人 coolkillerjmq
  • 开始时间 开始时间
C

coolkillerjmq

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在这个程序调用前面10次能正常显示,但后面会产生几次乱码,以后就一直是空,请问这个是什么原因,在线等待,问题解决立即放分
function Balance:PChar;stdcall;
var
sReadStr:String;
IdTCPClient:TIdTCPClient;
BalanceResult:String;
myIni:TIniFile;
sSoftRegCode,sSoftRegPwd:string;
sRemoteIP:string;
sPort:integer;
readstr:String;
begin
myini:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'NtxSmsSet.ini');
//从ini文件中读取值
sSoftRegCode:=DeCrypt(myini.ReadString('set','sSoftRegCode',''),3);
sSoftRegPwd:=DeCrypt(myini.ReadString('set','sSoftRegPwd',''),3);
sRemoteIP:=myini.ReadString('cnconfig','RemoteIP','218.16.124.163');
sPort:=myini.ReadInteger('cnconfig','Port',9099);
myini.Free;
IdTcpClient:=TIdTcpClient.Create(Application);
try
IdTcpClient.Host:=sRemoteIP;
IdTcpClient.Port:=sPort;
if IdTCPClient.Connected=False then IdTcpClient.Connect(6000);

IdTCPClient.WriteLn('[*FB*] '+ sSoftRegCode + ' ' + sSoftRegPwd)
//向Server发送查询余额信息

sReadStr:=IdTcpClient.ReadLn
//接收查到的余额

if Copy(sReadStr,1,6)='[FBSC]' then
begin
ReadStr:=Trim(Copy(sReadStr,8,12));
if sReadStr='' then
Balanceresult:='-22'
else
Balanceresult:=ReadStr;
end
else if Copy(sReadStr,1,6)='[RCNT]' then
Balanceresult:='-22'
else
Balanceresult:='-25';
except
on E : Exception do
begin
IdTCPClient.Disconnect;
result:='-26';
exit;
end;

end;
result:=PChar(BalanceResult);
IdTCPClient.Disconnect;
end;
 
我现在这个程序调用前面10次能正常显示,但后面会产生几次乱码,以后就一直是空,请问这个是什么原因,在线等待,问题解决立即放分
function Balance:PChar;stdcall;
var
sReadStr:String;
IdTCPClient:TIdTCPClient;
BalanceResult:String;
myIni:TIniFile;
sSoftRegCode,sSoftRegPwd:string;
sRemoteIP:string;
sPort:integer;
readstr:String;
begin
myini:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'NtxSmsSet.ini');
//从ini文件中读取值
sSoftRegCode:=DeCrypt(myini.ReadString('set','sSoftRegCode',''),3);
sSoftRegPwd:=DeCrypt(myini.ReadString('set','sSoftRegPwd',''),3);
sRemoteIP:=myini.ReadString('cnconfig','RemoteIP','218.16.124.163');
sPort:=myini.ReadInteger('cnconfig','Port',9099);
myini.Free;
IdTcpClient:=TIdTcpClient.Create(Application);
try
IdTcpClient.Host:=sRemoteIP;
IdTcpClient.Port:=sPort;
if IdTCPClient.Connected=False then IdTcpClient.Connect(6000);

IdTCPClient.WriteLn('[*FB*] '+ sSoftRegCode + ' ' + sSoftRegPwd)
//向Server发送查询余额信息

sReadStr:=IdTcpClient.ReadLn
//接收查到的余额

if Copy(sReadStr,1,6)='[FBSC]' then
begin
ReadStr:=Trim(Copy(sReadStr,8,12));
if sReadStr='' then
Balanceresult:='-22'
else
Balanceresult:=ReadStr;
end
else if Copy(sReadStr,1,6)='[RCNT]' then
Balanceresult:='-22'
else
Balanceresult:='-25';
except
on E : Exception do
begin
IdTCPClient.Disconnect;
result:='-26';
exit;
end;

end;
result:=PChar(BalanceResult);
IdTCPClient.Disconnect;
end;
 
这样写
function YourFun(): Boolean;stdcall;export;
 
我需要的返回值是PCHAR类型的,不是boolean类型啊
 
一样的,我的意思是你的后面缺少了;export
 
这个是问题的原因吗????
export是可以不加的啊
 
BalanceResult:String;放到函数外面定义成全局变量[:D]
 
多谢lichengbin,不过出现这个问题的原因是什么能给我解释下吗??
 
因为Result的关系问题,
Result就表示了函数的结束,所以Pchar(BalanceResult),就可能不执行了,
另外BalanceResult是局部变量,函数结束就释放了内存,所以会出错。
查看Delphi的帮助如下:
Returning a PChar local variable
--------------------
A common error when working with PChars is to store a local variable in a data structure, or return it as a value. When your routine ends, the PChar disappears because it is a pointer to memory, and not a reference counted copy of the string. For example:

function title(n: Integer): PChar;
var
s: string;
begin
s := Format('title - %d', [n]);
Result := PChar(s)
// DON'T DO THIS
end;

This example returns a pointer to string data that is freed when the title function returns.
---------------------
 
多人接受答案了。
 
后退
顶部