请高手帮忙把Delphi代码改写成用C++Builder写的DLL文件

zjntche_cn 2005-07-28 03:17:18
我想把下面这两段Delphi6代码用C++Builder6改写,并做成DLL文件,供PowerBuilder6.5使用,请各位高手帮忙指点哦!

//这个是一个form里定义的调用socket的函数。
function Tsndform.SendPackage(const sPackageBody: string): string;
var
sk:TSocket;
begin
try
sk := TSocket.Create(nil);
result := sk.sendText(sPackageBody);
sk.Free;
except
on e:exception do
begin
result := '9999';
showmessage('发送报文');
end;
end;
end;

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
//这个是自定义的Socket,文件名为socket.pas
unit Socket;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls,ExtCtrls,Sockets,
Psock, IniFiles;
Type
TSocket = class(TClientSocket)
private
{ Private declarations }
waitTime:integer; //交易超时时间
protected
public
{ Public declarations }
constructor Create(AOwner: TComponent);override;
{发送字符串
Text:需要发送的字符串;
返回值:收到的字符串}
function sendText(Text:string):String;
end;
Type
TSerSocket = class(TServerSocket)
private
{ Private declarations }
protected
public
{ Public declarations }
constructor Create(AOwner: TComponent);override;
end;

{ 读取INI配置文件参数
根据参数从配置文件中取配置数据,配置文件定义:运行程序所在目录的SYSINI.INI文件。
根据返回值的不同分为两个函数,返回值分别为整型和字符串型。
参数:INI配置文件内容查找参数(ection, Ident, Default) }
function ReadIniFileI(const sect,flag: string; default: Integer): integer;
function ReadIniFileS(const sect,flag: string; default: string): string;
{ 写文本文件
将输入的字符串写入当前运行程序所在目录下的指定文件中,如果文件不存在则创建并写入,如果已经存在则在末尾处续写。
参数:输入字符串,文件名 }
function WriteTxtFile(Const sStr, sFileName: string): boolean;




implementation
{ TSocket }

constructor TSocket.Create(AOwner: TComponent);
begin
inherited;
//取SOCKET地址参数
self.address:=ReadIniFileS('socket','CliAddress','');
//取SOCKET端口参数
self.Port:=ReadIniFileI('socket','CliPort',0);
//取SOCKET等待时间
self.waitTime:=ReadIniFileI('socket','CliWaittime',20000);
//设置SOCKET类型
self.ClientType:=ctBlocking;
end;
{发送字符串
Text:需要发送的字符串;
返回值:收到的字符串}
function TSocket.sendText(Text: string): String;
var Stream : TWinSocketStream;
buff:array[0..1024] of char;
str:String;
begin
str:=Text;
try //激活SOCKET
self.Active:=true;
except //异常
text:='2191'+str;
result:=text;
exit;
end;
//发送报文
socket.SendText(Text);
//记录发报日志
WriteTxtFile(Text,'SendLog.txt');
Text:='';
while (self.Active) do
begin
try
Stream := TWinSocketStream.Create(Socket, waitTime);
try
FillChar(Buff, 1024, 0);
if Stream.WaitForData(waitTime) then
begin
if Stream.Read(Buff, 1024) = 0 then
self.Close;
Text:=Text+buff;
if text<>'' then
begin
//记录收报日志
WriteTxtFile(Text,'ReadLog.txt');
self.Close;
end //end ifthen
else
//收到的是空包
WriteTxtFile('收到 '+Text ,'ReadLog.txt');
end // end ifthen
else
begin
self.Close;
text:='2190'+str;//远程联接超时或收到的包长度不等于包头数值
result:=text;
exit;
end; //end ifelse
finally
Stream.Free;
end;
except //异常处理
on e:Exception do
begin
showmessage('交易接收异常');
self.Close;
end; //end exception
end;
end; //end whiled
self.Close;
result:=Text;
end;

{ TSerSocket }

constructor TSerSocket.Create(AOwner: TComponent);
begin
inherited;
self.Port:=ReadIniFileI('socket','SerPort',8000);
end;



//读取INI配置文件参数
function ReadIniFileI(const sect,flag: string; default: Integer): integer;
var
iniFile :TIniFile;
begin
try
IniFile := TIniFile.Create(ExtractFilePath(ParamStr(0))+'sysfile.ini');
result := iniFile.ReadInteger(sect,flag,default);
iniFile.Free;
except
on e:exception do
begin
showmessage('读取INI配置文件参数异常');
result:=-1;
end;
end;
end;
function ReadIniFileS(const sect,flag: string; default: string): string;
var
iniFile :TIniFile;
begin
try
IniFile := TIniFile.Create(ExtractFilePath(ParamStr(0))+'sysfile.ini');
result := iniFile.ReadString(sect,flag,default);
iniFile.Free;
except
on e:exception do
begin
showmessage('读取INI配置文件参数异常');
result:='异常';
end;
end;
end;
//写文本文件
function WriteTxtFile(Const sStr, sFileName: string): boolean;
var
F: TextFile;
sPath: string;
begin
try

sPath := extractfilepath(paramstr(0));
AssignFile(f, sPath + sFileName);
if not fileexists(sPath + sFileName) then
rewrite(f)
else
Append(f);
writeln(f,sStr);
closefile(f);
result := true;

except
result := false;
end;
end;

end.
...全文
205 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cdstarnet 2005-08-21
  • 打赏
  • 举报
回复
2. 您要找的这款产品在星品网(http://www.cdstar.net)里有售,是原装的光盘,价格只需4元。
zjntche_cn 2005-08-09
  • 打赏
  • 举报
回复
这个问题自己搞定了,虽然DLL文件在PowerBuilder6中使用会出问题,但在PowerBuilder8中使用却未出现问题。版主可以结帖了!

1,316

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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