VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



This class maps network drives to your PC by UNC name. If it exists then it returns the drive lette

by Mark S. (5 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 15th February 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

This class maps network drives to your PC by UNC name. If it exists then it returns the drive letter, otherwise it will use the next available

API Declarations


Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Long) As Long
Private Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long

Private Const MAX_VALUE As Integer = 255
Private Const WN_Access_Denied = &H7
Private Const WN_Already_Connected = &H34
Private Const WN_Bad_Localname = &H33
Private Const WN_Bad_NetName = &H32
Private Const WN_Bad_Password = &H6
Private Const WN_Bad_Pointer = &H4
Private Const WN_Net_Error = &H2
Private Const WN_Not_Supported = &H1
Private Const WN_Out_Of_Memory = &HB
Private Const WN_Success = &H0

Private mstrDriveName As String
Private mstrForceLetter As String
Private mstrUNCName As String

Property Let DriveName(ByVal strDriveName As String)

mstrDriveName = strDriveName

End Property

Property Get DriveName() As String

DriveName = mstrDriveName

End Property

Property Let ForceLetter(ByVal strForceLetter As String)

mstrForceLetter = strForceLetter

End Property

Property Get ForceLetter() As String

ForceLetter = mstrForceLetter

End Property

Property Let UNCName(ByVal strUNCName As String)

mstrUNCName = strUNCName

End Property

Property Get UNCName() As String

UNCName = mstrUNCName

End Property

Rate This class maps network drives to your PC by UNC name. If it exists then it returns the drive lette




    ' These are error descriptions generated by the WNet family of API calls from
    ' mpr.dll.
    Select Case lngErrorCode
    Case WN_Not_Supported:
        WnetError = "Function is not supported."
    Case WN_Out_Of_Memory:
        WnetError = "Out of Memory."
    Case WN_Net_Error:
        WnetError = "An error occurred on the network."
    Case WN_Bad_Pointer:
        WnetError = "The Pointer was Invalid."
    Case WN_Bad_NetName:
        WnetError = "Invalid Network Resource Name."
    Case WN_Bad_Password:
        WnetError = "The Password was Invalid."
    Case WN_Bad_Localname:
        WnetError = "The local device name was invalid."
    Case WN_Access_Denied:
        WnetError = "A security violation occurred."
    Case WN_Already_Connected:
        WnetError = "The local device was connected to a remote resource."
    Case Else:
        WnetError = "Unrecognized Error " & Str(lngErrorCode) & "."
    End Select

End Function

Public Function ConnectUNC(strUNCName As String, strForceLetter As String) As String

    Dim boolFound As Boolean
    Dim bytDrives As Byte
    Dim lngRetCode As Long
    Dim lngReturnedUNCName As Long
    Dim lngCheck As Long
    Dim strDrive As String
    Dim strReturnedUNCName As String

    On Error GoTo Process_Error
    
    If strUNCName > vbNullString Then
    
        mstrUNCName = strUNCName
        mstrForceLetter = strForceLetter
        
        If mstrForceLetter > vbNullString Then
            strDrive = Left$(mstrForceLetter, 1) & ":"
            lngRetCode = WNetAddConnection(mstrUNCName & Chr$(0), "" & Chr$(0), strDrive & Chr$(0))
            If lngRetCode = 0 Then
                ConnectUNC = Left$(strDrive, 1)
            Else
                ConnectUNC = Trim$(mstrUNCName) & " could not be mapped to " & Trim$(mstrForceLetter) & ":."
            End If
                     
        Else
            
            boolFound = False

            For bytDrives = 68 To 90
                strDrive = Chr$(bytDrives) & ":"
                strReturnedUNCName = Space$(MAX_VALUE)
                lngReturnedUNCName = Len(strReturnedUNCName)
                lngRetCode = WNetGetConnection(strDrive, strReturnedUNCName, lngReturnedUNCName)
                If lngRetCode = 0 Then
                    lngCheck = InStr(strReturnedUNCName, Chr$(0)) - 1
                    If LCase$(Left$(strReturnedUNCName, lngCheck)) = LCase$(mstrUNCName) Then
                        boolFound = True
                        Exit For
                    End If
                End If
            Next bytDrives
        
            ' Iterate letters D through Z.
            If boolFound Then
                ConnectUNC = Left$(strDrive, 1)
            Else
                For bytDrives = 68 To 90
                    strDrive = Chr$(bytDrives) & ":"
                    lngRetCode = WNetAddConnection(mstrUNCName & Chr$(0), "" & Chr$(0), strDrive & Chr$(0))
                    If lngRetCode = 0 Then
                        boolFound = True
                        Exit For
                    End If
                Next bytDrives
                         
                If boolFound Then
                    ConnectUNC = Left$(strDrive, 1)
                Else
                    ConnectUNC = Trim$(mstrUNCName) & " could not be mapped."
                End If
            End If
        End If
    
    Else
        ConnectUNC = WnetError(WN_Bad_Localname)
    End If
    
    GoTo Process_Continue

Process_Error:

    ConnectUNC = WnetError(lngRetCode)
    
Process_Continue:

End Function

Public Function DisconnectUNC(strDriveName As String) As String

    Dim lngRetCode As Long
    
    On Error GoTo Process_Error
    
    If strDriveName > vbNullString Then
    
        mstrDriveName = Left$(strDriveName, 1) & ":"
    
        lngRetCode = WNetCancelConnection(mstrDriveName & Chr$(0), 0)
        DisconnectUNC = vbNullString
        
    Else
        DisconnectUNC = WnetError(WN_Bad_Localname)
    End If

    GoTo Process_Continue

Process_Error:
    
    DisconnectUNC = WnetError(lngRetCode)
    
Process_Continue:

End Function

Download this snippet    Add to My Saved Code

This class maps network drives to your PC by UNC name. If it exists then it returns the drive lette Comments

No comments have been posted about This class maps network drives to your PC by UNC name. If it exists then it returns the drive lette. Why not be the first to post a comment about This class maps network drives to your PC by UNC name. If it exists then it returns the drive lette.

Post your comment

Subject:
Message:
0/1000 characters