?? downcfg.pas
字號:
//作者:莊斗
//zhuangd@21cn.com
//下載單元,創建類TDownCFG配置實例,
//server服務器http全名,無則在配置文件中區
//inifile:ini配置文件
//section:讀取的部分
//【×××】《--section
// server 《--服務器,傳入為空時取此值
// from 《--服務器文件全路徑 及文件
// to 《--本地存放文件相對(當前執行目錄)路徑及文件
// method 《--下載后處理方法 1:直接存盤
// 2:運行,直到運行結束
// 3:運行,直接返回
// 9:為信息顯示
// ver 《--版本號
// desc 《--文件描述
//使用:
//給ProcessBar賦值
//downfile下載文件
//writecfg更新CFG文件
//exefile執行下載的文件
//downcfg直接從服務器下載升級文件信息http://server/update/server.ini
//compareCFG比較2個文件,返回需要下載的文件個數,注意文件名的路徑
{modify:
2004.3.14 增加downcfg,writecfg函數
修改downfile的try部分
2004.3.20 修改下載存盤路徑方式,直接參盤到toto中,可以靈活設置toto值
增加zompareCFG函數
}
unit downcfg;
interface
uses
forms,windows,classes,SysUtils,inifiles, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP,IdGlobal, ComCtrls,dialogs,IdAntiFreezeBase,
IdAntiFreeze;
type
TDownCFG=class
inifile:string;
ini:Tinifile; //配置文件
Server:string; //http服務器
From:string; //下載URL
toto:string; //存盤URL
ver:string; //版本號
method:string; //處理方法
section:string;//部分
desc:string;//本描述
error:string;//錯誤信息
param:string;//運行參數
fileCnt:integer;//下載文件個數
ProgressBar: TProgressBar;//進度條
procedure http1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCountMax: Integer);
procedure http1Work(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
procedure http1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
private
FHttpOnBeginWork:TWorkBeginEvent ;
FHttpOnWork:TWorkEvent ;
FHttpOnWorkEnd:TWorkEndEvent ;
iDiv:integer;
public
property OnBeginWork:TWorkBeginEvent read FHttpOnBeginWork write FHttpOnBeginWork;
property OnWork:TWorkEvent read FHttpOnWork write FHttpOnWork;
property OnWorkEnd:TWorkEndEvent read FHttpOnWorkEnd write FHttpOnWorkEnd;
function downfile:boolean;
procedure writeCFG(source:TDownCFG;desc:TDownCFG);
function Exefile:boolean;
function downcfg:boolean;
function CompareCFG(server:string;client:string):integer;
constructor Create(server1:string;inifile:string;section:string); //override;
end;
var
anFree: TIdAntiFreeze;
implementation
uses DownMess;
procedure TDownCFG.http1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCountMax: Integer);
begin
ProgressBar.Position:=0;
ProgressBar.Max:=aworkcountmax;
iDiv:=1;
if aworkcountmax>3000000 then
iDiv:=60
else if aworkcountmax>1000000 then
iDiv:=20
else if aworkcountmax>500000 then
iDiv:=2;
end;
//下載條控制
procedure TDownCFG.http1Work(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
begin
ProgressBar.Position:=ProgressBar.Position+aworkCount div iDiv;
end;
procedure TDownCFG.http1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
ProgressBar.Position:=0;
end;
//運行文件直到結束,返回,進行下一步
function WinExecAndWait32(FileName:String; Visibility:integer):integer;
var
zAppName:array[0..512] of char;
zCurDir:array[0..255] of char;
WorkDir:String;
i:cardinal;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
StrPCopy(zAppName,FileName);
GetDir(0,WorkDir);
StrPCopy(zCurDir,WorkDir);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,zAppName, { 執行文件}
nil, { 指向安全屬性 }
nil, { 執行thread屬性 }
false, { handle標識 }
CREATE_NEW_CONSOLE or { 標識 }
NORMAL_PRIORITY_CLASS,
nil, { 新的環境塊 }
nil, { 當前目錄 }
StartupInfo, { 指向STARTUPINFO }
ProcessInfo)
then Result := -1 { pointer to PROCESS_INF }
else
begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,i);
result:=i;
end;
end;
function TDownCFG.downcfg:boolean;
begin
self.from:='http://'+server+'/update/server.ini';
self.toto:='server.ini';
if self.downfile then
result:=true
else
result:=false;
end;
//比較配置文件,返回需要下載的文件數
function TDownCFG.compareCFG(server:String;client:string):integer;
var
i,cnt,k:integer;
iniS,iniC:tinifile;
vS,vC:string;
begin
k:=0;
iniS:=tinifile.Create(server);
iniC:=tinifile.Create(client);
cnt:= iniS.ReadInteger('server','filecnt',0);
for i:=1 to cnt do
begin
vS:=iniS.ReadString('file'+inttostr(i),'ver','');
vC:=iniC.ReadString('file'+inttostr(i),'ver','');
if vS>vC then
inc(k);
end;
iniS.Free;
iniC.Free;
result:=k;
end ;
//更新配置文件
procedure TDownCFG.writeCFG(source:TDownCFG;desc:TDownCFG);
begin
ini:=tinifile.Create(extractfilepath(paramstr(0))+desc.inifile);
ini.WriteString('server','host',source.Server);
ini.WriteString(section,'From',source.From);
ini.WriteString(section,'To',source.toto);
ini.WriteString(section,'Ver',source.ver);
ini.WriteString(section,'Method',source.method);
ini.WriteString(section,'Desc',source.desc);
ini.Free;
end;
//下載文件后的處理
{9:顯示信息
1:下載保存
2:下載運行,結束后返回
3:下載運行,直接返回
}
function TDownCFG.ExeFile:boolean;
var
id:integer;
begin
result:=true;
case strtoint(method) of
9://顯示消息
begin
mess:=TMess.Create(nil);
mess.left:= (screen.Width-mess.Width) div 2;
mess.top:=(screen.Height-mess.Height) div 2;
mess.RE1.Lines.LoadFromFile(extractfilepath(paramstr(0))+toto);
mess.ShowModal;
mess.Free;
end;
1:;//存盤文件
2://執行文件,直到文件運行結束
begin
//winexec(pchar(extractfilepath(paramstr(0))+toto+' '+param),sw_shownormal);
WinExecAndWait32(extractfilepath(paramstr(0))+toto+' '+param,1);
//showmessage('end');
end;
3://執行文件,立即返回
begin
winexec(pchar(extractfilepath(paramstr(0))+toto+' '+param),sw_shownormal);
end;
end;
end;
//下載文件,from-->toto
function TDownCFG.downfile:boolean;
var
MyStream:TMemoryStream;
HTTP1: TIdHTTP;
begin
if not assigned(ProgressBar) then
begin
error:='配置文件初始化失敗processBar';
result:=false;
exit;
end;
if toto='' then
begin
error:='目標文件空';
result:=false;
exit;
end;
try
MyStream:=TMemoryStream.Create;
except
result:=false;
error:='創建Stream失敗';
exit;
end;
try
http1:=TIdHTTP.Create(nil);
except
result:=false;
error:='創建http失敗';
MyStream.Free;
exit;
end;
//初始化http控件
http1. MaxLineAction := maException ;
http1.AllowCookies:= True;
http1.ReadTimeout:=0;
http1.ProxyParams.BasicAuthentication := False;
http1.ProxyParams.ProxyPort := 0;
http1.Request.ContentLength := -1;
http1.Request.ContentRangeEnd := 0;
http1.Request.ContentRangeStart := 0;
http1.Request.ContentType := 'text/html' ;
http1.Request.Accept := 'text/html, */*' ;
http1.Request.BasicAuthentication := False;
http1.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
http1.HTTPOptions := [hoForceEncodeParams];
http1.OnWork:=OnWork;;
http1.OnWorkBegin:=OnBeginWork;;
http1.OnWorkEnd:=OnWorkEnd;;
try
http1.Get(from,Mystream);
result:=true;
except
on e:exception do
begin
result:=false;
error:=e.Message;
mystream.Free;
http1.Free;
exit;
end;
end;
chdir(extractfilepath(paramstr(0)));
try
MyStream.SaveToFile(toto);
except
on e:exception do
begin
result:=false;
error:=e.Message;
end;
end;
mystream.Free;
http1.Free;
end;
//類實例創建,初始化一些值
constructor TDownCFG.Create(server1:string;inifile:string;section:string);
begin
inherited Create;
ini:=tinifile.Create(extractfilepath(paramstr(0))+inifile);
if server1<>'' then
server:=server1
else
server:=ini.ReadString('server','host','127.0.0.1');
fileCnt:=ini.ReadInteger('server','filecnt',0);
param:=ini.ReadString(section,'param','');
from:=ini.ReadString(section,'From','');
Toto:=ini.ReadString(section,'To','');
ver:=ini.ReadString(section,'ver','');
method:=ini.ReadString(section,'Method','');
desc:=ini.ReadString(section,'Desc','');
self.inifile:=inifile;
self.section:=section;
FHttpOnBeginWork:= http1WorkBegin;
FHttpOnWork:= http1Work;
FHttpOnWorkEnd:= http1WorkEnd;
ini.Free;
anFree:= TIdAntiFreeze.Create(nil);
anfree.OnlyWhenIdle:=false;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -