Use DELPHI to create online program upgrade information query

Source: Internet
Author: User
More and more programs support online upgrade. This article describes how to obtain upgrade information from the website. Here, I mainly use the version information to check whether the upgraded version needs to be downloaded.
The general principle is as follows:
1. place information text to the website.
 
2. Use TNMHTTP to obtain text from the website information text.
3. analyze the information required for text parsing.
4. Compare the program version to provide the upgrade information.
First, we place an information text to our website. This text has its own file format. I have 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 the token as an update.txt file and use the [] <> identifier to classify the information, including the program name, version, update date, and. In this case, upload the file to http://2ccc.com/update.txt.
Then we use the TNMHTTP component to get the content 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'); {retrieve website text}
Result: = NMHTTP1.Body;
End;
After obtaining the text, we need to separate the information. I use 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 strings 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'); {obtains the update information between program names}
If TmpStr = ''then
Result: = False else {unable to find the file upgrade information}
Begin
Ver: = CenterStr (TmpStr, '<ver>', '</ver> ');
Update. Version: = SeparateVerStr (Ver); {resolution Version}
Update. Date: = StrToDate (CenterStr (TmpStr, '<date>', '</date>'); {parsing Date}
Update. URL: = CenterStr (TmpStr, '<url>', '</url>'); {resolution upgrade address}
Result: = True;
End;
End;
Here, TUpdate is the record format of my defined information:
TSimpleVersion = record {simplified version information}
DwProductVersionMS: DWORD; {main version}
DwProductVersionLS: DWORD; {secondary version}
End;
TUpdate = record {upgrade information}
Name: String [63]; {program Name}
Version: TSimpleVersion; {Version}
Date: TDate; {Date}
URL: Required string ;{}
End;
The SeparateVerStr () function converts the upgraded version information in the string format to the simplified version information format:
Function SeparateVerStr (s: String): TSimpleVersion;
Const
Separator = '.'; {think '.' delimiter}
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 file. First, get your own version. I use the following function:
Function GetBuildInfo (FName: string): TSimpleVersion; {Get your 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; {main version}
Result. dwProductVersionLS: = dwFileVersionLS; {auxiliary version}
End;
FreeMem (VerInfo, VerInfoSize );
End;
Use the following function to compare the upgraded version and current version of the website. If TRUE is returned, a new version file is available:
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;
The basic method is described here. I have passed the debugging in the DELPHI + WIN2000 environment. I did not write the complete code here. On my homepage, I made a demo program, you can download and study it slowly.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.