Results 1 to 7 of 7

Thread: [RESOLVED] Relative path vs Absolute path

  1. #1

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    390

    Resolved [RESOLVED] Relative path vs Absolute path

    I created a small app that uses somewhere in the code a relative path from a server that is mapped in my machine. It worked properly for weeks in this way but starting with this weekend that relative path has become unresponsive although nothing has changed in the mapping structure. Trying to better understand what happened, I wrote a few rows around the vb function GetAttr testing both relative and absolute path. The result is that while the absolute path works without issue the other generates the error 76 "Path not found". This validation was run in the IDE mode but when the snippet was compiled I was surprised to find that both work. I just want to mention that the same scenario using FSO library gives the same results.. Any idea about this bug?
    Code:
    If GetAttr(myPath) And vbDirectory Then
       MsgBox "Valid Directory"
    End If
    "VB code is practically pseudocode" - Tanner Helland
    "When you do things right, people won't be sure you've done anything at all" - Matt Groening

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,733

    Re: Relative path vs Absolute path

    Normally you would always run the IDE with elevation.

    Under split-token operation mode (UAC not disabled) a member of the Administrators group has two security profiles. Each of these has its own separate things, for example drive mappings.

    So you probably have mapped a remote share to a drive letter in the standard-user profile but not the elevated profile.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,733

    Re: Relative path vs Absolute path

    Of course that doesn't explain anything related to relative paths.

    Are you misusing terminology? Perhaps you mean UNC paths vs. mapped paths?

  4. #4
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,215

    Re: Relative path vs Absolute path

    Relative paths are based off process current drive and current dierctory i.e. the one that are set by ChDrive and ChDir built-in functions.

    What is the current drive/path when IDE starts? If you start VB6.exe by double clicking it -- it's the path the .exe is located. If you start it with a shortcut -- it's written in the shrtcut's "Start In" setting.

    The same goes for you final executable -- it's process current drive/path depends on the shortcut it's started from. You cannot depend on initial current path being exactly App.Path because App.Path is the path to the executable, always.

    Things get murky if you use open/save file dialogs as these *do* change current drive/path when the user navigate the file system.

    Here is how to get absolute (or canonical) path from a relative path

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Debug.Print CanonicalPath("..\test\aaa.txt")
    End Sub
    
    Public Function CanonicalPath(sPath As String) As String
        On Error GoTo EH
        CanonicalPath = sPath
        With CreateObject("Scripting.FileSystemObject")
            CanonicalPath = .GetAbsolutePathName(sPath)
        End With
    EH:
    End Function
    FYI, canonical path is one where all .. and . back-references are resolved.

    cheers,
    </wqw>

  5. #5

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    390

    Re: Relative path vs Absolute path

    Thank you for your feedback. Yes, maybe the right or more appropriate terminology would be UNC paths vs. mapped paths as dile mentioned above - even the Authoritative Dictionary of IEEE Standards Terms (2018 edition) defines the Relative Pathname as "A pathname not beginning with a <slash> character." Anyway, the idea is cannot get the absolute path ("A pathname beginning with a single or more than two <slash> characters") even I use the FSO library as in the wqweto's example. Perhaps there are some particularities related to the OS because at the office we use an Windows Server 2016 as a virtual machine.
    "VB code is practically pseudocode" - Tanner Helland
    "When you do things right, people won't be sure you've done anything at all" - Matt Groening

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,215

    Re: Relative path vs Absolute path

    No, this is not what relative vs absolute path mean -- this is mapped drive vs UNC path.

    Try this API call

    Code:
    Option Explicit
    
    Private Declare Function WNetGetConnection Lib "mpr" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
    
    Private Sub Form_Load()
        Debug.Print GetUncFromMappedDrive("P:")
    End Sub
    
    Public Function GetUncFromMappedDrive(sLocalName As String) As String
        GetUncFromMappedDrive = String$(1000, 0)
        Call WNetGetConnection(Left$(sLocalName, 2), GetUncFromMappedDrive, Len(GetUncFromMappedDrive) - 1)
        GetUncFromMappedDrive = Left$(GetUncFromMappedDrive, InStr(GetUncFromMappedDrive, vbNullChar) - 1)
    End Function
    cheers,
    </wqw>

  7. #7

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    390

    Re: Relative path vs Absolute path

    Quote Originally Posted by wqweto View Post
    No, this is not what relative vs absolute path mean -- this is mapped drive vs UNC path.

    Try this API call

    Code:
    Option Explicit
    
    Private Declare Function WNetGetConnection Lib "mpr" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
    
    Private Sub Form_Load()
        Debug.Print GetUncFromMappedDrive("P:")
    End Sub
    
    Public Function GetUncFromMappedDrive(sLocalName As String) As String
        GetUncFromMappedDrive = String$(1000, 0)
        Call WNetGetConnection(Left$(sLocalName, 2), GetUncFromMappedDrive, Len(GetUncFromMappedDrive) - 1)
        GetUncFromMappedDrive = Left$(GetUncFromMappedDrive, InStr(GetUncFromMappedDrive, vbNullChar) - 1)
    End Function
    cheers,
    </wqw>
    In this way, finally it worked ! Interesting, the function does return the first sequence that is missing from the mapped drive path. If I add a row in the function above I get exactly what I needed.
    Code:
    Public Function GetUncFromMappedDrive(sLocalName As String) As String
        GetUncFromMappedDrive = String$(1000, 0)
        Call WNetGetConnection(Left$(sLocalName, 2), GetUncFromMappedDrive, Len(GetUncFromMappedDrive) - 1)
        GetUncFromMappedDrive = Left$(GetUncFromMappedDrive, InStr(GetUncFromMappedDrive, vbNullChar) - 1)
        GetUncFromMappedDrive = GetUncFromMappedDrive & Mid$(sLocalName, 3)
    End Function
    Thank you very much, wqweto.
    "VB code is practically pseudocode" - Tanner Helland
    "When you do things right, people won't be sure you've done anything at all" - Matt Groening

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