Get full path with Unicode file name

I have a path in the short version or in DOS format ( "C: / DOCUME ~ 1" , for example) and you want to get the full path / long path ( "C: / Documents and Settings" , for example).

I tried the GetLongPathName api. It worked. But when working with a unicode file, it fails.

Private Declare Function GetLongPathName Lib "kernel32" Alias _
    "GetLongPathNameA" (ByVal lpszShortPath As String, _
    ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long

I tried to use the alias GetLongPathNameW instead, but it seems to do nothing, for BOTH Unicode and non-Unicode the file name always returns 0. There is only an article on MSDN about GetLongPathNameW for C / C ++ and not for VB / VBA. Can I do something wrong?

Is there any solution for this case? I spend hours on Google and StackOverflow, but I can’t find out.

Hi,

+5
source share
2 answers

Does this work for you? I converted the path to the shortcut path and then converted it back, which gives the correct line even if unicode (e.g. C: / Tö +)

Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _
    (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Private Declare Function GetLongPathName Lib "kernel32" Alias "GetLongPathNameA" _
    (ByVal lpszShortPath As String, ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long

Public Function GetShortPath(ByVal strFileName As String) As String
    'KPD-Team 1999
    'URL: [url]http://www.allapi.net/[/url]
    'E-Mail: [email]KPDTeam@Allapi.net[/email]
    Dim lngRes As Long, strPath As String
    'Create a buffer
    strPath = String$(165, 0)
    'retrieve the short pathname
    lngRes = GetShortPathName(strFileName, strPath, 164)
    'remove all unnecessary chr$(0)'s
    GetShortPath = Left$(strPath, lngRes)
End Function

Public Function GetLongPath(ByVal strFileName As String) As String
    Dim lngRes As Long, strPath As String
    'Create a buffer
    strPath = String$(165, 0)
    'retrieve the long pathname
    lngRes = GetLongPathName(strFileName, strPath, 164)
    'remove all unnecessary chr$(0)'s
    GetLongPath = Left$(strPath, lngRes)
End Function

Private Sub Test()
    shortpath = GetShortPath("C:/Documents And Settings")

    Longpath = GetLongPath(shortpath)
End Sub
+4
source

To use W functions from vb6 / vba, you declare all string parameters:

Private Declare Function GetLongPathName Lib "kernel32" Alias "GetLongPathNameW" _
  (ByVal lpszShortPath As Long, _
   ByVal lpszLongPath As Long, _
   ByVal cchBuffer As Long) As Long

and pass StrPtr(a_string)instead a_string.

So, if you have:

dim s_path as string
dim l_path as string

s_path = "C:\DOCUME~1"
l_path = string$(1024, vbnullchar)

GetLongPathNameA s_path, l_path, len(l_path)

he would become

dim s_path as string
dim l_path as string

s_path = "C:\DOCUME~1"
l_path = string$(1024, vbnullchar)

GetLongPathNameW strptr(s_path), strptr(l_path), len(l_path)
+1
source

All Articles