用DELPHI製作線上程式升級資訊查詢

來源:互聯網
上載者:User
    越來越多的程式支援線上升級功能,本文介紹的就是如何從網站擷取升級資訊。這裡我主要使用版本資訊來檢測是否需要下載升級版本。
大致原理如下:
  1、放置資訊文本到網站。
 
  2、使用TNMHTTP從網站資訊文本擷取常值內容。
  3、分析文本解析所需資訊。
  4、比較程式版本提供升級資訊。
    首先,我們放置一個資訊文本到自己的網站,這個文本有自己的檔案格式,我定義了如下的格式:
  [update]
    <ver>1.79.9.25</ver>
    <url>http://delphibox.com/softm/3_update.zip</url>
    <date>2002-9-25</date>
  [/update]
我們可以將它儲存為update.txt檔案,使用[]<>的標示符將資訊分類,這裡包含了程式名、版本、更新日期和。這裡我假使上傳到http://2ccc.com/update.txt。
    然後我們使用TNMHTTP組件從網站擷取此檔案的內容:
function TForm1.GetUpdateText:String;
begin
  NMHTTP1.InputFileMode := FALSE;
  NMHTTP1.OutputFileMode := FALSE;
  NMHTTP1.ReportLevel := Status_Basic;
  NMHTTP1.Get('http://2ccc.com/update.txt'); { 擷取網站文本 }
  Result:=NMHTTP1.Body;
end;
    擷取文本以後,我們要將其中的資訊分離,我使用了如下的函數:
function TForm1.AnalyseUpdate(Body:String;var Update:TUpdate):Boolean;
var
  TmpStr,Ver:String;
  function CenterStr(Src:String;Before,After:String):String;
  { 這個函數用來分離兩個字串中間的字串,
    例如 ..('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'); { 得到程式名間的升級資訊 }
  if TmpStr='' then
    Result:=False else { 找不到此檔案升級資訊 }
  begin
    Ver:=CenterStr(TmpStr,'<ver>','</ver>');
    Update.Version:=SeparateVerStr(Ver); { 解析版本 }
    Update.Date:=StrToDate(CenterStr(TmpStr,'<date>','</date>')); { 解析日期 }
    Update.URL:=CenterStr(TmpStr,'<url>','</url>'); { 解析升級地址 }
    Result:=True;
  end;
end;
其中TUpdate是我定義的資訊的記錄格式:
  TSimpleVersion=record  { 簡化的版本資訊 }
    dwProductVersionMS: DWORD; { 主要版本 }
    dwProductVersionLS: DWORD; { 輔版本 }
  end;
  TUpdate=record       { 升級資訊 }
    Name:String[63];  { 程式名 }
    Version:TSimpleVersion; { 版本 }
    Date:TDate;  { 日期 }
    URL:ShortString;  { }
  end;
而SeparateVerStr()函數是將得到字串形式的升級版本資訊轉換為簡化的版本資訊格式:
function SeparateVerStr(s:String):TSimpleVersion;
const
  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;
    最後要做的就是比較檔案的版本資訊,先得到自己的版本,我使用如下的函數:
function GetBuildInfo(FName:string):TSimpleVersion; { 得到自身版本資訊 }
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; { 主要版本 }
    Result.dwProductVersionLS := dwFileVersionLS; { 輔版本 }
  end;
  FreeMem(VerInfo, VerInfoSize);
end;
然後使用如下的函數比較網站的升級版本和現在的版本,如果返回TRUE,說明有新版本檔案:
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;
    到這裡基本方法就介紹完了,我在DELPHI+WIN2000環境調試通過,這裡我沒有寫出完整的代碼,在我的首頁我做了示範程式,大家可以下載慢慢研究。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.