网页功能: 加入收藏 设为首页 网站搜索  
定/取消“网络磁盘”
发表日期:2002-12-08作者:[] 出处:  

使用方法:

建立连线

 Function AddConnection(ByVal RemoteLocation As String, _

             ByVal LocalDriver As String, _

             ByVal Passwd As String, _

             ByVal UserName As String) As Boolean

  RemoteLocation : 为网络磁盘原始来源,格式为

           \\RemoteComputer\ShareDirectory

  LocalDriver   : 对应本机的磁盘代号,如: "H:"

  Passwd     : 存取网络磁盘的Password,传Null表示不用密码

  UserName    : 存取网络磁盘的使用者代号

  成功时传回True,否则为False

例:

  Call AddConnection("\\Shih\cmias", "x:", vbNullString, "cww")

取消连线

Function CancelConnection(ByVal LocalDriver As String, _

              ByVal ForceClose As Boolean) As Boolean

  LocalDriver  : 对应本机的磁盘代号,如: "H:"

  ForceClose  : True表示强迫结束连线,而不管有没有程式正与之连线

  成功时传回True,否则为False

例:

 Call CancelConnection("x:", True)

以下在.bas

Option Explicit

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

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

Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long

Declare Function WNetGetLastError Lib "mpr.dll" Alias "WNetGetLastErrorA" (lpError As Long, ByVal lpErrorBuf As String, ByVal nErrorBufSize As Long, ByVal lpNameBuf As String, ByVal nNameBufSize As Long) As Long

Public Const RESOURCE_PUBLICNET = &H2

Public Const RESOURCETYPE_ANY = &H0

Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0

Public Const RESOURCEUSAGE_CONNECTABLE = &H1

Public Const CONNECT_UPDATE_PROFILE = &H1

Public Function AddConnection(ByVal RemoteLocation As String, ByVal LocalDriver As String, _

               ByVal Passwd As String, ByVal UserName As String) As Boolean

Dim ne As NETRESOURCE, i As Long

Dim errstr As String, errpriv As String, erno As Long

ne.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC

ne.dwScope = RESOURCE_PUBLICNET

ne.dwType = RESOURCETYPE_ANY

ne.dwUsage = RESOURCEUSAGE_CONNECTABLE

ne.lpComment = vbNullString

ne.lpLocalName = LocalDriver

ne.lpProvider = vbNullString

ne.lpRemoteName = RemoteLocation

i = WNetAddConnection2(ne, Passwd, UserName, 0)

If i = 0 Then

  AddConnection = True

Else

 AddConnection = False

 errstr = String(256, 0)

 errpriv = String(256, 0)

 i = WNetGetLastError(erno, errstr, 256, errpriv, 256)

 errstr = Left(errstr, InStr(1, errstr, Chr(0)) - 1)

 MsgBox errstr, vbCritical

End If

End Function

Public Function CancelConnection(ByVal LocalDriver As String, ByVal ForceClose As Boolean) As Boolean

Dim i As Long

Dim errstr As String, errpriv As String, erno As Long

i = WNetCancelConnection2(LocalDriver, 0, IIf(ForceClose, 1, 0))

If i = 0 Then

  CancelConnection = True

Else

 CancelConnection = False

 errstr = String(256, 0)

 errpriv = String(256, 0)

 i = WNetGetLastError(erno, errstr, 256, errpriv, 256)

 errstr = Left(errstr, InStr(1, errstr, Chr(0)) - 1)

 MsgBox errstr, vbCritical

End If

End Function

我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 定/取消“网络磁盘”
本类热点文章
  用MSComm控件实现来电显示
  WOL(Wake on Lan)程序的源代码
  用VB调试串口通讯
  用PocketPCPhone替代AT指令的手机SMS服务
  保存页面为MHT的代码
  用VB动态获取所有网络邻居的名称及其IP..
  于Winsock控件制作的一个“服务器,N客..
  VB实现远程共享显示及声音
  Windows下访问共享的解决方案
  VB实现的MSN信息群发软件
  如何检测是否已连接到Internet?
  VB学习:MX记录获取组件
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2019 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00602