X
xjzcg
Unregistered / Unconfirmed
GUEST, unregistred user!
这段代码在执行第三次的时候出现Access violation at address XXXXXX.write of 。。。的错误,前两次都正常运行。请各位高手看看问题出在那里。
//类型定义
TFtpSitInfo=Record
Name:String[30];
Port:integer;
UserName:String[30];
PassWord:String[30];
end;
THostName=record
HostName:String[30] ;
end;
// 其中 n 是TNMFTP控件 , 直接画在窗体上的,没有动态创建
procedure TFormMain.Button8Click(Sender: TObject);
var
s:String;
pChar;
Sit:TFtpSitInfo;
begin
if FileExists(Extractfilepath(application.exename)+'host.cfg') then
begin
//读取配置信息
sit:=ReadFtpSitInfo(Extractfilepath(application.exename)+'host.cfg');
n.Host:=sit.Name ;<------第三次在这里出错
n.Port:=sit.port;<------n是TNMFTP控件
n.UserID :=sit.UserName;
n.Password:=sit.PassWord ;
Try
//连接服务器
n.Connect;
except
application.MessageBox('与服务器连接失败','错误',64);
exit;
end;
//下载主机地址
Try
n.Download('host/host.zcg',Extractfilepath(application.exename)+'host.zcg');
except
application.MessageBox('主机地址文件下载失败','下载',64);
exit;
end;
n.Disconnect;
Try
s:=ReadHostNameFromFile(Extractfilepath(application.exename)+'host.zcg').HostName ;
getmem(p,length(s));
strpcopy(p,s);
ShellExecute(Application.Handle, 'open', 'IEXPLORE.EXE',
Pchar('http://'+P), nil, SW_NORMAL);
except
application.MessageBox('打开主机地址文件失败','错误',64);
end;
end
else
begin
application.MessageBox('服务器配置信息不存在,请配置服务器信息!','错误',64);
exit;
end;
end;
//用到的函数和过程
Function ReadHostNameFromFile(FileName:String):THostName;
var
HostFile: TFileStream;
FileFlag: WORD;
HostName:THostName;
begin
HostFile := TFileStream.Create(FileName, fmOpenRead);
try
with HostFile do
begin
Read(FileFlag, SizeOf(FileFlag));
if FileFlag <> $AA88 then
begin
ShowMessage('打开主机列表文件错误');
Exit;
end;
Read(HostName,SizeOf(THostName));
Result:=HostName;
end;
finally
HostFile.Free;
HostFiie:=nil;
end;
end;
Function ReadFtpSitInfo(Filename:String):TFtpSitInfo;
var
SitInfoFile: TFileStream;
FileFlag: WORD;
Sit:TFtpSitInfo;
begin
SitInfoFile := TFileStream.Create(FileName, fmOpenRead);
try
with SitInfoFile do
begin
Read(FileFlag, SizeOf(FileFlag));
if FileFlag <> $AA99 then
begin
ShowMessage('打开服务器配置错误');
Exit;
end;
Read(sit,SizeOf(TFtpSitInfo));
Result:=sit;
end;
finally
SitInfoFile.Free;
SitInfoFile:=nil;
end;
//类型定义
TFtpSitInfo=Record
Name:String[30];
Port:integer;
UserName:String[30];
PassWord:String[30];
end;
THostName=record
HostName:String[30] ;
end;
// 其中 n 是TNMFTP控件 , 直接画在窗体上的,没有动态创建
procedure TFormMain.Button8Click(Sender: TObject);
var
s:String;
pChar;
Sit:TFtpSitInfo;
begin
if FileExists(Extractfilepath(application.exename)+'host.cfg') then
begin
//读取配置信息
sit:=ReadFtpSitInfo(Extractfilepath(application.exename)+'host.cfg');
n.Host:=sit.Name ;<------第三次在这里出错
n.Port:=sit.port;<------n是TNMFTP控件
n.UserID :=sit.UserName;
n.Password:=sit.PassWord ;
Try
//连接服务器
n.Connect;
except
application.MessageBox('与服务器连接失败','错误',64);
exit;
end;
//下载主机地址
Try
n.Download('host/host.zcg',Extractfilepath(application.exename)+'host.zcg');
except
application.MessageBox('主机地址文件下载失败','下载',64);
exit;
end;
n.Disconnect;
Try
s:=ReadHostNameFromFile(Extractfilepath(application.exename)+'host.zcg').HostName ;
getmem(p,length(s));
strpcopy(p,s);
ShellExecute(Application.Handle, 'open', 'IEXPLORE.EXE',
Pchar('http://'+P), nil, SW_NORMAL);
except
application.MessageBox('打开主机地址文件失败','错误',64);
end;
end
else
begin
application.MessageBox('服务器配置信息不存在,请配置服务器信息!','错误',64);
exit;
end;
end;
//用到的函数和过程
Function ReadHostNameFromFile(FileName:String):THostName;
var
HostFile: TFileStream;
FileFlag: WORD;
HostName:THostName;
begin
HostFile := TFileStream.Create(FileName, fmOpenRead);
try
with HostFile do
begin
Read(FileFlag, SizeOf(FileFlag));
if FileFlag <> $AA88 then
begin
ShowMessage('打开主机列表文件错误');
Exit;
end;
Read(HostName,SizeOf(THostName));
Result:=HostName;
end;
finally
HostFile.Free;
HostFiie:=nil;
end;
end;
Function ReadFtpSitInfo(Filename:String):TFtpSitInfo;
var
SitInfoFile: TFileStream;
FileFlag: WORD;
Sit:TFtpSitInfo;
begin
SitInfoFile := TFileStream.Create(FileName, fmOpenRead);
try
with SitInfoFile do
begin
Read(FileFlag, SizeOf(FileFlag));
if FileFlag <> $AA99 then
begin
ShowMessage('打开服务器配置错误');
Exit;
end;
Read(sit,SizeOf(TFtpSitInfo));
Result:=sit;
end;
finally
SitInfoFile.Free;
SitInfoFile:=nil;
end;