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

Posted on 2008-11-08
Last Modified: 2012-08-14
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

Question by:valerieschupbach
    LVL 84
    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?

    Author Comment

    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,



    Expert Comment

       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.


    Author Comment

    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.


    Expert Comment

    I mean Windows 2003 Server.  That's where I saw the same error.

    Author Comment

    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


    Expert Comment

    We found that if only fails when running from read-only share on a UNC path.  Are you running from a UNC share?

    Author Comment

    No, both source and destination are on a regular folder ("C:\USERS\..\DOCUMENTS\ETC...").

    Accepted Solution

    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.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
    What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
    In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now