Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Other Code Examples

Converting a mapped drive-path to a UNC path

Converting a mapped drive-path to a UNC path The source code below returns/converts a mapped path into a UNC path: Option Explicit Private Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long 'Purpose : Returns the UNC Path given a path 'Inputs : sPathName The path to convert 'Outputs : The UNC path of sPathName 'Notes : Requires NT/2000 'Revisions : Function ConvertToUNC(sPathName As String) As String Dim szValue As String, szValueName As String, sUNCName As String Dim lErrCode As Long, lEndBuffer As Long Const lLenUNC As Long = 520 'Return values for WNetGetConnection Const NO_ERROR As Long = 0 Const ERROR_NOT_CONNECTED As Long = 2250 Const ERROR_BAD_DEVICE = 1200& Const ERROR_MORE_DATA = 234 Const ERROR_CONNECTION_UNAVAIL = 1201& Const ERROR_NO_NETWORK = 1222& Const ERROR_EXTENDED_ERROR = 1208& Const ERROR_NO_NET_OR_BAD_PATH = 1203& 'Verify whether the disk is connected to the network If Mid$(sPathName, 2, 1) = ":" Then sUNCName = String$(lLenUNC, 0) lErrCode = WNetGetConnection(Left$(sPathName, 2), sUNCName, lLenUNC) lEndBuffer = InStr(sUNCName, vbNullChar) - 1 'Can ignore the errors below (will still return the correct UNC) If lEndBuffer > 0 And (lErrCode = NO_ERROR Or lErrCode = ERROR_CONNECTION_UNAVAIL Or lErrCode = ERROR_NOT_CONNECTED) Then 'Success sUNCName = Trim$(Left$(sUNCName, InStr(sUNCName, vbNullChar) - 1)) ConvertToUNC = sUNCName & Mid$(sPathName, 3) Else 'Error, return original path ConvertToUNC = sPathName End If Else 'Already a UNC Path ConvertToUNC = sPathName End If End Function