Private Const RESOURCETYPE_DISK = &H1 Private Const RESOURCE_GLOBALNET As Long = &H2 Private Const RESOURCEDISPLAYTYPE_SHARE = &H3 Private Const RESOURCEUSAGE_CONNECTABLE = &H1'=============================自定义结构======================================== 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'=================================API声明======================================= 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 LongPrivate Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" ( _ ByVal lpName As String, _ ByVal dwFlags As Long, _ ByVal fForce As Long _ ) As LongPublic Function funIpcConn(ByVal strRemoteName As String, ByVal strUserName As String, ByVal strPassword As String) As Boolean '目的:建立网络间的联系 '返回: True ---- 成功 ' False --- 失败 '注释:strRemoteName为要连接的计算机名称(如:\\HostName、\\192.168.199.144、\\HostName\Data) ' strUserName为登录的用户名,strPassword为登录的密码 Dim NetR As NETRESOURCE NetR.dwScope = RESOURCE_GLOBALNET NetR.dwType = RESOURCETYPE_DISK NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE NetR.lpRemoteName = strRemoteName '建立网络映射 funIpcConn = IIf(WNetAddConnection2(NetR, strPassword, strUserName, 0) = 0, True, False) End FunctionPublic Function funIpcDisConn(ByVal strName As String) As Long funIpcDisConn = WNetCancelConnection2(strName, 0, 0) '原样返回API的返回值 End Function Dim aa As Boolean aa = funIpcConn("文件路径", "用户名", "密码") if aa=true then msgbox("连接成功") else msgbox("连接失败") end if
Private Const RESOURCE_GLOBALNET As Long = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE = &H3
Private Const RESOURCEUSAGE_CONNECTABLE = &H1'=============================自定义结构========================================
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'=================================API声明=======================================
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 LongPrivate Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" ( _
ByVal lpName As String, _
ByVal dwFlags As Long, _
ByVal fForce As Long _
) As LongPublic Function funIpcConn(ByVal strRemoteName As String, ByVal strUserName As String, ByVal strPassword As String) As Boolean
'目的:建立网络间的联系
'返回: True ---- 成功
' False --- 失败
'注释:strRemoteName为要连接的计算机名称(如:\\HostName、\\192.168.199.144、\\HostName\Data)
' strUserName为登录的用户名,strPassword为登录的密码
Dim NetR As NETRESOURCE
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpRemoteName = strRemoteName
'建立网络映射
funIpcConn = IIf(WNetAddConnection2(NetR, strPassword, strUserName, 0) = 0, True, False)
End FunctionPublic Function funIpcDisConn(ByVal strName As String) As Long
funIpcDisConn = WNetCancelConnection2(strName, 0, 0) '原样返回API的返回值
End Function
Dim aa As Boolean
aa = funIpcConn("文件路径", "用户名", "密码")
if aa=true then
msgbox("连接成功")
else
msgbox("连接失败")
end if