Hide shared files programmatically

The company I work with has a program written on the old version of vb6, which is updated quite often, and most clients run the executable from a mapped network drive. These are actually surprisingly few problems, the biggest of which is automatic updating. Currently, the updater (written in C ++) renames an existing exe, then downloads and puts the new version into the old version. This usually works fine, but in some environments it just fails.

The solution runs this command from 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 exe.

In C ++, I can use the System(""); function System(""); to run this command, or I could redirect the output of net files and iterate the search results for a specific file and run the net file /close command to close them, But it would be much better if there were winapi functions that have similar features to enhance reliability and safety in the future.

Is there any way to programmatically find all network shared files and close the corresponding ones?

+7
c ++ windows auto-update
source share
1 answer

You can programmatically do what net file /close does. Just enable lmshare.h and the link to Netapi32.dll . You have two functions: NetFileEnum to list all open network files (on this computer) and NetFileClose to close them.

A quick (it is assumed that the program runs on the same server and not many open connections, see the last paragraph) and dirty (without error checking) example:

 FILE_INFO_2* pFile = 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, 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, pFile[i].fi2_id); NetApiBufferFree(pFile); 

Learn more about NetFileEnum and NetFileClose . Note that NetFileEnum may return ERROR_MORE_DATA if more data is available.

+7
source share

All Articles