当不同用户失败时卸载并立即重新安装 SMB 共享
我正在开发一个在 Windows XP SP3 上运行的 MFC 应用程序。要求之一是,当用户登录应用程序时,应用程序必须使用该用户的凭据挂载托管在嵌入式 Linux 服务器上的多个 SMB 共享。
该程序具有“切换用户”功能,允许不同的用户登录而无需关闭应用程序,从而终止第一个用户的会话。发生这种情况时,必须卸载 SMB 共享,然后以新用户身份重新安装。
我们通过调用 WNetCancelConnection2
紧接着是 WNetAddConnection2
< /a>. WNetCancelConnection2
返回NO_ERROR
,表明连接成功。 但是,有时 WNetAddConnection2
无法以新用户身份重新安装驱动器。使用 FormatMessage
从返回值中获取错误字符串会导致此结果错误消息,尽管 WNetCancelConnection2
似乎成功了:
<块引用>不允许同一用户使用多个用户名多次连接到服务器或共享资源。断开之前与服务器或共享资源的所有连接,然后重试。
从命令提示符运行 net use
也不会显示与 SMB 共享的打开连接。
如果用户从不与安装的驱动器交互,则不会发生此故障。仅当用户以某种方式访问已安装的驱动器时才会发生这种情况,即用户在 Windows 资源管理器中打开已安装的驱动器。同样,如果您完全退出应用程序然后重新启动它并以身份登录,则不会发生这种情况不同的用户。
所有这些让我相信存在一种竞争条件,即在关闭网络资源的所有打开句柄之前 WNetCancelConnection2
返回。我的问题是我可以让我的程序在调用WNetCancelConnection2
之后阻塞或等待,直到所有这些句柄都关闭,从而保证WNetAddConnection2
不会以这种方式失败吗?< /strong> 阻塞 API 或忙等待连接完全关闭的方式都是可以接受的。两者都比 sleep()
更好,并希望得到最好的结果。 :-)
I am working on an MFC application that runs on Windows XP SP3. One of the requirements is that when a user logs into the application, the application must mount several SMB shares hosted on an embedded Linux server using that user's credentials.
The program has a "switch user" feature, which allows a different user to log in without closing the application, terminating the first user's session. When that happens, the SMB shares must be unmounted and then remounted as the new user.
We do this by calling WNetCancelConnection2
followed immediately by WNetAddConnection2
. WNetCancelConnection2
returns NO_ERROR
, indicating it was successful. However, sometimes WNetAddConnection2
will fail to remount the drive as the new user. Using FormatMessage
to obtain an error string from the return value results in this error message, despite the fact that WNetCancelConnection2
seemed to succeed:
Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again.
Running net use
from the command prompt does not show an open connection to the SMB share either.
This failure will not occur if the user never interacts with the mounted drive. It only occurs if the user has accessed the mounted drive in some way, i.e. the user opened the mounted drive in Windows Explorer. Likewise, it does not occur if you quit the application completely and then restart it and log in as a different user.
All of this leads me to believe that there is a race condition where WNetCancelConnection2
returns before all open handles to the network resource are closed. My question is can I make my program block or wait after calling WNetCancelConnection2
until all those handles are closed, guaranteeing that WNetAddConnection2
doesn't fail in this way? Either a blocking API or a way to busy-wait for the connection to be fully closed would be acceptable. Either would be preferable to sleep()
and hope for the best. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论