VB6 - "Upgrade" currently-running exe by copying newer version from server

Posted on 2003-03-20
Medium Priority
Last Modified: 2008-02-01
I'm trying to create an "upgrade" feature in my application.  If the program determines that a newer version of the .exe exists on the server, clicking "Upgrade" should replace the currently-running program with the newer program, by copying it off of the server.  I can do this with no problem.

The problem arises in that the currently-running program must be overwritten, which obviously cannot be done while it is running.  So instead, I unload all of the forms in the project and in the unload method of the last form to be unloaded, I call upgrade.exe (which I created).  Upgrade.exe takes care of copying the new one off the server via CopyFile(overwrite = True), and then running the new application via Shell().  Running upgrade.exe is called using the Shell() command, which is the last line of the Unload method, so the main program should terminate immediately after executing this command.

However, even with DoEvents and a Sleep command in the upgrade.exe, I cannot make the first application close before upgrade.exe tries to overwrite it with the application from the server.  So obviously the overwriting fails and no "upgrade" occurs.

Any solutions or suggestions?  I am using VB6.
Question by:swimr612
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

saurabhgupta earned 300 total points
ID: 8176074
Hi swimr612,
To let the program delete itself
Open App.Path & IIf(Right(App.Path, 1) <> "\", "\Del.bat", "Del.bat") For Output As #1
Print #1, "@Echo off"
Print #1, ":S"
Print #1, "Del " & App.EXEName & ".exe"
Print #1, "If Exist " & App.EXEName & ".exe" & " Goto S"
Print #1, "Del Del.bat"
Close #1
Shell "Del.bat", vbHide
Unload Me
 You can do anything else in the batch file like overwriting the exe.


Expert Comment

ID: 8176405
'I launch the setup and then end my program...

Sub Main()
Dim rtn As Double

If App.PrevInstance Then
    MsgBox "C.M.I. is already running.", _
        vbCritical + vbOKOnly + vbSystemModal, "Load Error"
    Exit Sub
End If

Screen.MousePointer = vbHourglass


'Here I have some conditioning code to check version, etc. before running my setup so that the setup
runs only when required.  In your case you are copying the setup to the client and that's ok, I just run the setup from the server...  

    rtn = Shell("\\APP_SERVER\trk corp\CMI install\setup.exe", vbNormalFocus)
    Unload frmSplash  
    Exit Sub  'This ends my app but leaves the setup running

'Here I would continue wih the app because an update is not required...

End Sub

Your situation sounds a bit different but I still don't see why you can't launch a program and then exit the instigator.
LVL 38

Expert Comment

ID: 8176902
They can't run your exe off the server, eh?

I would try code like this in the Upgrade.exe.  You can diddle with the length of the Sleep commands to find the most responsive combination.

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Form_Load()
    Dim strCaption As String   'main form caption of the program you are replacing...
    Dim intCount As Integer
    strCaption = "My Application"
    Do While FindWindow(vbNullString, strCaption) > 0 And intCount <= 30
        Sleep 1000
        intCount = intCount + 1  'allow a timeout
    Sleep 500  'Just insurance so window hasn't unloaded, but filesystem still locked the exe.
    If intCount > 30 Then
        Unload Me
    End If
    'Program has unloaded so we can copy if we reach here
End Sub

Author Comment

ID: 8177064
Thanks everyone for your help!

I'm going to give the points to Saurabh as his solution was the fastest and most robust for my issue.  (Who ever thought I'd call a DOS batch file "robust"??  Anyway...)

trkcorp -- that wasn't the issue.  i was fine with running setup.exe, but i also wanted to create a "quick upgrade" option which only requires replacing the .exe file, as my versions rarely require new dll's, ocx's, etc., to be packaged...  for some reason i couldn't tell my upgrade.exe program to wait until the other program was done

PaulHews -- thanks, that was actually my best alternative, but i wanted something that was more robust than checking for the window name (though unlikely, it's possible that a different application could have the same name as mine, etc.)  Plus the static "sleep" counts add some unnecessary delay.

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question