程序中自动连接或断开共享文件夹连接的参考示例:
**
** 连接共享文件夹()
** 参数: szLocalName,szRemoteName,szUserName,szPassWord,ddFlags
** szLocalName ... 影射到本地设备,如 X:,为空时不映射为盘符,空连接。
** szRemoteName .. 连接的远程设备,如 \\server\共享文件夹$
** szUserName .... 用户名
** szPassWord .... 密码
** ddFlags ....... 1连接,0断开
**
例:建立空连接
**
连接共享文件夹(",\\192.168.0.1\data$,admin,12349798,1")
**
FUNCTION 连接共享文件夹(sz连接参数)
LOCAL a连接参数[1]
IF ALINES(a连接参数, sz连接参数, ",") != 5
RETURN .F.
ENDIF
DECLARE LONG StrDup IN Shlwapi STRING@
DECLARE LONG WNetAddConnection2 IN Mpr.dll STRING@, STRING@, STRING@, LONG
DECLARE LONG WNetCancelConnection2 IN Mpr.dll STRING@, LONG, LONG
LOCAL szLocalName, szRemoteName, szUserName, szPassWord, ddFlags
LOCAL stNR, ddRet, szMsg
szLocalName
= a连接参数[1]
szRemoteName = a连接参数[2]
szUserName
= a连接参数[3]
szPassWord
= a连接参数[4]
ddFlags
= a连接参数[5]
IF ddFlags == "0"
ddRet = WNetCancelConnection2(@szRemoteName, 0, 1)
RETURN (ddRet == 0)
ENDIF
* NETRESOURCEA 结构
stNR = REPLICATE(0h00, 4);
+ BINTOC(1,
"4RS");
+ REPLICATE(0h00, 8);
+ BINTOC(StrDup(@szLocalName),
"4RS");
+ BINTOC(StrDup(@szRemoteName), "4RS");
+ REPLICATE(0h00, 8)
ddRet = WNetAddConnection2(@stNR, @szPassWord, @szUserName, 1)
IF ddRet == 1219
&& 已经有连接,断开再连接。
WNetCancelConnection2(@szRemoteName, 0, 1)
ddRet = WNetAddConnection2(@stNR, @szPassWord, @szUserName, 1)
ENDIF
IF ddRet != 0
szMsg = ICASE(ddRet == 5,
"找不到 "
+ szRemoteName + " 设备",;
ddRet == 85,
"本地设备 "
+ szLocalName
+ " 已经连接",;
ddRet == 66,
"本地设备 "
+ szLocalName
+ " 类型不匹配",;
ddRet == 1200, "指定的设备 " + szLocalName
+ " 名称无效",;
ddRet == 67,
"找不到 "
+ szRemoteName + " 网络名称",;
ddRet == 1206, "调用WNetAddConnection2()的配置文件格式不正确",;
ddRet == 1204, "网络设备 "
+ szRemoteName + " 名称无效",;
ddRet == 2202, "网络设备 "
+ szUserName
+ " 用户名无效",;
ddRet == 170,
"网络忙,请重试",;
ddRet == 1223, "连接被取消",;
ddRet == 1205, "调用WNetAddConnection2()无法打开用户配置文件",;
ddRet == 1202, "本地设备 "
+ szLocalName
+ " 与其他网络资源已经连接",;
ddRet == 1208, "发生网络错误,可用WNetGetLastError()函数获得错误信息。",;
ddRet == 487,
"资源结构的lpLocalName成员未指定 " + szLocalName,;
ddRet == 87,
"资源结构的dwtype成员参数不正确",;
ddRet == 86,
"用户名或密码无效",;
ddRet == 1326, "用户名或密码无效",;
ddRet == 1203, "网络设备 "
+ szRemoteName + " 不被接受",;
ddRet == 1222, "网络不可用", "登录连接异常")
MESSAGEBOX(szMsg, "提示")
ENDIF
RETURN (ddRet == 0)
ENDFUNC
[此贴子已经被作者于2017-2-24 19:36编辑过]