'=============================自定義常量========================================
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 Long
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" ( _
ByVal lpName As String, _
ByVal dwFlags As Long, _
ByVal fForce As Long _
) As Long
Public 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 Function
Public Function funIpcDisConn(ByVal strName As String) As Long
funIpcDisConn = WNetCancelConnection2(strName, 0, 0) '原樣返回API的返回值
End Function