Option Explicit
Private Sub Command1_Click()
Dim NetR As NETRESOURCE
Dim ErrInfo As Long
Dim MyPass As String, MyUser As String
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = "X:" ' If undefined, Connect with no device
NetR.lpRemoteName = "\\ServerName\ShareName" ' Your valid share
'NetR.lpComment = "Optional Comment"
'NetR.lpProvider = ' Leave this undefined
' If the MyPass and MyUser arguments are null (use vbNullString), the
' user context for the process provides the default user name.
ErrInfo = WNetAddConnection2(NetR, MyPass, MyUser, _
CONNECT_UPDATE_PROFILE)
If ErrInfo = NO_ERROR Then
MsgBox "Net Connection Successful!", vbInformation, _
"Share Connected"
Else
MsgBox "ERROR: " & ErrInfo & " - Net Connection Failed!", _
vbExclamation, "Share not Connected"
End If
End Sub
Private Sub Command2_Click()
Dim ErrInfo As Long
Dim strLocalName As String
' You may specify either the lpRemoteName or lpLocalName
'strLocalName = "\\ServerName\ShareName"
strLocalName = "X:"
ErrInfo = WNetCancelConnection2(strLocalName, _
CONNECT_UPDATE_PROFILE, False)
If ErrInfo = NO_ERROR Then
MsgBox "Net Disconnection Successful!", vbInformation, _
"Share Disconnected"
Else
MsgBox "ERROR: " & ErrInfo & " - Net Disconnection Failed!", _
vbExclamation, "Share not Disconnected"
End If
End Sub