Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How Do I map a drive from VB?
Message
From
06/08/2001 15:08:05
 
 
To
06/08/2001 14:42:46
GW Gross
{Banned by Information Security Policy}
Lake Mary, Florida, United States
General information
Forum:
Visual Basic
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00540320
Message ID:
00540333
Views:
26
This message has been marked as the solution to the initial question of the thread.
Here is something sleek and sassy for that. HTH. All you should have to do is define the value bolNTBox if its NT/2000 or 9x.
'--------------- MAP And UNMAP A Drive --------------------------------
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
    
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
'

Private Const NO_ERROR = 0
Private Const CONNECT_UPDATE_PROFILE = &H1
Private Const RESOURCETYPE_DISK = &H1
Private Const RESOURCETYPE_PRINT = &H2
Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCE_CONNECTED = &H1
Private Const RESOURCE_REMEMBERED = &H3
Private Const RESOURCE_GLOBALNET = &H2
Private Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Private Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Private Const RESOURCEDISPLAYTYPE_SERVER = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE = &H3
Private Const RESOURCEUSAGE_CONNECTABLE = &H1
Private Const RESOURCEUSAGE_CONTAINER = &H2
Private Const ShareLevel2 As Long = 2&
Private Const MAX_WSADescription = 256
Private Const MAX_WSASYSStatus = 128
Private Const MAX_LANA = 254
Private Const ERROR_SUCCESS       As Long = 0
Private Const WS_VERSION_REQD     As Long = &H101
Private Const WS_VERSION_MAJOR    As Long = WS_VERSION_REQD \ &H100 And &HFF&
Private Const WS_VERSION_MINOR    As Long = WS_VERSION_REQD And &HFF&
Private Const MIN_SOCKETS_REQD    As Long = 1
Private Const SOCKET_ERROR        As Long = -1
Private Const NCBASTAT = &H33
Private Const NCBNAMSZ = 16
Private Const HEAP_ZERO_MEMORY = &H8
Private Const HEAP_GENERATE_EXCEPTIONS = &H4
Private Const NCBRESET = &H32
'
Private Type NETRESOURCE
    dwScope         As Long
    dwType          As Long
    dwDisplayType   As Long
    dwUsage         As Long
    lpLocalName     As String
    lpRemoteName    As String
    lpComment       As String
    lpProvider      As String
End Type
'

Public Function Map(RemotePath As String, DriveLetter As String, UserName As String, Password As String) As Long

    If bolNTBox Then
        Map = ConnectDriveNT(RemotePath, DriveLetter, UserName, Password)
    Else
        Map = ConnectDrive9x(RemotePath, DriveLetter, UserName, Password)
    End If
    
End Function

Private Function ConnectDriveNT(RemotePath As String, DriveLetter As String, UserName As String, Password As String) As Long

    Dim NetR As NETRESOURCE
    Dim ErrInfo As Long
    
    NetR.dwScope = RESOURCE_GLOBALNET
    NetR.dwType = RESOURCETYPE_DISK
    NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
    NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
    NetR.lpLocalName = DriveLetter$
    NetR.lpRemoteName = RemotePath$
    
    ErrInfo = WNetAddConnection2(NetR, Password$, UserName$, CONNECT_UPDATE_PROFILE)
    
    If Not lReturnValue = NO_ERROR Then
        mvarErrMsg = "clsNetEng:ConnectDriveNT" & vbCrLf & LoadResString(lReturnValue)
        ConnectDriveNT = lReturnValue
    Else
        ConnectDriveNT = NO_ERROR
    End If
   
End Function
Private Function DisconnectDrive9x(DriveLetter As String)

    Dim lReturnValue As Long
    Dim strLocalName As String
    
    strLocalName = DriveLetter$
    lReturnValue = WNetCancelConnection2(strLocalName, CONNECT_UPDATE_PROFILE, False)
    
    If Not lReturnValue = NO_ERROR Then
        mvarErrMsg = "clsNetEng:DisconnectDrive9x" & vbCrLf & LoadResString(lReturnValue)
        DisconnectDrive9x = lReturnValue
    Else
        DisconnectDrive9x = NO_ERROR
    End If
    
End Function
Private Function DisconnectDriveNT(DriveLetter As String)

    Dim lReturnValue As Long
    Dim strLocalName As String
    
    strLocalName = DriveLetter$
    lReturnValue = WNetCancelConnection2(strLocalName, CONNECT_UPDATE_PROFILE, False)
    
    If Not lReturnValue = NO_ERROR Then
        mvarErrMsg = "clsNetEng:DisconnectDriveNT" & vbCrLf & LoadResString(lReturnValue)
        DisconnectDriveNT = lReturnValue
    Else
        DisconnectDriveNT = NO_ERROR
    End If

End Function


' ********************
>I have written a program that works well for my clients, but it depends upon a drive mapping (I:) to be there. Of course, I know my error trapping is not as good as it should be since this is the first time I have written a VB program that is actually being used by someone.
>
>So here are the questions:
>
>How do I know if the drive mapping has been lost?
>
>How do I reestablish the drive mapping if I know the UNC path name?
>
>Is there a good tutorial on how to deal with network connections and what errors I should trap for?
>
>Thanks,
~Joe Johnston USA

"If ye love wealth better than liberty, the tranquility of servitude better than the animated contest of freedom, go home from us in peace. We ask not your counsel or arms. Crouch down and lick the hands which feed you. May your chains set lightly upon you, and may posterity forget that ye were our countrymen."
~Samuel Adams

Previous
Next
Reply
Map
View

Click here to load this message in the networking platform