Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 533335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:29:54+00:00 2026-05-13T09:29:54+00:00

This is a part of an open source project called JNotify. I am trying

  • 0

This is a part of an open source project called JNotify. I am trying to fix the Win32 Implementation and it’s really driving me nuts. I already read everything there is to read in MSDN about this, and read every web post about this sucky API.
I am trying to receive file system notifications on windows using ReadDirectoryChangesW, using a completion port.

the behavior I am seeing is that normally it works, but some times the buffer I receive when GetQueuedCompletionStatus returns is corrupted in strange ways. eitehr FILE_NOTIFY_INFORMATION.NextEntryOffset points to the itself (resulting in an endless loop), or something else goes wrong and I receive a bogus file name length.
This only happens if I re-watch the directory, never in the first event (but re-watching is required otherwise you only get one event for that directory).

The test code that crashes every thing is trivial, it just watch many dirs and creates 2 files in each directory.

here is some relevant code, I can add all of it if you want (the whole thing is not too big), but feels too big for a question here.

This bit of code creates the completion port, it only runs once – and then I use this completion port for all directories.

_completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);

This is the WatchData constructor, that actually opens the directory handle and associates it with the completion port.

WatchData::WatchData(const WCHAR* path, int mask, bool watchSubtree, HANDLE completionPort)
    :
    _watchId(++_counter), 
    _mask(mask), 
    _watchSubtree(watchSubtree),
    _byteReturned(0),
    _completionPort(completionPort)
{
    _path = _wcsdup(path); 
    _hDir = CreateFileW(_path,
                         FILE_LIST_DIRECTORY | GENERIC_READ | GENERIC_WRITE,
                         FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                         NULL, //security attributes
                         OPEN_EXISTING,
                         FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL);
    if(_hDir == INVALID_HANDLE_VALUE )  
    {
        throw GetLastError();
    }

    if (NULL == CreateIoCompletionPort(_hDir, _completionPort, (ULONG_PTR)&_watchId, 0))
    {
        throw GetLastError();
    }
}

This is the code running (inside a WatchData object) when I start watching a directory:

int WatchData::watchDirectory()
{
    printf("(Re)watching %ls\n", _path);
    memset(_buffer, 0, sizeof(_buffer));
    memset(&_overLapped, 0, sizeof(_overLapped));
    if( !ReadDirectoryChangesW( _hDir,
                                _buffer,//<--FILE_NOTIFY_INFORMATION records are put into this buffer
                                sizeof(_buffer),
                                _watchSubtree,
                                _mask,
                                &_byteReturned,
                                &_overLapped,
                                NULL))



    {
        return GetLastError();
    }
    else
    {
        return 0;
    }
}

This is the main loop that run in it’s own thread, handling completion events.
Note that “This should not happen bit”, it actually happens a lot.

DWORD WINAPI Win32FSHook::mainLoop( LPVOID lpParam )
{
    debug("mainLoop starts");
    Win32FSHook* _this = (Win32FSHook*)lpParam;

    HANDLE hPort = _this->_completionPort;
    DWORD dwNoOfBytes = 0;
    ULONG_PTR ulKey = 0;
    OVERLAPPED* pov = NULL;
    WCHAR name[1024];

    while (_this->_isRunning)
    {
        pov = NULL;
        BOOL fSuccess = GetQueuedCompletionStatus(
                        hPort,         // Completion port handle
                        &dwNoOfBytes,  // Bytes transferred
                        &ulKey,
                        &pov,          // OVERLAPPED structure
                        INFINITE       // Notification time-out interval
                        );
        if (fSuccess)
        {
            if (dwNoOfBytes == 0)
            {
                // can happen after a watch is removed
                continue;
            }
            int wd = *(int*)ulKey;
            EnterCriticalSection(&_this->_cSection);
            WatchData *watchData = _this->find(wd);
            if (!watchData)
            {
                log("mainLoop : ignoring event for watch id %d, no longer in wid2WatchData map", wd);
                LeaveCriticalSection(&_this->_cSection);
                continue;
            }

            //const char* buffer = watchData->getBuffer();
            char buffer[watchData->getBufferSize()];
            memcpy(buffer, watchData->getBuffer(), watchData->getBufferSize());
            LeaveCriticalSection(&_this->_cSection);
            FILE_NOTIFY_INFORMATION *event;
            DWORD i=0;
            do
            {
                event = (FILE_NOTIFY_INFORMATION*)(buffer+i);
                int action = event->Action;
                DWORD len = event->FileNameLength / sizeof(WCHAR);
                for (DWORD k=0;k<len && k < (sizeof(name)-sizeof(WCHAR))/sizeof(WCHAR);k++)
                {
                    name[k] = event->FileName[k];
                }
                name[len] = 0;

                _this->_callback(watchData->getId(), action, watchData->getPath(), name);

                if (i != 0 && event->NextEntryOffset == i)
                {
                    log("should not happen!");
                    break;
                }

                i = event->NextEntryOffset;
            }
            while (i != 0);

            int res = watchData->watchDirectory();
            if (res != 0)
            {
                log("Error watching dir %s : %d",watchData->getPath(), res);
            }
        }
        else
        {
            log("GetQueuedCompletionStatus returned an error");
        }
    }
    debug("mainLoop exits");
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:29:54+00:00Added an answer on May 13, 2026 at 9:29 am

    I’m pretty sure NextEntryOffset is relative to the current record, not the first record.

    ...
    char* current = buffer;
    do
    {
        event = (FILE_NOTIFY_INFORMATION*)current;
        ...
        i = event->NextEntryOffset;
        current += i;
    }
    while (i != 0);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.