More and more programs support online upgrade functions. This article introduces how to obtain upgrade information from the website. Here I mainly use version information to detect whether I need to download an upgraded version.
The general principle is as follows:
1. Place information text on the website.
2. Use TNMHTTP to obtain text content from website information text.
3. Analyze the information required for text parsing.
4. Compare program versions to provide upgrade information.
First, we place an information text on our website. This text has its own file format. I defined the following format:
[update]
<ver>1.79.9.25</ver>
<url>http://delphibox.com/softm/3_update.zip</url>
<date>2002-9-25</date>
[/update]
We can save it as an update.txt file and use the []<> identifier to classify the information, which includes the program name, version, update date and download address. Here I suppose I upload it to http://2ccc.com/update.txt.
We then use the TNMHTTP component to get the contents of this file from the website:
function TForm1.GetUpdateText:String;
begin
NMHTTP1.InputFileMode := FALSE;
NMHTTP1.OutputFileMode := FALSE;
NMHTTP1.ReportLevel := Status_Basic;
NMHTTP1.Get('http://2ccc.com/update.txt'); {Get website text}
Result:=NMHTTP1.Body;
end;
After obtaining the text, we need to separate the information. I used the following function:
function TForm1.AnalyseUpdate(Body:String;var Update:TUpdate):Boolean;
var
TmpStr,Ver:String;
function CenterStr(Src:String;Before,After:String):String;
{ This function is used to separate the string in the middle of two strings.
For example..('DelphiBox.com','Delphi','.com')=>'Box'. }
var
Pos1,Pos2:Word;
begin
Pos1:=Pos(Before,Src)+Length(Before);
Pos2:=Pos(After,Src);
Result:=Copy(Src,Pos1,Pos2-Pos1);
end;
begin
TmpStr:=CenterStr(Body,'update'); {Get the upgrade information between program names}
if TmpStr='' then
Result:=False else {Cannot find this file upgrade information}
begin
Ver:=CenterStr(TmpStr,'<ver>','</ver>');
Update.Version:=SeparateVerStr(Ver); {parse version}
Update.Date:=StrToDate(CenterStr(TmpStr,'<date>','</date>')); { Parse date}
Update.URL:=CenterStr(TmpStr,'<url>','</url>'); {Resolve upgrade address}
Result:=True;
end;
end;
Where TUpdate is the recording format of the information I defined:
TSimpleVersion=record{Simplified version information}
dwPRoductVersionMS: DWORD;{major version}
dwProductVersionLS: DWORD;{minor version}
end;
TUpdate=record {upgrade information}
Name:String[63];{program name}
Version:TSimpleVersion;{version}
Date:TDate;{date}
URL:ShortString;{Download address}
end;
The SeparateVerStr() function converts the upgraded version information in string form into a simplified version information format:
function SeparateVerStr(s:String):TSimpleVersion;
const
Separator='.'; {think '.' separator}
var
p,v1,v2,v3,v4:WORD;
begin
if Length(s)=0 then Exit;
p:=pos(Separator, s);
v1:=StrToInt(copy(s,1,p-1));
Delete(s,1,p);
p:=Pos(Separator,s);
v2:=StrToInt(copy(s,1,p-1));
Delete(s,1,p);
p:=Pos(Separator,s);
v3:=StrToInt(copy(s,1,p-1));
Delete(s,1,p);
v4:=StrToInt(s);
Result.dwProductVersionMS:=v1*$10000+v2;
Result.dwProductVersionLS:=v3*$10000+v4;
end;
The last thing to do is to compare the version information of the files and get your own version first. I use the following function:
function GetBuildInfo(FName:string):TSimpleVersion; {get own version information}
var
VerInfoSize: DWORD;
VerInfo: Pointer;
VerValueSize: DWORD;
VerValue: PVSFixedFileInfo;
Dummy: DWORD;
begin
VerInfoSize := GetFileVersionInfoSize(PChar(FName), Dummy);
GetMem(VerInfo, VerInfoSize);
GetFileVersionInfo(PChar(ParamStr(0)), 0, VerInfoSize, VerInfo);
VerQueryValue(VerInfo, '/', Pointer(VerValue), VerValueSize);
with VerValue^ do
begin
Result.dwProductVersionMS := dwFileVersionMS; {major version}
Result.dwProductVersionLS := dwFileVersionLS; {minor version}
end;
FreeMem(VerInfo, VerInfoSize);
end;
Then use the following function to compare the upgraded version of the website with the current version. If TRUE is returned, it means there is a new version of the file:
function VersionCheck(OriVer,NewVer:TSimpleVersion):Boolean;
begin
if (OriVer.dwProductVersionMS=NewVer.dwProductVersionMS) then
begin
Result:=OriVer.dwProductVersionLS<NewVer.dwProductVersionLS;
end else
begin
Result:=OriVer.dwProductVersionMS<NewVer.dwProductVersionMS
end;
end;