Page 1 of 1

Update from Website

Posted: Thu May 19, 2011 8:07 pm
by Slyvnr
Do you have a program that you are distributing that you would like to have either Auto-Updated from a website, or allow the user to check for updates and install them automatically?

I did a search and found several programs but why pay for something you can do yourself?

Here is the basic framework for Windows based systems. I am using a Windows Built-in DLL but I am sure this can also be done using the PureBasic Ftp commands.

The process is basically

1) You have a website
2) on your website you have a file (i.e. in the code below it is called updatefile.txt)
3) Updatefile.txt (or whatever you call your file) contains 1 line that is the current release version number:

3.25

This indicates that the current release version is 3.25

4) On the end users local machine you maintain a "version.txt" file in the directory where your program resides this file looks like this:

2.10

This indicates that we are running version 2.10

At this point you can either implement a messagerequester that ask if the user wants to check for updates, or you can just have your program check everytime it is started and if there is an update notify the user and let them choose to install or not at that time.

Assuming the user selected to check/install updates
5) using the below code you call the Get_Current_Release_Version() procedure to download the "updatefile.txt" to a temporary location
6) get the current release version from this file and store to a current_release variable
7) get the current release version from the local "version.txt" file and store to a our_version variable
8) compare the values
9) if the values are different then run the Get_Update_File() procedure
10) if the Get_Update_File() completed successfully then RunProgram(directory$+"update.exe") and close your program so update can take place

Note: In your Update.exe file be sure to delete the "Version.txt" file and replace it with a new "Version.txt" file that contains the new version number ("3.25" in our above example)

Code: Select all

Procedure Get_Current_Release_Version(website$, versionfile$)
    result=OpenLibrary(0,"urlMON.dll")
    
    If result
      result=CallFunction(0,"URLDownloadToFileA",0,@website$,@versionfile$,0,0)
      Delay(1000)
    EndIf     
    CloseLibrary(0) 
      
     ProcedureReturn result
EndProcedure

Procedure Get_Update_File(website.s, updatefile.s)  
     result=OpenLibrary(0,"urlMON.dll")     
     If result
          result=CallFunction(0,"URLDownloadToFileA",0,@website,@updatefile,0,0)
     EndIf
     Delay(1000)
     ProcedureReturn result
EndProcedure


;--Update Start
result=MessageRequester("Update Check","Would you like to check for any updates now?", #PB_MessageRequester_YesNo)
If result=#PB_MessageRequester_Yes
   
     ; setup variables for where the "current release version" is stored in a text file
     website$="http://www.{WEBSITE_NAME_HERE}.com/updates/"
     ; You do not have to setup an "updates" subdirectory, but I would expect that it makes it easier to maintain.
     ; You might even have /{PROGRAM_NAME}/updates if your website hosts multiple program releases
     
     versionfile$="updatefile.txt"
     ; this is the name of the file to be stored on the "local" computer to check version information from
     webfile$=website$+versionfile$

     ; setup variables for temporary storage of the "current release version" text file on local machine
     If ExamineEnvironmentVariables()
          While NextEnvironmentVariable()
               If UCase(EnvironmentVariableName())="TEMP"
                    tmp_dir$=EnvironmentVariableValue()
               EndIf
          Wend
     EndIf
     If tmp_dir$=""
          tmp_dir$=GetCurrentDirectory()
     EndIf
     
     outfile$= tmp_dir$+"\updatefile.txt"
 
     ; Get the "Current Release Version" text file
     result=Get_Current_Release_Version(website$,outfile$)
     Delay(100)
   
         ; Get the Current Release Version string from the text file
          OpenFile(200,outfile$)
          releaseversion$=ReadString(200)
          CloseFile(200)
    

     ; Get the Current Version of our software
     ourversionfile$=GetCurrentDirectory()+"\version.txt"
     OpenFile(100,ourversionfile$)
     ourversion$=ReadString(100)
     CloseFile(100)

     ; If Our Current Version is different from the Current Release Version download update
          
     If ourversion$<>releaseversion$
          
          updatefile$="update.exe"
          updatesite$=website$+updatefile$
          saveupdatefile$=tmp_dir$+"\update.exe"
    
          result=Get_Update_File(updatesite$, saveupdatefile$)
                
          If result
             ;Remove the temp Current Release Version text file
             ;DeleteFile(outfile$)  
             RunProgram(saveupdatefile$)
            ;because we did not include any option in the RunProgram call the next line of code should process immediately and close this program
             End
          EndIf
     Else
          MessageRequester("Update Info","Your version of the software is up to date", #PB_MessageRequester_Ok)
     EndIf
     
  EndIf


;-- Update End


;-- Main program start
   


I have not fully tested this code. I have tested parts of it and this should be the correct logic. Once I fully implement in my program I will update with better code as I work through any problems.

-----------------------------
** EDIT **
Ok Above code works now... Made a few changes and all is good for me.

I do not want to do the FTP idea as then a username and password would be needed unless your website allows for "anonymous" user logins.
----------------------------

Take care

Sly'

Re: Update from Website

Posted: Fri May 20, 2011 8:00 am
by jesperbrannmark
Sounds brilliant, thats really something everyone needs.
I guess the "norton trojan" that everyone seem to install on their PC's (and even pay for) would probably remove this program without even asking the end user (or give the chance to take it back)....

Anyone got any way around this?

Re: Update from Website

Posted: Sun May 22, 2011 11:03 pm
by happer66
Simple yet efficient, thanks.

A few small changes and it should work:

Code: Select all

Procedure Get_Current_Release_Version(website$, versionfile$)
    result=OpenLibrary(0,"urlMON.dll")
   
    If result
      result=CallFunction(0,"URLDownloadToFileA",0,@website$,@versionfile$,0,0)
      Delay(1000)
    EndIf     
    CloseLibrary(0)
     
     ProcedureReturn result
EndProcedure

Procedure Get_Update_File(website.s, updatefile.s) 
     result=OpenLibrary(0,"urlMON.dll")     
     If result
          result=CallFunction(0,"URLDownloadToFileA",0,@website,@updatefile,0,0)
     EndIf
     Delay(1000)
     ProcedureReturn result
EndProcedure


;--Update Start
Response = MessageRequester("Update Check","Would you like to check for any updates now?", #PB_MessageRequester_YesNo) ;edited
If Response = #PB_MessageRequester_Yes
   
     ; setup variables for where the "current release version" is stored in a text file
     website$="http://www.{WEBSITE_NAME_HERE}.com/updates/"
     ; You do not have to setup an "updates" subdirectory, but I would expect that it makes it easier to maintain.
     ; You might even have /{PROGRAM_NAME}/updates if your website hosts multiple program releases
     
     versionfile$="updatefile.txt"
     ; this is the name of the file to be stored on the "local" computer to check version information from
     webfile$=website$+versionfile$

     ; setup variables for temporary storage of the "current release version" text file on local machine
     If ExamineEnvironmentVariables()
          While NextEnvironmentVariable()
               If UCase(EnvironmentVariableName())="TEMP"
                    tmp_dir$=EnvironmentVariableValue()
               EndIf
          Wend
     EndIf
     If tmp_dir$=""
          tmp_dir$=GetCurrentDirectory()
     EndIf
     
     outfile$= tmp_dir$+"\updatefile.txt"

     ; Get the "Current Release Version" text file
     result=Get_Current_Release_Version(webfile$,outfile$) ;edited
     Delay(100)
   
         ; Get the Current Release Version string from the text file
          OpenFile(200,outfile$)
          releaseversion$=ReadString(200)
          CloseFile(200)
   

     ; Get the Current Version of our software
     ourversionfile$=GetCurrentDirectory()+"\version.txt"
     OpenFile(100,ourversionfile$)
     ourversion$=ReadString(100)
     CloseFile(100)

     ; If Our Current Version is different from the Current Release Version download update
         
     If ourversion$<>releaseversion$
         
          updatefile$="update.exe"
          updatesite$=website$+updatefile$
          saveupdatefile$=tmp_dir$+"\update.exe"
   
          result=Get_Update_File(updatesite$, saveupdatefile$)
               
          If result = #S_OK ;edited
             ;Remove the temp Current Release Version text file
             ;DeleteFile(outfile$) 
             RunProgram(saveupdatefile$)
            ;because we did not include any option in the RunProgram call the next line of code should process immediately and close this program
             End
          EndIf
     Else
          MessageRequester("Update Info","Your version of the software is up to date", #PB_MessageRequester_Ok)
     EndIf
     
  EndIf


;-- Update End


;-- Main program start
   

Re: Update from Website

Posted: Mon May 23, 2011 12:07 am
by Joakim Christiansen
What about programs that are not freeware?
Then there needs to be security added so the file can only be downloaded by registered users.
For example sending a POST request with the user's username and password instead of a GET, it's not hard though and thanks for sharing the code.