1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| unit CheckVersion; // Inventé par AndNotor
interface
uses
Windows, Messages, SysUtils, Classes, URLMon, ShellAPI, Forms;
const
WM_NEWVERSION = WM_USER; //Message Nouvelle version disponible
WM_DOWNLOADCOMPLETE = WM_USER +1; //Téléchargement terminé (ou erreur)
type
//Tâche contrôle de version
TCheckVersionThread = class(TThread)
private
Wnd :hWnd; //Fenêtre qui reçoit les notifications
RemoteFile :string; //HTTP://... fichier distant contenant le numéro de version
CurrentVersion :string; //Version actuellement installée
protected
function CompareVersion(aFileName :TFileName) :boolean;
procedure Execute; override;
public
constructor Create(const aWnd :hWnd; const aRemoteFile, aCurrentVersion :string);
end;
//Tâche téléchargement
TDownloadNewVersionThread = class(TThread)
private
Wnd :hWnd; //Fenêtre qui reçoit les notifications
RemoteFile :string; //HTTP://... fichier nouvelle version
protected
procedure Execute; override;
public
constructor Create(const aWnd :hWnd; const aRemoteFile :string);
end;
implementation
{ TCheckVersionThread }
function TCheckVersionThread.CompareVersion(aFileName: TFileName): boolean;
begin
//A toi d'implémenter ton contrôle de version :-)
//Pour la demo, renvoi toujours vrai !
Result := TRUE;
end;
constructor TCheckVersionThread.Create(const aWnd :hWnd; const aRemoteFile, aCurrentVersion: string);
begin
inherited Create(FALSE);
Wnd := aWnd;
RemoteFile := aRemoteFile;
CurrentVersion := aCurrentVersion;
//Libération automatique à la fin du thread
FreeOnTerminate := TRUE;
end;
procedure TCheckVersionThread.Execute;
var
TempFileName :array[0..MAX_PATH] of Char;
begin
//Télécharge dans le cache internet.
//On aurait aussi pu récupérer directement une chaîne
//avec un idHttp sans passer par un fichier
if URLDownloadToCacheFile(nil, PChar(RemoteFile), TempFileName, MAX_PATH, 0, nil) = S_OK then
//Comparaison...
if CompareVersion(TempFileName) then
//...et le cas échéant, notifie la fiche principale
PostMessage(Wnd, WM_NEWVERSION, 0, 0);
end;
{ TDownloadNewVersionThread }
constructor TDownloadNewVersionThread.Create(const aWnd: hWnd; const aRemoteFile: string);
begin
inherited Create(FALSE);
Wnd := aWnd;
RemoteFile := aRemoteFile;
//Libération automatique à la fin du thread
FreeOnTerminate := TRUE;
end;
procedure TDownloadNewVersionThread.Execute;
var
TempFileName :array[0..MAX_PATH] of Char;
Result :integer;
begin
//Télécharge dans le cache internet...
Result := URLDownloadToCacheFile(nil, PChar(RemoteFile), TempFileName, MAX_PATH, 0, nil);
//...et notifie la fiche principale, même en cas d'erreur !
//Attend la décision de la tâche principale (installation (ID_YES) ou non)
if SendMessage(Wnd, WM_DOWNLOADCOMPLETE, Result, integer(@TempFileName)) = ID_YES then
begin
ShellExecute(0, 'open', TempFileName, nil, nil, SW_SHOWNORMAL);
PostMessage(Application.MainForm.Handle, WM_CLOSE, 0, 0);
end;
end;
end. |
Partager