简体   繁体   English

以编程方式关闭共享文件

[英]Close shared files programmatically

The company I'm working with has a program written in ye olde vb6, which is updated pretty frequently, and most clients run the executable from a mapped network drive.我工作的公司有一个用 ye olde vb6 编写的程序,它经常更新,大多数客户端从映射的网络驱动器运行可执行文件。 This actually has surprisingly few issues, the biggest of which is automatic updates.这实际上几乎没有问题,其中最大的问题是自动更新。 Currently the updater program (written in c++) renames the existing exe, then downloads and places the new version into the old version's place.目前更新程序(用 C++ 编写)重命名现有的 exe,然后下载新版本并将其放置到旧版本的位置。 This generally works fine, but in some environments it simply fails.这通常工作正常,但在某些环境中它只是失败。

The solution is running this command from microsoft:解决方案是从 microsoft运行此命令:

for /f "skip=4 tokens=1" %a in ('net files') do net files %a /close

This command closes all network files that are shared (well... most) and then the updater can replace the exe.此命令关闭所有共享的网络文件(好吧...大多数),然后更新程序可以替换 exe。

In C++ I can use the System("");在 C++ 中,我可以使用System(""); function to run that command, or I could redirect the output of net files , and iterate through the results looking for the particular file in question and run net file /close command to close them.函数来运行该命令,或者我可以重定向net files的输出,并遍历结果以查找有问题的特定文件并运行net file /close命令以关闭它们。 But it would be much much nicer if there were winapi functions that have similar capabilities for better reliability and future safety.但是,如果有具有类似功能以提高可靠性和未来安全性的 winapi 函数,那就更好了。

Is there any way for me to programmatically find all network shared files and close relevant ones?有什么方法可以让我以编程方式查找所有网络共享文件并关闭相关文件?

You can programmatically do what net file /close does.您可以以编程方式执行net file /close执行的操作。 Just include lmshare.h and link to Netapi32.dll .只需包含lmshare.h并链接到Netapi32.dll You have two functions to use: NetFileEnum to enumerate all open network files (on a given computer) and NetFileClose to close them.您可以使用两个函数: NetFileEnum枚举所有打开的网络文件(在给定的计算机上)和NetFileClose关闭它们。

Quick (it assumes program is running on same server and there are not too many open connections, see last paragraph) and dirty (no error checking) example:快速(它假设程序在同一台服务器上运行并且没有太多打开的连接,见最后一段)和脏(没有错误检查)示例:

FILE_INFO_2* pFiles = NULL;
DWORD nRead = 0, nTotal = 0;

NetFileEnum(
  NULL, // servername, NULL means localhost
  "c:\\directory\\path", // basepath, directory where VB6 program is
  NULL, // username, searches for all users
  2, // level, we just need resource ID
  (LPBYTE*)&pFiles, // bufptr, need to use a double pointer to get the buffer
  MAX_PREFERRED_LENGTH, // prefmaxlen, collect as much as possible
  &nRead, // entriesread, number of entries stored in pFiles
  &nTotal, // totalentries, ignore this
  NULL //resume_handle, ignore this 
);

for (int i=0; i < nRead; ++i)
    NetFileClose(NULL, pFiles[i].fi2_id);

NetApiBufferFree(pFiles);

Refer to MSDN for details aboutNetFileEnum andNetFileClose .有关NetFileEnumNetFileClose 的详细信息,请参阅 MSDN。 Note that NetFileEnum may return ERROR_MORE_DATA if more data is available.请注意,如果有更多数据可用, NetFileEnum可能会返回ERROR_MORE_DATA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM