Function for Automatic Update

Function for Automatic Update

Postby George » Tue Aug 22, 2006 3:17 pm

Below the functions that I am using to do automatic update from Internet server. Any comments or enhancement are welcome


George


Code: Select all  Expand view
FUNCTION Main()
   // In this sample the name of our program is 'MyProgram.exe'
   //The name of the version to be downloaded from internet server is '_MyProgram.exe'

  //Add to the start of your program

  // Name of the running program, including the path
  cPathAppliName :=    alltrim(hb_argv( 0 ))
  nLen := len(cPathAppliName)
  nAtNum := atnum( "\", cPathAppliName)
  nPos := nLen - nAtNum

  // name of the running program only
  // Could be  'MyProgram.exe' oor "_MyProgram.exe"
  cAppliName := right(cPathAppliName, nPos)

  // Original program name
  cOrigAppli := alltrim(cAppliDir) + "\MyProgram.exe"

  // Ck if is running the downloaded program "_MyProgram.exe"
   IF upper((cPathAppliName)) <> upper((cOrigAppli))
       // Yes is running the new vesionn!!!

       // Erase the original 'MyProgram.exe'
       ferase(cOrigAppli) <> 0
       syswait(1)

       // Rename the new version with the name of the original version
       RENAME  "_MyProgram.exe" TO "MyProgram.exe"
   ENDIF

   /*

   Your code

   */


RETURN (.T.)


FUNCTION CkUpdate()
   // This is the function that you execute to verify
   // if exist a new version of your software

   dbselectarea("PATHFILE") // File with server's parameters
   cAlias := alias()
   GO 1

   cServFtp := alltrim((cAlias)->SERVER) // Server address
   cUserFTp := alltrim((cAlias)->USER)   // User  Name
   cPassFTp := alltrim((cAlias)->PASSW)   // Password
   cServPath := alltrim((cAlias)->SERPATH) // Server path for _MyProgram.exe'
   cVersion2 := (cAlias)->VERSION          // Current Version of 'MyProgram.exe'

   Dbcloseall()

   // Go to the server to download last version number
   IF !GetUpdateFromServer(cServFtp, cUserFTp, cPassFTp, cServPath, cVersion2 )
      // Do not exist a new version or connection to server failed
      RETURN .F.
   ENDIF

   // New version '_MyProgram.exe' has beeb downloaded from internet server
   cFileExe := (cAppliDir) + "\_MyProgram.exe"

   OpenFiles() // Function to open the database

   // RUN THE NEW VERSION '_MYPROGRAM.EXE' DOWNLOADED FROM INTERNET SERVER
   WinExec("_MyProgram.exe")

   // 'KILL' THE CURRENT SOFTWARE 'MYPROGRAM.EXE'
   PostQuitMessage(0)
   __Quit()

RETURN (.T.)


Function GetUpdateFromServer( cServFtp, cUserFTp, cPassFTp, cServPath, cVersion2 )

   Local oInternet, oFtp, oMeter, oText
   Local cTitle := "Connecting to SDN Server", lExistAlt := .F.
   LOCAL hInternet, hConnect, cAlias, cAlias1, cNewTitle, lGetFileAll := .F.

   PRIVATE oSay

   // Dialog with a message indicating conection to server
   DEFINE DIALOG oDlg RESOURCE "DIALOG_13";
      TITLE cTitle

   REDEFINE SAY oSay ID 55 of oDlg

   ACTIVATE DIALOG oDlg NOWAIT

   oSay:SetText ("Checking for update....")

   hLib = LOADLIBRARY( "wininet.dll" )

   hInternet = INETOPEN( "Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 )

   hConnect = INETCONNECT( hInternet, cServFtp, INTERNET_INVALID_PORT_NUMBER,;
      cUserFTp, cPassFTp, INTERNET_SERVICE_FTP, 0, 0 )

   IF hConnect = 0
      MsgAlert("Connection to server failed!" + chr(13)+;
         "Please check your internet connection and try later.")
      oDlg:end()
      OpenFiles()
      RETURN .F.
   ENDIF


   // P_VERS.DBF is a file wih one field with the Program  Version Number
   cDownUPD := cAppliDir + "\P_VERS.DBF"
   cLastUPD := cServPath + "/P_VERS.DBF"

   lGetLastUPD := FTPGETFILE( hConnect, cLastUPD, cDownUPD, 0, FILE_ATTRIBUTE_ARCHIVE, 0, 0 )

   IF !lGetLastUPD
      MsgAlert("P_VERS Downloaded failed!" + chr(13) +;
         "Please check your internet connection!")
      INETCLOSEHANDLE( hConnect )
      INETCLOSEHANDLE( hInternet )
      FREELIBRARY( hLib )
      OpenFiles()
      oDlg:End()
      RETURN .F.
   ENDIF

   OpenFiles()

   dbselectarea("P_VERS")
   cAlias := alias()
   GO 1
   cVersion1 := (cAlias)->VERSION

   dbcloseall()

   // Compare the VERSION No. of the current software that is running
   // with the VERSION No. of the software that is in internet server
   IF alltrim(cVersion1) == alltrim(cVersion2)
      // If is the same number, ther are not new version.
      Msginfo(" You already have the most recent version.")
      INETCLOSEHANDLE( hConnect )
      INETCLOSEHANDLE( hInternet )
      FREELIBRARY( hLib )
      OpenFiles()
      oDlg:End()
      RETURN .F.
   ENDIF


   // If different proceed to download the new program version that is in the Server
   cDataSDN := cServPath + "/_MyProgram.exe"
   cPathDir := cAppliDir + "\_MyProgram.exe"

   oDlg:End()

   MsgRun( "Downloading Update Files Please Wait...", "My Program",;
      { || lGetFileAll := FTPGETFILE( hConnect, cDataSDN, cPathDir, 0, FILE_ATTRIBUTE_ARCHIVE, 0, 0 ) } )

   IF !lGetFileAll
      MsgAlert("File Downloaded failed!" + chr(13) +;
         "Please check your internet connection!")
      INETCLOSEHANDLE( hConnect )
      INETCLOSEHANDLE( hInternet )
      FREELIBRARY( hLib )
      OpenFiles()
      RETURN .F.
   ENDIF

   INETCLOSEHANDLE( hConnect )
   INETCLOSEHANDLE( hInternet )
   FREELIBRARY( hLib )

   oDlg:end()
RETURN .T.

George
 
Posts: 725
Joined: Tue Oct 18, 2005 6:49 pm

Postby Silvio » Fri Aug 25, 2006 2:11 pm

where i can download an sample to test it : there are not some function and res files ...

Regards
Best Regards, Saludos

Falconi Silvio
User avatar
Silvio
 
Posts: 3107
Joined: Fri Oct 07, 2005 6:28 pm
Location: Teramo,Italy


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Otto and 64 guests