Option Explicit
Private Declare Function OSGetLongPathName Lib "VB5STKIT.DLL" Alias "GetLongPathName" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Public Declare Function OSGetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Function GetLongPathName(ByVal strShortPath As String) As String
Const cchBuffer = 300
Dim strLongPath As String
Dim lResult As Long
On Error GoTo 0
strLongPath = String(cchBuffer, Chr$(0))
lResult = OSGetLongPathName(strShortPath, strLongPath, cchBuffer)
If lResult = 0 Then
GetShortPathName = ""
Else
GetLongPathName = StripTerminator(strLongPath)
End If
End Function
Public Function GetShortPathName(ByVal strLongPath As String) As String
Const cchBuffer = 300
Dim strShortPath As String
Dim lResult As Long
On Error GoTo 0
strShortPath = String(cchBuffer, Chr$(0))
lResult = OSGetShortPathName(strLongPath, strShortPath, cchBuffer)
If lResult = 0 Then
GetShortPathName = ""
Else
GetShortPathName = StripTerminator(strShortPath)
End If
End Function
'-----------------------------------------------------------
' 函数: StripTerminator
'
' 返回非零结尾的字符串。典型地,这是一个由 Windows API 调用返回的字符串。
'
' 入口: [strString] - 要删除结束符的字符串
'
' 返回: 传递的字符串减去尾部零以后的值。
'-----------------------------------------------------------
'
Private Function StripTerminator(ByVal strString As String) As String
Dim intZeroPos As Integer
intZeroPos = InStr(strString, Chr$(0))
If intZeroPos > 0 Then
StripTerminator = Left$(strString, intZeroPos - 1)
Else
StripTerminator = strString
End If
End Function