[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1529
  • Last Modified:

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

0
valerieschupbach
Asked:
valerieschupbach
1 Solution
 
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?
0
 
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,

Valérie.


0
 
calonzoCommented:
Valérie,
   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.

Chris
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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.

Valérie
0
 
calonzoCommented:
I mean Windows 2003 Server.  That's where I saw the same error.
0
 
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!

Thanks
Valerie




' 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"
        Else
            BuildMoveFileExString = BuildMoveFileExString & "," & asc2hex(Mid(s, i, 1)) & ",00"
        End If
    Next
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

0
 
CommVaultCommented:
We found that if only fails when running from read-only share on a UNC path.  Are you running from a UNC share?
0
 
valerieschupbachAuthor Commented:
No, both source and destination are on a regular folder ("C:\USERS\..\DOCUMENTS\ETC...").
0
 
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now