Appologies to word wrapping.
There is more code than the bare minimum to get the class to work. I
put an error routine to get the english meaning of the returned error
codes. Also, all of the constants defined by the Microsoft MSDN2 pages
are defined, although I only needed a couple of them.
Enjoy
Imports System.Runtime.InteropServices
Public Class clsConnectDrive
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias _
"WNetAddConnection2A" (ByVal netResource As NETRESOURCE, _
ByVal password As [String], ByVal Username As [String], _
ByVal Flag As Integer) As Integer
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias
_
"WNetCancelConnection2A" (ByVal lpName As String, _
ByVal Flag As Integer, ByVal fForce As Integer) As Integer
Private m_strDrive As String
Private m_strNetworkShare As String
Private m_intError As Integer
<StructLayout(LayoutKind.Sequential)> _
Class NETRESOURCE
Public dwScope As Integer
Public dwType As Integer
Public dwDisplayType As Integer
Public dwUsage As Integer
Public LocalName As String
Public RemoteName As String
Public Comment As String
Public Provider As String
End Class
'---------- Network Constants ----------
Private Const NO_ERROR As Integer = 0
Private Const CONNECT_UPDATE_PROFILE As Integer = &H1 '
Disconnect
Private Const RESOURCETYPE_ANY As Integer = &H0
Private Const RESOURCETYPE_DISK As Integer = &H1
Private Const RESOURCETYPE_PRINT As Integer = &H2
Private Const RESOURCE_CONNECTED As Integer = &H1
Private Const RESOURCE_GLOBALNET As Integer = &H2
Private Const RESOURCE_REMEMBERED As Integer = &H3
Private Const RESOURCEDISPLAYTYPE_GENERIC As Integer = &H0
Private Const RESOURCEDISPLAYTYPE_DOMAIN As Integer = &H1
Private Const RESOURCEDISPLAYTYPE_SERVER As Integer = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE As Integer = &H3
Private Const RESOURCEUSAGE_CONNECTABLE As Integer = &H1
Private Const RESOURCEUSAGE_CONTAINER As Integer = &H2
' Error Constants:
Private Const ERROR_ACCESS_DENIED As Integer = 5&
Private Const ERROR_ALREADY_ASSIGNED As Integer = 85&
Private Const ERROR_BAD_DEV_TYPE As Integer = 66&
Private Const ERROR_BAD_DEVICE As Integer = 1200&
Private Const ERROR_BAD_NET_NAME As Integer = 67&
Private Const ERROR_BAD_PROFILE As Integer = 1206&
Private Const ERROR_BAD_PROVIDER As Integer = 1204&
Private Const ERROR_BUSY As Integer = 170&
Private Const ERROR_CANCELLED As Integer = 1223&
Private Const ERROR_CANNOT_OPEN_PROFILE As Integer = 1205&
Private Const ERROR_DEVICE_ALREADY_REMEMBERED As Integer = 1202&
Private Const ERROR_EXTENDED_ERROR As Integer = 1208&
Private Const ERROR_INVALID_PASSWORD As Integer = 86&
Private Const ERROR_NO_NET_OR_BAD_PATH As Integer = 1203&
'---------- Start Code ----------
Public Sub New(ByVal Drive As String, ByVal NetworkShare As
String)
m_strDrive = Drive
If m_strDrive.Length > 1 Then m_strDrive =
m_strDrive.Substring(0, 1)
m_strDrive += ":"
m_strNetworkShare = NetworkShare
End Sub
'---------- Connect a drive ----------
Public Function DriveConnect(Optional ByVal IgnoreAlreadyConnected
As Boolean = True) As Integer
Dim myNetResource As New NETRESOURCE
myNetResource.dwScope = RESOURCE_GLOBALNET
myNetResource.dwType = RESOURCETYPE_DISK
myNetResource.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
myNetResource.dwUsage = RESOURCEUSAGE_CONNECTABLE
myNetResource.LocalName = m_strDrive
myNetResource.RemoteName = m_strNetworkShare
myNetResource.Provider = Nothing
m_intError = WNetAddConnection2(myNetResource, Nothing,
Nothing, 0)
If m_intError = ERROR_ALREADY_ASSIGNED AndAlso
IgnoreAlreadyConnected Then m_intError = NO_ERROR
Return m_intError
End Function
'---------- Disconnect a drive ----------
Public Function DriveDisconnect() As Integer
m_intError = WNetCancelConnection2(m_strDrive,
CONNECT_UPDATE_PROFILE, 0)
Return m_intError
End Function
'---------- Error Code Description ----------
' Use the last error from either the connect or disconnect
Public Function ErrorCodeMeaning() As String
Dim m_strMessage As String = "*** Undefined ***"
Select Case m_intError
Case NO_ERROR : m_strMessage = ""
Case ERROR_ACCESS_DENIED : m_strMessage = "Access Denied"
Case ERROR_ALREADY_ASSIGNED : m_strMessage = "Already
Assigned"
Case ERROR_BAD_DEV_TYPE : m_strMessage = "Bad Device Type"
Case ERROR_BAD_DEVICE : m_strMessage = "Bad Device"
Case ERROR_BAD_NET_NAME : m_strMessage = "Bad Net Name"
Case ERROR_BAD_PROFILE : m_strMessage = "Bad Profile"
Case ERROR_BAD_PROVIDER : m_strMessage = "Bad Provider"
Case ERROR_BUSY : m_strMessage = "Busy"
Case ERROR_CANCELLED : m_strMessage = "Cancelled"
Case ERROR_CANNOT_OPEN_PROFILE : m_strMessage = "Can Not
Open Profile"
Case ERROR_DEVICE_ALREADY_REMEMBERED : m_strMessage =
"Device Already Remembered"
Case ERROR_EXTENDED_ERROR : m_strMessage = "Extended
Error"
Case ERROR_INVALID_PASSWORD : m_strMessage = "Invalid
Password"
Case ERROR_NO_NET_OR_BAD_PATH : m_strMessage = "No Network
or Bad Path"
Case 53 : m_strMessage = "Can Not Connect - Check Samba
Log"
Case Else : m_strMessage = "*** Undefined ***"
End Select
Return m_strMessage
End Function
End Class