Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

Work with paths longer than MAX_PATH

$
0
0
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:

Public Sub ShortenPath(nPath As String)
    Const MAX_PATH = 260
    Dim iRet As Long
    Dim iBuff As String
   
    If Len(nPath) > MAX_PATH Then
        iRet = GetShortPathNameW(StrPtr("\\?\" & nPath), 0, 0)
        If iRet Then
            iBuff = Space$(iRet - 1)
            If GetShortPathNameW(StrPtr("\\?\" & nPath), StrPtr(iBuff), iRet) Then
                If Left$(iBuff, 4) = "\\?\" Then iBuff = Mid(iBuff, 5)
                nPath = iBuff
            End If
        End If
    End If
End Sub

Prepending "\\?" 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.

Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>