MoveFileEx with MOVEFILE_DELAY_UNTIL_REBOOT on Vista returns error code 5 (access denied)

The code below correctly renames a file upon reboot on XP.

On a Vista machine error code 5 is returned, even when user with administrator privilege runs it. Has this to do with UAC and is there a way to upgrade rights through a VBA call?

Thanks for the input!

Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) As Long
Sub RenameOnReboot()
    Dim src As String, dst As String
    src = "C:\Users\Valerie\Documents\Source.txt"
    dst = "C:\Users\Valerie\Documents\Destination.txt"
    Debug.Print MoveFileEx(src, dst, 4)
    Debug.Print Err.LastDllError
End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Are you running this in Access?

Do you get this immediately upon running your RenameOfReboot?

Is the Source.txt actually in use during the call? That is, do you have it open either via the interface, or in code somewhere?

Finally: What's the purpose of doing this on reboot?
valerieschupbachAuthor Commented:
Yes, I am running the code in Access. The return code is immediate and no entries can be found in the registry.

The source.txt file is just a dummy file that is not used/opened during exceution.

The real use is in automatic upgrades of the access application via the internet.
- application runs as application.mde
- application.mde downloads Newapplication.mde
- during reboot application.mde is deleted and Newapplication.mde is renamed to application.mde

As said this works in XP; I believe that under Vista you will need the user to confirm execution (e.g. as when you start the registry editor). Note that when you disable user account control (UAC) the call works fine.

Hope this helps,


   Did you find a solution to this?  I have run into the same problem, but it is not even on Vista.  It is on 2003.

The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

valerieschupbachAuthor Commented:
No solution yet. But what do you mean by 2003?
The platform is Vista + Office 2007 (where it does not work) while on Office 2007 (and I guess Office 2003) with XP it is ok.

I mean Windows 2003 Server.  That's where I saw the same error.
valerieschupbachAuthor Commented:
Unfortunately nobody found a solution.
I posted a separate question to get a way to elevate privileges but also no answer.

Attached is a solution that works more or less, HOWEVER it overwrites other pending rename operations as I did not find a way to append the rename operations that may be pending from other programs (for example from an anti-virus program). SO PLS DO NOT USE THIS CODE AS IS. Basically instead of calling MoveFileEx the registry key is created directly.

In order for this solution to be complete I would need to retrieve the registry key value but frankly I do not know how to do that for a REG_MULTI_SZ registry key.

If anybody has some suggestions? This is not really satisfactory!


' Build the registry entry to rename a file upon reboot
Sub BuildMoveFileEx(strSource, strDestination)
    Dim strFile As String, intFile As Integer
    strFile = GetWindowsTempDir & "\BuildMoveFileEx.reg"
    strFile = "C:\Users\Henk de Wolde\Documents\MyScript.reg"
    intFile = FreeFile
    Open strFile For Output As intFile
    Print #1, "Windows Registry Editor Version 5.00"
    Print #1, ""
    Print #1, "[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager]"
    Print #1, """PendingFileRenameOperations""=hex(7):\"
    Print #1, BuildMoveFileExString("\??\" & strSource) & ",00,00,\"
    Print #1, BuildMoveFileExString("\??\" & strDestination) & ",00,00,00,00"
    Close #1
    Shell ("regedit """ & strFile & """")
End Sub
Function BuildMoveFileExString(s) As String
    Dim i As Integer
    BuildMoveFileExString = ""
    For i = 1 To Len(Nz(s))
        If BuildMoveFileExString = "" Then
            BuildMoveFileExString = asc2hex(Mid(s, i, 1)) & ",00"
            BuildMoveFileExString = BuildMoveFileExString & "," & asc2hex(Mid(s, i, 1)) & ",00"
        End If
End Function
Function asc2hex(c)
    Dim intAsc As Integer, intMultiple As Integer
    Dim strC
    intAsc = Asc(c)
    intMultiple = Int(intAsc / 16)
    strC = CStr(intMultiple)
    intAsc = intAsc - intMultiple * 16
    Select Case intAsc
        Case 0: strC = strC & "0"
        Case 1: strC = strC & "1"
        Case 2: strC = strC & "2"
        Case 3: strC = strC & "3"
        Case 4: strC = strC & "4"
        Case 5: strC = strC & "5"
        Case 6: strC = strC & "6"
        Case 7: strC = strC & "7"
        Case 8: strC = strC & "8"
        Case 9: strC = strC & "9"
        Case 10: strC = strC & "A"
        Case 11: strC = strC & "B"
        Case 12: strC = strC & "C"
        Case 13: strC = strC & "D"
        Case 14: strC = strC & "E"
        Case 15: strC = strC & "F"
    End Select
    asc2hex = strC
End Function

Open in new window

We found that if only fails when running from read-only share on a UNC path.  Are you running from a UNC share?
valerieschupbachAuthor Commented:
No, both source and destination are on a regular folder ("C:\USERS\..\DOCUMENTS\ETC...").
valerieschupbachAuthor Commented:
I will close this question. As a workaround I have created a separate application that is launched through a shell() and that performs the rename as soon as the launching application has exited. Not very nice, but works.

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
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
Microsoft Access

From novice to tech pro — start learning today.