Error 70 - Permission Denied

I have an application that checks if there is a new version available. If so, I launch another application (using Shell) to copy the new exe file from a Network drive down to the workstation.

I first terminate the original application from within the second application before i "Delete" the exe file and then copy the the file from the Network (the new exe file) onto the workstation. After copying, I re-launch the first application (using Shell again) and then terminate the second application.

Below is the code that I have and it works only if I have the MsgBox. Without it, it throws an Error 70 - Permission Denied error. I was thinking that without the MsgBox, the thread might not have terminated just yet and that the program is still in memory. That's why the exe file may be locked and thus giving the Permission Denied error. I've tried using Sleep to delay the deletion process to no avail.

Does anybody know the reason why this is happening? I can live with what I have right now but I would like to know how this MsgBox can affect the deletion process.

Thanks a lot!

    Dim fso As New FileSystemObject
    Dim fu As File, fi As File
    Dim a As Integer
    Dim iPath As String

    On Error GoTo Err_Handler
    'get update exe & installed exe
    Set fu = fso.GetFile(g_ini_UNLAB_Update & "\" & PROGRAM_NAME)
    Set fi = fso.GetFile(App.Path & "\" & PROGRAM_NAME)

    'wait 1 second then copy new version of the application
    SetMsg "Please wait... Copying updated version of the Application."
    Sleep 1000
    iPath = fi.Path     ': Set fi = Nothing       'save installed path to variable

    'without the msgbox below, this code will throw an error 70 - Permission Denied
    a = MsgBox("Application about to be upgraded! Continue?", vbQuestion + vbYesNo, Me.Caption)
    If a = vbNo Then Shell iPath & " 0", vbNormalFocus: GoTo Terminate
    'Kill iPath                              'delete old version
    fu.Copy iPath                           'copy new version
    SetMsg "Latest version of the application installed."
    Sleep 2000
    SetMsg "Please wait... relaunching latest version of the application."
    Sleep 3000
    'launch a new thread of latest application version
    Shell iPath, vbNormalFocus
    'destroy file system objects
    Set fso = Nothing: Set fu = Nothing: Set fi = Nothing
    'terminate this program
    Unload Me
    Exit Sub
    MsgBox Err.Number & " - " & Err.Description & " - " & Err.Source
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

SmallintConnect With a Mentor Commented:

Try to wait until YourOrginal.exe is really terminated.


Private Declare Function Process32First Lib "kernel32" _
(ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long

Private Declare Function Process32Next Lib "kernel32" _
(ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long

Private Declare Function CreateToolhelp32Snapshot Lib _
"kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
           dwSize As Long
           cntUsage As Long
           th32ProcessID As Long
           th32DefaultHeapID As Long
           th32ModuleID As Long
           cntThreads As Long
           th32ParentProcessID As Long
           pcPriClassBase As Long
           dwFlags As Long
           szExeFile As String * 260
End Type

Private Const TH32CS_SNAPPROCESS = &H2&

Public Function IsExeRunning(ByVal ExeFind As String) As Boolean
  Dim hProc As Long, ExeName As String
  Dim lRet As Long, Proc As PROCESSENTRY32
  Dim g As Long, sDeighton As String
  Dim sClean As String
  Dim sAtom As String
  hProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  IsExeRunning = False
  If hProc = 0 Then
      Exit Function
  End If
   Proc.dwSize = Len(Proc)
   lRet = Process32First(hProc, Proc)
   Do While lRet
     ExeName = Left(Proc.szExeFile, Len(Proc.szExeFile) - 1)
     sClean = ""
     For g = 1 To Len(ExeName)
         sAtom = Mid(ExeName, g, 1)
         If sAtom = Chr(0) Then
                Exit For
                sClean = sClean & sAtom
         End If
     sDeighton = ""
     For g = Len(sClean) To 1 Step -1
         If Mid(sClean, g, 1) = "\" Then Exit For
            sDeighton = Mid(sClean, g, 1) & sDeighton
     If UCase(sDeighton) = UCase(ExeFind) Then
          IsExeRunning = True
          Exit Function
     End If
      lRet = Process32Next(hProc, Proc)
End Function


While IsExeRunning("YourOrginal.exe") = true
     Sleep 1000

you can probably try to execute the same thing a few hundred time check for an error do again
put your doevent and sleep statements so whatever it is has a chance to run the the other process.

hope it works...

   Dim l As Long
    '1000 it will try 1000 times more if you want
    Do Until l >= 1000
        On Error Resume Next
        '##NOTE: what ever it is your trying to do.
        sleep 100  'hopefully smaller
PePiAuthor Commented:

thanks for all the help. SmallInt gets the points.
All Courses

From novice to tech pro — start learning today.