Results 1 to 10 of 10

Thread: Work with paths longer than MAX_PATH

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,987

    Work with paths longer than MAX_PATH

    Windows 10 allows to have paths > MAX_PATH (260 characters), but you'll have problems with VB and APIs.

    Here is a workaround:

    Code:
    Private Declare Function GetShortPathNameW Lib "kernel32" (ByVal lpszLongPath As Long, ByVal lpszShortPath As Long, ByVal cchBuffer As Long) As Long
    Code:
    Private Sub HandleLongPath(nPath As String)
        Const MAX_PATH = 260
        
        If Len(nPath) >= MAX_PATH Then
            Dim iRet As Long
            Dim iBuff As String
            Dim iPath As String
            Dim iShortened As Boolean
            
            If Left$(nPath, 4) <> "\\?\" Then
                iPath = "\\?\" & nPath
            Else
                iPath = nPath
            End If
            iRet = GetShortPathNameW(StrPtr(iPath), 0, 0)
            If iRet Then
                iBuff = Space$(iRet - 1)
                If GetShortPathNameW(StrPtr(iPath), StrPtr(iBuff), iRet) Then
                    If Left$(iBuff, 4) = "\\?\" Then iBuff = Mid(iBuff, 5)
                    nPath = iBuff
                    iShortened = True
                End If
            End If
            If Not iShortened Then
                If Left$(nPath, 4) <> "\\?\" Then
                    nPath = "\\?\" & nPath ' the 8.3 short paths system is disabled but prepending this can work for some APIs in Windows 10
                End If
            End If
        End If
    End Sub
    Usage:

    Code:
    HandleLongPath MyPath ' MyPath is a String variable that I'm about to use to do something with that file
    Prepending
    Code:
    "\\?\"
    to the path allows the GetShortPathNameW API to handle it and return a short path that the program can use.

    PS: I passed the argument ByRef to make it work faster.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,987

    Re: Work with paths longer than MAX_PATH

    Note:

    This will only solve the issue if 8.3 short file names are enabled on the system, that I guess it will be the most of the cases.
    If not, anyway the long path names would not work so you lose nothing.
    If someone else have a better solution please post it.

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,817

    Re: Work with paths longer than MAX_PATH

    I believe that most servers have this turned off. And I believe it's turned on for most desktop Windows installations.

    And sadly, if you're trying to use UNC paths, it's often servers where you run into the 260 limit.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,987

    Re: Work with paths longer than MAX_PATH

    Quote Originally Posted by Elroy View Post
    I believe that most servers have this turned off. And I believe it's turned on for most desktop Windows installations.

    And sadly, if you're trying to use UNC paths, it's often servers where you run into the 260 limit.
    Hello Elroy,

    I'm testing in my computer, Windows 11, and I found this issue with a program that I'm working on.
    For local files, as long as short paths are turned on, it works.

    But if there is a better and wider solution, of course I'm interested (and it will be useful to anyone else coming here).

    PS: I think the ultimate fix would be to move to 64 bits exes, but not possible with VB6.

  5. #5
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Work with paths longer than MAX_PATH


  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,817

    Re: Work with paths longer than MAX_PATH

    Quote Originally Posted by Magic Ink View Post
    Yes, exactly.

    I'm not sure there's a perfect answer for VB6. In rare cases, I've had to work with network administrators to map drive letters to long path names into their servers. I'm not crazy about that solution, as it requires the mapping to always be done correctly. But it is a solution for servers that don't have the 8.3 feature turned on.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,092

    Re: Work with paths longer than MAX_PATH

    Quote Originally Posted by Eduardo- View Post
    This will only solve the issue if 8.3 short file names are enabled on the system, that I guess it will be the most of the cases.
    Not default on recent server editions and recent Win10/11 for secondary drives. Here is how to check it out yourself.

    Not sure if brand new [Ex]FAT (i.e. USB) drives support short names nowadays too.

    SHCreateStreamOnFileEx API function supports LFN and works fine with files above 2GB so using IStream is my recent go to solution for file handling that does not depend on VB6 runtime or VBScript implementation.

    The state of affairs with LFN on Windows is very sad.

    cheers,
    </wqw>

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,987

    Re: Work with paths longer than MAX_PATH

    I made a modification to the OP.

    When the 8.3 file system is disabled, prepending

    Code:
    "\\?\"
    can allow some APIs to work in Windows 10+. So that is an alternative fix.

    PS: some interesting read about the topic here.

  9. #9
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    578

    Re: Work with paths longer than MAX_PATH

    Quote Originally Posted by wqweto View Post
    Not default on recent server editions and recent Win10/11 for secondary drives. Here is how to check it out yourself.

    Not sure if brand new [Ex]FAT (i.e. USB) drives support short names nowadays too.

    SHCreateStreamOnFileEx API function supports LFN and works fine with files above 2GB so using IStream is my recent go to solution for file handling that does not depend on VB6 runtime or VBScript implementation.

    The state of affairs with LFN on Windows is very sad.

    cheers,
    </wqw>
    I completely agree about how bad LFN's are. would you mind sharing a demo of SHCreateStreamOnFileEx.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,987

    Re: Work with paths longer than MAX_PATH

    Update, changed:

    Code:
    If Len(nPath) > MAX_PATH Then
    to

    Code:
    If Len(nPath) >= MAX_PATH Then
    Files with exact 260 characters also need to be handled specially.

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