0

I am working on a C++ application that detects changes happening on a shared folder (using ReadDirectoryChangesW) and call NetFileEnum on that modified file/folder. This way, I know the possible candidates modifying file/folder.

According to NetFileEnum docs:

Returns information about some or all open files on a server, depending on the parameters specified.

However, NetFileEnum is not consistent, it does not always tell me who is currently accessing the resource. It seems to work only 50-60% of the time.

More specifically, say I modify \\172.20.30.40\myshare\file.txt (local path: C:\myshare\file.txt).
NetFileEnum is then called with the following parameters:

fStatus = NetFileEnum(L"\\\\172.20.30.40",
            (LPWSTR)szLocalPathStr.c_str(), // C:\\myshare\\file.txt
            pszUserName,                    // NULL
            dLevel,                         // 3
            (LPBYTE*)&pFile,
            dPrefMaxLen,
            &dEntriesRead,
            &dTotalEntries,
            NULL);

fStatus returns NERR_success (0). However, 40-50% of the time, dEntriesRead is 0. Is this a known issue with NetFileEnum? Is there a more consistent way to know who all are accessing a shared resource?

Praveen
  • 73
  • 2
  • 5
  • 1
    `(LPWSTR)szLocalPathStr.c_str()` is a potential bug. Either use a `const_cast` or pass `szLocalPathStr.data()`, calling [std::string::data()](https://en.cppreference.com/w/cpp/string/basic_string/data) in place of `std::string::c_str()`. – IInspectable Sep 24 '18 at 06:50
  • You might want to read: https://stackoverflow.com/questions/49799109/win32file-readdirectorychangesw-doesnt-find-all-moved-files. I haven't worked with *NetFileEnum* for years, but something doesn't seem right about the 1st 2 parameters. – CristiFati Sep 27 '18 at 16:04
  • @CristiFati I consulted the MSDN docs for NetFileEnum and I don't find anything unusual. I could remove \\ before server name though. – Praveen Oct 09 '18 at 08:53
  • I initially made a confusion, I thought that the 2nd argument should be the share name, but it's not the case. I've recently tested it from *Python* and it works if the server name contains simply the IP (or *NULL*), and the 2nd arg contains the absolute path. I also pasted an *URL* to an answer that I provided regarding *ReadDirectoryChangesW* (which is most likely the cause for some events being lost). – CristiFati Oct 09 '18 at 09:32
  • @CristiFati Passing server name as NULL implies local machine. Also, I am aware of the problem with ReadDirectoryChangesW, I use a buffered and asynchronous version. The problem here is with NetFileEnum, and I am still looking for a solution. – Praveen Oct 09 '18 at 10:34

0 Answers0