-
Apr 5th, 2022, 01:58 PM
#1
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 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.
Last edited by Eduardo-; Jun 13th, 2022 at 05:14 PM.
-
Apr 5th, 2022, 02:18 PM
#2
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.
-
Apr 5th, 2022, 02:51 PM
#3
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.
-
Apr 5th, 2022, 03:01 PM
#4
Re: Work with paths longer than MAX_PATH
Originally Posted by Elroy
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.
-
Apr 6th, 2022, 04:27 AM
#5
Re: Work with paths longer than MAX_PATH
-
Apr 6th, 2022, 09:46 AM
#6
Re: Work with paths longer than MAX_PATH
Originally Posted by Magic Ink
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.
-
Apr 6th, 2022, 10:17 AM
#7
Re: Work with paths longer than MAX_PATH
Originally Posted by Eduardo-
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>
-
Apr 6th, 2022, 03:26 PM
#8
Re: Work with paths longer than MAX_PATH
I made a modification to the OP.
When the 8.3 file system is disabled, prepending
can allow some APIs to work in Windows 10+. So that is an alternative fix.
PS: some interesting read about the topic here.
Last edited by Eduardo-; Apr 6th, 2022 at 03:29 PM.
-
Apr 7th, 2022, 06:38 AM
#9
Re: Work with paths longer than MAX_PATH
Originally Posted by wqweto
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.
-
Jun 13th, 2022, 05:15 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|