Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCE_CONNECTED = &H1
Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As Long 'Returns MemLocation
lpRemoteName As Long
lpComment As Long
lpProvider As Long
End Type
Private Declare Function GetDriveType Lib "KERNEL32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) As Long
Private Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, lpBuffer As Any, lpBufferSize As Long) As Long
Private Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Long) As Long
Private Declare Function lstrlen Lib "KERNEL32" Alias "lstrlenA" (ByVal lpString As Any) As Long
Private Declare Function lstrcpy Lib "KERNEL32" Alias "lstrcpyA" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Function LetterToUNC(DriveLetter As String) As String
Dim hEnum As Long
Dim NetInfo(1023) As NETRESOURCE
Dim entries As Long
Dim nStatus As Long
Dim LocalName As String
Dim UNCName As String
Dim I As Long
Dim r As Long
' Begin the enumeration
nStatus = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, ByVal 0&, ByVal 0&, hEnum)
LetterToUNC = DriveLetter
'Check for success from open enum
If ((nStatus = 0) And (hEnum <> 0)) Then
' Set number of entries
entries = 1024
' Enumerate the resource
nStatus = WNetEnumResource(hEnum, entries, NetInfo(0), CLng(Len(NetInfo(0))) * 1024)
' Check for success
If nStatus = 0 Then
For I = 0 To entries - 1
' Get the local name
LocalName = ""
If NetInfo(I).lpLocalName <> 0 Then
LocalName = Space(lstrlen(NetInfo(I).lpLocalName) + 1)
r = lstrcpy(LocalName, NetInfo(I).lpLocalName)
End If
' Strip null character from end
If Len(LocalName) <> 0 Then
LocalName = Left(LocalName, (Len(LocalName) - 1))
End If
If UCase$(LocalName) = UCase$(DriveLetter) Then
' Get the remote name
UNCName = ""
If NetInfo(I).lpRemoteName <> 0 Then
UNCName = Space(lstrlen(NetInfo(I).lpRemoteName) + 1)
r = lstrcpy(UNCName, NetInfo(I).lpRemoteName)
End If
' Strip null character from end
If Len(UNCName) <> 0 Then
UNCName = Left(UNCName, (Len(UNCName) - 1))
End If
' Return the UNC path to drive
'added the [] to seperate on printout only
LetterToUNC = UNCName
' Exit the loop
Exit For
End If
Next I
End If
End If
' End enumeration
nStatus = WNetCloseEnum(hEnum)
End Function
Private Sub Form_Load()
'Example Usage
'MsgBox GET_SHARE_FROM_FILENAME("Q:\00000temp\sybase.pdf")
End Sub
Private Function GET_SHARE_FROM_FILENAME(ByVal F_Name As String) As String
'Pass Example for just Drive: Q:\
F_Name = Trim(F_Name)
GET_SHARE_FROM_FILENAME = F_Name
If Mid(F_Name, 2, 2) = ":\" Then
If GetDriveType(Mid(F_Name, 1, 3)) = 4 Then
GET_SHARE_FROM_FILENAME = LetterToUNC(Mid(F_Name, 1, 2)) & Mid(F_Name, 3)
End If
End If
End Function