用Wininet函数,一个例子
unit Updater;
//{$A+,B-,D-,L-,M-,O+,Q-,R-,S-,X+,Y-}
(*
TChkUpdate 1.3
"Lets go get the net!"
-------- -------- -------- -------- -------- -------- -------- --------
Contents:
TChkUpdate - Component for checking wether an update of an application
is available on your homepage.
-------- -------- -------- -------- -------- -------- -------- --------
Copyrights (C) 1999 ved T Skovmand Eriksen
email: TSEriksen@cyberdude.com
ALL RIGHTS RESERVED
-------- -------- -------- -------- -------- -------- -------- --------
This source is freeware and may be used as follows:
* In development of in-house applications which will not be published.
* In development of freeware applications PROVIDED that credit is given
to me in the applications aboutbox or helpfile.
* In development of shareware and commercial applications PROVIDED that
I, Troels S Eriksen, recieve a free unlimited copy of all versions of
said application AND that credits are given to me in the applications
aboutbox or helpfile.
* In printed or electronic form (fx. in a book) or in a source-library
(fx. on CD) PROVIDED that I, Troels S Eriksen, recieve a royality-free
unlimited copy of said book or library.
* In any other way PROVIDED that I, Troels S Eriksen, give my explicit
permission to the suggested use.
By using this source you do acknowledge that the above restrictions apply to
you and your use of this component and that you will confirm by them.
-------- -------- -------- -------- -------- -------- -------- --------
Revision history:
TChkUpdate
01 - Component created.
02 - Ver. 1.1: Property for Version number check added.
03 - Internet Connection check added - doesn't seem to work, though...
04 - Ver. 1.2: AutoProxy property added thanks to Jura Kornienko.
05 - Minor bugfixes. Allows more loose syntax in ver/rev comments.
06 - Ver. 1.3: Background execute (Threaded) added.
07 - Threaded property added.
-------- -------- -------- -------- -------- -------- -------- --------
TChkUpdate Properties:
AutoProxy When TRUE uses IE's default proxy settings.
Homepage The URL to the main homepage of your company or
application. (Used by the OnFound method)
Revision Current revision. Is checked against the <!--rev #-->
comment on the webpage
Threaded When TRUE connects to the internet in a separate
thread for version/revision checking. When FALSE the
main thread is blocked while check occurs.
URL URL to an internet document containing the revision
comment to check for
Version Current version. Is checked against the <!--ver #.#-->
comment on the webpage
TChkUpdate Events
OnFound Triggered if the revision comment is greather than
the current revision. Use the Launch value to decide
wether the end-users default browser should open the
homepage.
OnNotFound Triggered if the above is not the true.
OnNoConnect Triggered if connection to the internet could not be
established or if the URL was not found.
TChkUpdate Methods:
Execute Connects to the url and checks the revision and version.
If Threaded is TRUE return value is always TRUE. If
Threaded is FALSE will only return TRUE if connection
to the server could be established and the URL found.
IsConnected Returns TRUE is an connection to the server defined
in the URL property may be established. FALSE will be
returned if no internet connection currently exists.
-------- -------- -------- -------- -------- -------- -------- --------
Requirements:
wininet.dll Windows 32bit Internet API library must be present
and working.
-------- -------- -------- -------- -------- -------- -------- --------
Thanks to:
Jura Kornienko Who had the ideas for rev. 04-06, made me sit up, add
a few lines of code and end up with a better component.
-------- -------- -------- -------- -------- -------- -------- --------
Bugs:
None reported - none fixed.
"It is not a bug, its an undisclosed feature for further extension of
this applications feasibility"
-------- -------- -------- -------- -------- -------- -------- --------
How to use:
First, create an webpage for your application. Somewhere within the
first kilobyte of this homepage, add a revision comment - like this:
"<!--rev #-->" and/or a version comment like this: "<!--ver #.##-->".
Everytime you update your app, increase the rev number (the #) or the
version number (the #.##) on this webpage.
Second, Drop a TChkUpdate component on your main form, enter the URL to
the webpage in the URL property and the current revision number in the
Revision property and the current version number in the version property.
Add some code to the OnFound / OnNotFound properties to inform the
end-user wether there is an update available or not. If you set the
variable Launch to true, the users browser will automatically show
the URL from the Homepage property. (Would be nice to ask the users
permission before launching anything).
If the homepage property points to an .zip or .exe-file - user should
automatically be asked to download/open the file directly (at least in
IE3 and later).
Then add some way that the user may call the Execute method (fx. a
button). Check the return value and inform the user if an internet
connection could not be made.
*)
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
{ Import function from Wininet.dll - this has been left out of Inprises
(Borlands) official import unit. Shame, Shame, Shame...
}
function InternetAttemptConnect(dwReserved
Word):dword; stdcall;
{ Then the component itself
}
type
TUpdateEvent = procedure (Sender:TObject;var Launch:boolean) of object;
type
TChkUpdate = class(TComponent)
private
{ Private declarations }
fAutoProxy : boolean;
fURL : string;
fHomepage : string;
fRevision : integer;
fVersion : string;
fThreaded : boolean;
fOnFound : TUpdateEvent;
fOnNotFound : TNotifyEvent;
fOnNoConnect : TNotifyEvent;
// procedure SetVersion(Value:string);
// function GetVersion:string;
protected
procedure ChkResult(WebVer:double;WebRev:integer);
function ThreadExecute:boolean;
function BlockingExecute:boolean;
public
{ Methods }
constructor Create(AnOwner:TComponent); override;
function IsConnected:boolean;
function Execute:boolean;
published
{ Properties }
property AutoProxy : boolean read fAutoProxy write fAutoProxy;
property Homepage : string read fHomepage write fHomepage;
property Revision : integer read fRevision write fRevision;
property Threaded : boolean read fThreaded write fThreaded;
property URL : string read fURL write fURL;
property Version : string read fVersion write fVersion;
{ Events }
property OnFound : TUpdateEvent read fOnFound write fOnFound;
property OnNotFound : TNotifyEvent read fOnNotFound write fOnNotFound;
property OnNoConnect: TNotifyEvent read fOnNoConnect write fOnNoConnect;
end;
{ ------- -------- -------- -------- -------- -------- -------- ------- }
TChkResult = procedure(WebVer:double;WebRev:integer) of object;
{ ------- -------- -------- -------- -------- -------- -------- ------- }
TThreadedChecker = class(TThread)
private
{ Private declarations }
anURL : string;
aFlag : DWORD;
fVer : double;
fRev : integer;
fCallBack : TChkResult;
procedure ReturnResult;
protected
procedure Execute; override;
constructor Create(flags:integer;URL:string;Callback:TChkResult);
end;
procedure Register;
implementation
uses
Wininet, ShellApi;
{ ------- -------- -------- -------- -------- -------- -------- ------- }