Results 1 to 20 of 20

Thread: VB6 Filecopy and FSO.copyfile

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    4

    VB6 Filecopy and FSO.copyfile

    Hi

    I have an application that copies a file to another different name in windows directory. Code has been working fine till we decided to implement it on WIN7. In win7 environment, code executes without any error, but it does not copy the file. however, if I use xcopy via DOS then it works fine. UAC is enabled.

    Here is the snippet of the code:

    Dim ofso As New FileSystemObject
    Dim frompath As String
    Dim topath As String


    frompath = "c:\windows\amsd.ini"
    topath = "c:\windows\work.ini"

    On Error GoTo error_process

    FileCopy frompath, topath

    ofso.CopyFile frompath, topath, True

    Set ofso = Nothing

    End
    error_process:
    MsgBox "error occured during copy" + vbCrLf + Err.Description
    Set ofso = Nothing

    End

    Any help would be appreciated.
    Thanks in Advance.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB6 Filecopy and FSO.copyfile

    if I use xcopy via DOS then it works fine.
    it is probably going to a virtual store, like "C:\Users\tester\AppData\Local\VirtualStore\Windows\vbaddin - Copy.ini"

    if you try to copy in windows explorer, it will ask for admin
    you could try to shell xcopy
    Last edited by westconn1; May 2nd, 2012 at 04:04 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    4

    Re: VB6 Filecopy and FSO.copyfile

    Having specified the windows directory in the path, why would the file go to virtual store directory?

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: VB6 Filecopy and FSO.copyfile

    Does the program work as you want it to if you "Run As Administrator" on the program?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,253

    Re: VB6 Filecopy and FSO.copyfile

    Does the API equivalent work OK on Win 7? (does for me)

    Code:
    Private Declare Function MoveFileW Lib "kernel32.dll" (ByVal lpExistingFileName As Long, ByVal lpNewFileName As Long) As Long
    
    Public Function MoveFile(pOldPath As String, pNewPath As String)
    
        MoveFileW StrPtr(pOldPath), StrPtr(pNewPath)
        
    End Function
    EDIT: This will work with Unicode chars in filenames, too

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 Filecopy and FSO.copyfile

    API calls won't do a thing to help if what we have here is a UAC violation being silently remediated via appcompat file virtualization.


    Of course until we see confimation we won't even know if this is what's going on.

    Quote Originally Posted by patelr View Post
    Having specified the windows directory in the path, why would the file go to virtual store directory?
    Your program probably isn't marked as "Vista aware" (Win7 is really Vista R2). If it was, then it would be dying on some form of security violation error like error 70. Instead Windows takes a sniff and considers you "legacy" and applies several appcompat shims such as the one for file virtualization.

    There was a ton of developer information published on this and related topics back in 2006. Much of it is gone because programmers are supposed to know it by now, and little remains except materials of a "reference manual" nature.

    You snooze, you lose.

    Instead we're mostly left with 3rd party tidbits on these topics, for example Folder Virtualization Concepts in Windows Vista.

    Under User Account Control, Windows Vista restricts portions of the Windows file system and registry. UAC also restricts write operations during normal operation (i.e., standard user mode). For example, applications no longer have unlimited access to C:\Program Files and C:\Windows, which has considerable ramifications because most software created for Windows today [2006-2007] expects unfettered access to all directories. The system-wide file system and Registry writes are automatically and silently redirected to per-user locations that won't harm the wider system.
    ...
    When an application installer attempts to write a file called Entry.txt in C:\Program Files, it is silently redirected to a Virtual Store directory located inside the current user's account. To the application, things proceed as normal, and it has no idea that it is being redirected. To the user, the application, too, still appears to be located at the old, expected location. But because the application is not access system-wide file locations, it cannot be used to harm the system.
    ...
    Registry virtualization works similarly. In this case, the HKEY_LOCAL_MACHINE\SOFTWARE hive is virtualized so that applications which attempt to store configuration information in system-wide portions of the Registry are re-directed to a new introduced structure under

    HKEY_CLASSES_USER\Classes\VirtualStore\MACHINE\SOFTWARE.

    As with file virtualization, each user on a system will have their own copy of configuration information that was previously issued once on a global basis.
    There might even be a FAQ here on VBForums, though you might find the FAQs here as difficult to navigate or search as I do.


    A great deal of the bad press Vista received was because software developers hadn't gotten their acts together. By the time Win7 hit the market this had changed, so people often feel Win7 somehow "improved" though it didn't.

    Those programmers who played Rip Van Winkle even longer and are only coming to the aging Windows 7 now (remember, it came out back in 2009!) simply have a tough row to hoe.

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    4

    Re: VB6 Filecopy and FSO.copyfile

    If I run the program using "Run as an administrator" then it works fine.


    When I run as a standard user, it doesn't copy the file under ..\virtual store\windows\ directory either.

  8. #8
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: VB6 Filecopy and FSO.copyfile

    Quote Originally Posted by patelr View Post
    If I run the program using "Run as an administrator" then it works fine.


    When I run as a standard user, it doesn't copy the file under ..\virtual store\windows\ directory either.
    To automatically elevate your program to "Admin" you can use something such as:

    vb Code:
    1. Option Explicit
    2.  
    3. Private Const BCM_SETSHIELD As Long = &H160C&
    4.  
    5. Private Declare Sub InitCommonControls Lib "comctl32" ()
    6.  
    7. Private Declare Function IsUserAnAdmin Lib "shell32" () As Long
    8.  
    9. Private Declare Function SendMessage Lib "user32" _
    10.     Alias "SendMessageA" ( _
    11.     ByVal hWnd As Long, _
    12.     ByVal wMsg As Long, _
    13.     ByVal wParam As Long, _
    14.     ByRef lParam As Any) As Long
    15.  
    16. Private Declare Function ShellExecute Lib "shell32" _
    17.     Alias "ShellExecuteA" ( _
    18.     ByVal hWnd As Long, _
    19.     ByVal lpOperation As String, _
    20.     ByVal lpFile As String, _
    21.     ByVal lpParameters As String, _
    22.     ByVal lpDirectory As String, _
    23.     ByVal nShowCmd As VbAppWinStyle) As Long
    24.  
    25. Private mblnIsElevated As Boolean
    26.  
    27. Public Function IsElevated() As Boolean
    28.     IsElevated = mblnIsElevated
    29. End Function
    30.  
    31. Public Sub OperationRequiringElevation(ByRef Params As Variant)
    32.     MsgBox "Insert logic here for: " & vbNewLine _
    33.          & Join(Params, vbNewLine)
    34. End Sub
    35.  
    36. Public Sub RequestOperation( _
    37.     ByVal hWnd As Long, _
    38.     ByVal Focus As VbAppWinStyle, _
    39.     ByRef Params As Variant)
    40.  
    41.     ShellExecute hWnd, "runas", App.EXEName & ".exe", _
    42.                  Join(Params, " "), CurDir$(), Focus
    43. End Sub
    44.  
    45. Public Sub SetShield(ByVal hWnd As Long)
    46.     SendMessage hWnd, BCM_SETSHIELD, 0&, 1&
    47. End Sub
    48.  
    49. Private Sub Main()
    50. Dim objForm As Form
    51.     If Len(Command$()) > 0 Then
    52.         'Assume we've been run elevated to execute an operation
    53.         'specified as a set of space-delimited strings.
    54.         OperationRequiringElevation Split(Command$(), " ")
    55.                   'unload all forms
    56.                    For Each objForm In Forms
    57.                    Unload objForm
    58.                    Set objForm = Nothing
    59.                    Next objForm
    60.                       Else
    61.         mblnIsElevated = IsUserAnAdmin()
    62.         InitCommonControls
    63.         Elevation
    64.     End If
    65.  
    66. End Sub
    67.  
    68.  
    69. Private Sub Elevation()
    70. Dim Params As Variant
    71.  
    72.        Params = Array(App.EXEName, "Put something here", "and here")
    73.     If IsElevated() Then
    74.         OperationRequiringElevation Params
    75.     Else
    76.         RequestOperation Form1.hWnd, vbHide, Params
    77.     End If
    78.     Form1.Show
    79. End Sub

    Just add that code to a module with a sub Main.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 Filecopy and FSO.copyfile

    Quote Originally Posted by patelr View Post
    If I run the program using "Run as an administrator" then it works fine.

    When I run as a standard user, it doesn't copy the file under ..\virtual store\windows\ directory either.
    This sounds farfetched. You should be seeing virtualization or a security error (unless you are actually masking all errors).

    Your "code snippet" looks like air code, surely you don't actually have End statements in it??? That and the use of "+" for string concatenation doesn't help the confidence level much.


    Can you create a real test case you can share? Here's one that works exactly as expected:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Const ssfDESKTOP = 0
        Const ssfWINDOWS = 36
        Dim OriginalFile As String
        Dim CopiedFile As String
        Dim F As Integer
        
        With CreateObject("Shell.Application")
            With .NameSpace(ssfDESKTOP).Self
                OriginalFile = .Path & "\test-orig.txt"
            End With
            With .NameSpace(ssfWINDOWS).Self
                CopiedFile = .Path & "\test-copy.txt"
            End With
        End With
        F = FreeFile(0)
        Open OriginalFile For Output As #F
        Print #F, "Testing!"
        Close #F
        DoEvents
        FileCopy OriginalFile, CopiedFile
        Kill OriginalFile
        MsgBox FileLen(CopiedFile)
    End Sub
    Attached Files Attached Files

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB6 Filecopy and FSO.copyfile

    To automatically elevate your program to "Admin" you can use something such as:
    this will not work in an enviroment where users do not have access to administrator level passwords, as in many businesses
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11
    Junior Member
    Join Date
    Dec 2011
    Location
    Tullahoma, TN
    Posts
    20

    Re: VB6 Filecopy and FSO.copyfile

    Quote Originally Posted by dilettante View Post
    By the time Win7 hit the market this had changed, so people often feel Win7 somehow "improved" though it didn't.
    In my little corner of the world it did; however this in no way means I disagree with the points you are making. I'm just a bit more moderate about it; I think Vista was rather unstable even for a new Microsoft platform, and I think that as you say developers didn't to the trouble of understanding it. The bad press was for both reasons, some deserved, some not.

    Specifically in my case, there was a bug in Vista that in some situations would cause the "turn Windows features on or off" screen to present a blank list of features. Research into the matter revealed that the Microsoft support people's approach to fixing this was to walk people through the process of wiping the drive and starting over. I was able to fix this without doing that, after several hours of research. (I don't remember precisely what I did, but it had to do with registry cleaning.) There was a problem in the registry where the system was prevented from writing the new version information for the module, presumably because it didn't have sufficient permissions to do so during some sort of update.

    I haven't found this particular problem in Windows 7, so my experience bears out the position that it is more stable than Vista, admittedly with very little data.
    Last edited by BobRodes; May 3rd, 2012 at 05:03 PM.

  12. #12
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: VB6 Filecopy and FSO.copyfile

    Quote Originally Posted by westconn1 View Post
    this will not work in an enviroment where users do not have access to administrator level passwords, as in many businesses
    Didn't think about that!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  13. #13
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,253

    Re: VB6 Filecopy and FSO.copyfile

    Quote Originally Posted by dilettante View Post
    API calls won't do a thing to help if what we have here is a UAC violation being silently remediated via appcompat file virtualization.


    Of course until we see confimation we won't even know if this is what's going on.
    Well, yes, that's true but at the time of the OP it was not clear that it was a UAC issue...

  14. #14
    Addicted Member
    Join Date
    Sep 2008
    Posts
    141

    Re: VB6 Filecopy and FSO.copyfile

    Quote Originally Posted by westconn1 View Post
    this will not work in an enviroment where users do not have access to administrator level passwords, as in many businesses
    You could elevate the user like the following.
    Code:
    Option Explicit
    
    Private Declare Function LogonUser Lib "advapi32.dll" Alias "LogonUserA" (ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Long, ByVal dwLogonProvider As Long, phToken As Long) As Long
    Private Declare Function RevertToSelf Lib "advapi32.dll" () As Long
    Private Declare Function ImpersonateLoggedOnUser Lib "advapi32.dll" (ByVal hToken As Long) As Long
    Private Declare Function DuplicateToken Lib "advapi32.dll" (ByVal ExistingTokenHandle As Long, ImpersonationLevel As Integer, DuplicateTokenHandle As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    Private Const LOGON32_PROVIDER_DEFAULT = 0
    Private Const LOGON32_PROVIDER_WINNT35 = 1
    Private Const LOGON32_PROVIDER_WINNT40 = 2
    Private Const LOGON32_PROVIDER_WINNT50 = 3
    
    Public Enum LOGON32_TYPE
        LOGON32_LOGON_INTERACTIVE = 2 'works for workstation
        LOGON32_LOGON_NETWORK = 3
        LOGON32_LOGON_BATCH = 4
        LOGON32_LOGON_SERVICE = 5
        LOGON32_LOGON_UNLOCK = 7
        LOGON32_LOGON_NETWORK_CLEARTEXT = 8
        LOGON32_LOGON_NEW_CREDENTIALS = 9 'works for network
    End Enum
    
    'Public Const ERROR_ACCOUNT_RESTRICTION = 1327
    
    Public Function ImpersonateLogon(ByVal xi_strAdminID As String, ByVal xi_strAdminPassword As String, ByVal xi_strDomain As String, ByVal xi_enmLogOnType As LOGON32_TYPE) As Long
        On Error GoTo Impersonation_Error
        
        Dim lngTokenHandle      As Long
        Dim intLogonType        As Integer
        Dim lngLogonProvider    As Long
        Dim blnResult           As Boolean
    
        intLogonType = xi_enmLogOnType
        lngLogonProvider = LOGON32_PROVIDER_WINNT50
    
        blnResult = RevertToSelf()
    
        blnResult = LogonUser(xi_strAdminID, xi_strDomain, xi_strAdminPassword, intLogonType, lngLogonProvider, lngTokenHandle)
    
        If blnResult = 0 Then
            ImpersonateLogon = Err.LastDllError
            MsgBox "mImpersonate.ImpersonateLogon failed with error " & Err.LastDllError, vbExclamation + vbOKOnly, "Impersonate (logon) Failed"
            Exit Function
        End If
    
        blnResult = ImpersonateLoggedOnUser(lngTokenHandle)
        
        DuplicateToken lngTokenHandle, intLogonType, 0
        
        If blnResult = 0 Then
            ImpersonateLogon = Err.LastDllError
            MsgBox "mImpersonate.ImpersonateLogon failed with error " & Err.LastDllError, vbExclamation + vbOKOnly, "Impersonate (logon) Failed"
            Exit Function
        End If
    
        CloseHandle (lngTokenHandle)
    
        ImpersonateLogon = 0
        
        Exit Function
        
    Impersonation_Error:
        If Err.Number <> 0 Then
            'ImpersonateLogon = Err.LastDllError
            MsgBox "mImpersonate.ImpersonateLogon failed with error " & Err.Number & vbCrLf & Err.Description
        End If
    End Function
    
    Public Function ImpersonateLogoff() As Boolean
        On Error GoTo ImpersonateLogoff_Err
    
        Dim blnResult As Boolean
    
        blnResult = RevertToSelf()
    
        If blnResult = 0 Then
            ImpersonateLogoff = Err.LastDllError
            MsgBox "mImpersonate.ImpersonateLogoff failed with error " & Err.LastDllError, vbExclamation + vbOKOnly, "Impersonate (logoff) Failed"
            Exit Function
        End If
        
        Exit Function
        
    ImpersonateLogoff_Err:
        If Err.Number <> 0 Then
            MsgBox "mImpersonate.ImpersonateLogoff failed with error " & Err.Number & vbCrLf & Err.Description
        End If
    End Function

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 Filecopy and FSO.copyfile

    That still requires an admin user/pw (so it generally doesn't apply, as already stated), plus it is the wrong way to elevate a function since it bypasses the requirement to raise the consent dialog.

    Almost a textbook example of "how not to do things."

    See the guidelines at User Account Control.

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB6 Filecopy and FSO.copyfile

    Almost a textbook example of "how not to do things."
    maybe but i will be testing it, to see if it saves me having to sit at many computers (or remote) to login as admin, for some minor maintenance function
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  17. #17

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    4

    Re: VB6 Filecopy and FSO.copyfile

    Hi Guys

    Thank you for your contribution.

    In my scenario, how do I make sure/force that files get copied to virtualstore\windows directory and if so, will the OS automatically redirect the application to use the new file location?

    patelr

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB6 Filecopy and FSO.copyfile

    how do I make sure
    you can test using DIR
    vb Code:
    1. d = Dir(Environ("localappdata") & "\virtualstore\windows\vbaddin.ini")
    where vbaddin.ini was the file copied to

    will the OS automatically redirect the application to use the new file location?
    probably as a dir call to c:\windows will also succeed
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  19. #19
    Addicted Member
    Join Date
    Feb 2018
    Location
    Texas
    Posts
    168

    Re: VB6 Filecopy and FSO.copyfile

    Here is a post about a similar problem of fso.CopyFile vs the standard FileCopy

    https://www.vbforums.com/showthread....30#post5506430

  20. #20
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: VB6 Filecopy and FSO.copyfile

    clickman...this is TOO old of thread to post to...your reference in your post of 13 Jan 2021 is sufficient.
    Sam I am (as well as Confused at times).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width