Re: self-autoupdate vb exe
- From: "Casey Provance" <casey@xxxxxxxxxxx>
- Date: Mon, 30 May 2005 13:26:30 -0400
Okay, here is how I do it...and it's been working so well for so lonog, I
figured it should be stable enough to share it.
I have two exe files...the main executable and another file I call
TpaPhx.exe and it operates only with a valid command line. This is done so
curious end users who try to start it manually will get nothing...the
program will simply end. With me so far?
Let's explore the update code in the main application. This code can be
easily reused in other programs with only a change in one or two lines of
code (specifically, those lines that check a web server for update
information.
The primary program has a function called CheckForUpdate (as there are a
plethora of ways to capture a file from your web server and parse it out, I
won't bother going into all of that...the point is, you have a function
which returns version information from your server that contains the version
number of your latest program). Once you have the current version and have
compared it to the current version running and determined that there is an
update available, we go to the next function which downloads two files.
The first file is a small text script (two lines actually) for the
TpaPhx.exe file to interprit. I call the file update.phx and keep it in the
same folder on my server as the updated file to download. I do this so I
can reuse the TpaPhx file with other programs...it isn't tied to just one
program.
So, my app has downloaded the update.phx file. The app now reads the two
lines in this file. The first line would be a command line that will be
used to pass to the TpaPhx file to let it know what kind of download it is
to install...either a single .exe file or an installation file. Either
method can be used with this updater. The second line of info in the .phx
file is the name of the update file to be downloaded. This is important to
diffienturate between the two types of updates available (replacement .exe
or installation.exe). Either way, the file needs to be downloaded with a
different extention so things don't get messed up. I use .new, but you can
use whatever you want, just as long as it is not .exe.
Let's look at this a little more closely:
I have an app called MyApp.exe and I want to update it. I want to simply
replace the exe file with a new one. I create a folder on my server, upload
the update as MyApp.exe and the phx script into the same directory. The
script has the following two lines:
rr
myapp.exe
The first line is the command line to be sent to the tpaphx.exe telling it
which kind of update it is. "rr" is for ResetRestart for single exe
replacement. The other command line that could be used is "setup" which
lets TpaPhx know that a setup file has been downloaded.
The second line is the name of the file to be downloaded. Both files will
be downloaded by the myapp.exe on your hard drive. So in your code you have
downloaded the phx script and downloaded the update, saving the update as
myapp.new. Accomplish this however you see fit.
Now it's time to read the contents of the .phx script and begin the updating
process. Here is the code I use (air code for this post, assume all
variable declared an FileExists function available). Because we are only
reading the first two lines, I stick them in an array, sData(0) and
sData(1). sData(0) contains the command line to send to TpaPhx.exe while
sData(1) holds the name of the updated program you just downloaded them.
The code below rewrites the information to the phx script so the TpaPhx has
the name of the file to be replaced and the name of the file to use to
replace the old file. Once this is done, we check for the existance of the
TpaPhx.exe and shell it with the necessary command line. Then end the
current program and the TpaPhx.exe take over and do the work.
sPath = App.path & "\update.phx"
If FileExists(sPath) Then
lFileNum = FreeFile
lIndex = 0
'Read the two lines from the .phx script
'and put them into a string array.
Open sPath For Input As #lFileNum
Do While Not EOF(lFileNum)
Line Input #lFileNum, sData(lIndex)
lIndex = lIndex + 1
Loop
Close #lFileNum
'Now, build the filepath and file name for
'the name of the file to be replaced, stick
'in a vbCrLf and then build the path and
'name of the replacement file (or setup).
OutputToFile App.path & "\update.phx", App.path & "\" & sData(1) &
vbCrLf & App.path & "\myapp.new"
'Check for the restart monitor and shell it
'using the command line read from the .phx
'script which was placed in sData(0).
If FileExists(App.path & "\tpaphx.exe") Then
Shell App.path & "\tpaphx.exe /" & sData(0)
End If
'End or Unload your program
'however you see fit.
UnloadAllForms
So now we have started tpaphx.exe with the proper command line and script
and instructed myapp.exe to end itself.
Create a new program and call it TpaPhx (or whatever you want, but if you
call it something else, please update the code to reflect the change.
The following code goes into the Sub Main of TpaPhx
Select Case Command$
Case "/rr"
mbIsSetupFile = False
Case "/setup"
mbIsSetupFile = True
Case Else
Exit Sub
End Select
ResetRestart
All we are doing is telling TpaPhx whether or not it needs to handle the
update file as an installion or a replacement by setting a module level
variable to either True or False, after which we begin the work via the
ResetRestart subroutine.
Public Function ResetRestart() As Boolean
Dim lFileNum As Long
Dim lIndex As Long
Dim sPhxFile As String
Dim sData(0 To 1) As String
Const PHX_OLD_FILE As Long = 0
Const PHX_NEW_FILE As Long = 1
On Error GoTo ErrorTrap
'The first thing we do is open the .phx script so we can get the
'location on the old file to get rid of
'and the new file to activate.
sPhxFile = App.Path & "\update.phx"
lFileNum = FreeFile
lIndex = 0
'The first line in the .phx script is the full path and name of
'the file to replace. We place this into a string array sData(0)
'The next line of the .phx script is the full path and name of
'The new file that will replace the old file and it goes into
'sData(1)
Open sPhxFile For Input As #lFileNum
Do While Not EOF(lFileNum)
Line Input #lFileNum, sData(lIndex)
lIndex = lIndex + 1
Loop
Close #lFileNum
'Now we check our module level variable to see if we are
'dealing with a replacement .exe or a setup file. If it's a
'replacement .exe then lets Sleep for a second then attempt
'to delete the old file. Now, there is a chance is could still
'be unloading at this point. The error trap will catch this
'and kick us back to the same line via Resume. This
'will continue until the old file has ended and been deleted.
'The reason for the second long sleep is because a closing
'file needs time between kill attempts or everything will get
'hung up and "freeze".
'We skip this step in the case of an installation .exe
'as my setup file check for running instances of the
'program to be installed and will handle it internally.
'If you use this, you should do the same.
If Not mbIsSetupFile Then
Sleep 1000
Kill sData(PHX_OLD_FILE)
End If
'Rename the new file to the file name is should be (.new to .exe)
Name sData(PHX_NEW_FILE) As sData(PHX_OLD_FILE)
'Start it up
Shell sData(PHX_OLD_FILE)
'Clean up, get rid of the .phx file, it's now useless
Kill sPhxFile
Exit Function
ErrorTrap:
Sleep 1000
Resume
End Function
Once TpaPhx has finished it's job and restarted your program (or started the
setup) it will end itself.
You have now successfull downloaded an update and installed it with only one
very small restart monitor and a two line script that can be used with any
program without any recoding of the restart monitor.
I could probably churn out some kind of sample app if need be or requested.
Hope this helps.
- Kev
.
- References:
- self-autoupdate vb exe
- From: Hunter
- self-autoupdate vb exe
- Prev by Date: Re: Edanmo's Task Scheduler
- Next by Date: Re: self-autoupdate vb exe
- Previous by thread: Re: self-autoupdate vb exe
- Next by thread: Trapping Keypress in a VB6 MDI form
- Index(es):
Relevant Pages
|