WNetAddConnection2Aを使用します。
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 Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
pLocalName As String
pRemoteName As String
pComment As Long
pProvider As Long
End Type
Private Const RESOURCE_CONNECTED = &H1
Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Private Const CONNECT_UPDATE_PROFILE = &H1
Private Sub Command1_Click()
Dim typNetResource As NETRESOURCE
Dim lngRet As Long
Dim pass As String
Dim user As String
pass = vbNullString
user = vbNullString
REM Call Command2_Click
With typNetResource
.dwScope = RESOURCE_CONNECTED
.dwType = RESOURCETYPE_ANY
.dwDisplayType = RESOURCEDISPLAYTYPE_DOMAIN
.pLocalName = "Z:" '空いている自ドライブ
.pRemoteName = CStr(Text3.Text)
End With
lngRet = WNetAddConnection2(typNetResource, pass, user, CONNECT_UPDATE_PROFILE)
If lngRet = 0 Then
MsgBox "接続できました。"
Else
MsgBox "接続できませんでした。"
End If
End Sub
Private Sub Command2_Click()
Dim lngRet As Long
lngRet = WNetCancelConnection2("Z:", CONNECT_UPDATE_PROFILE, True)
If lngRet = 0 Then
MsgBox "切断できました。"
Else
MsgBox "切断できませんでした。"
End If
End Sub