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

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
'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.
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
swimr612Author Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.