title: Asynchronous ReadDirectoryChangesW - GetQueuedCompletionStatus always times out
body: <p>Exactly as it sounds, I'm attempting asynchronous <code>ReadDirectoryChangesW</code> with IO Completion and it isn't working, specifically, <code>GetLastError</code> repeatedly returns 258 (<code>GetQueuedCompletionStatus</code> timeout).</p>
  
  <p>I have structs:</p>
  
  <pre><code>typedef struct dirinfo_struct
  {
      HANDLE hDirFH;           // directory handle
      OVERLAPPED Overlapped;   // overlapped storage
      int len_buffer;          // buffer length
      wchar_t* buffer;         // buffer itself
      wchar_t* directory_name; // target name
  } dirinfo_t;
  
  typedef struct dirmon_struct
  {
      HANDLE hDirOPPort;       // handle to the IO port.
      dirinfo_t* dirinfo;      // pointer to the struct above.
  } dirmon_t;
  </code></pre>
  
  <p>for storing the relevant information. This is initialised:</p>
  
  <pre><code>dirinfo_t* t = malloc(1*sizeof(dirinfo_t));
  dirmon_t* d = malloc(1*sizeof(dirmon_t));
  dirinfo_init(t); // does t-&gt;buffer = malloc(8192*sizeof(wchar_t));
  </code></pre>
  
  <p>Then I create my Directory Handle and com port:</p>
  
  <pre><code>t-&gt;hDirFH = CreateFile(L"C:\\test",
                          FILE_LIST_DIRECTORY,
                          FILE_SHARE_READ|FILE_SHARE_WRITE,
                          NULL,
                          OPEN_EXISTING,
                          FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
                          NULL); 
  d-&gt;dirinfo = t;
  d-&gt;hDirOPPort = CreateIoCompletionPort(d-&gt;dirinfo-&gt;hDirFH, 
                                         NULL,       
                                         (ULONG_PTR)(d-&gt;dirinfo), 
                                         1); 
  </code></pre>
  
  <p>Then I pass this information via d to a new thread. Now on said new thread I have:</p>
  
  <pre><code>bResultQ = GetQueuedCompletionStatus(d-&gt;hDirOPPort, lpBytes, 
                                       (ULONG_PTR*)d-&gt;dirinfo,    
                                       lpOverlapped, 1000);
  
  if ( bResultQ )
  {
      bResultR = ReadDirectoryChangesW(d-&gt;dirinfo-&gt;hDirFH, 
                                       (void*)d-&gt;dirinfo-&gt;buffer, 
                                       8192, TRUE,
                                       FILE_NOTIFY_CHANGE_FILE_NAME | 
                                       FILE_NOTIFY_CHANGE_DIR_NAME |
                                       FILE_NOTIFY_CHANGE_ATTRIBUTES | 
                                       FILE_NOTIFY_CHANGE_SIZE |
                                       FILE_NOTIFY_CHANGE_LAST_WRITE | 
                                       FILE_NOTIFY_CHANGE_LAST_ACCESS | 
                                       FILE_NOTIFY_CHANGE_CREATION | 
                                       FILE_NOTIFY_CHANGE_SECURITY,
                                       lpReadDirBytes,
                                       &amp;d-&gt;dirinfo-&gt;Overlapped,
                                       NULL );
  } 
  else
  {
      printf("GetQueuedCompletionStatus(): Failed, ");
      errorcode = GetLastError();
      printf("Error Code %d\n", errorcode);
      Sleep(500);
  }
  </code></pre>
  
  <p>So I set this off running and I merrily get timeouts (258 errors) as I should since the directory hasn't changed. However, even if I alter the directory, I'm still getting error messages; in other words those alterations are not being picked up. Which leads me to believe I've got this set up incorrectly.</p>
  
  <p>Any ideas?</p>
  
  <p>Caveats:</p>
  
  <ul>
  <li>Ironically, this will eventually be converted to Python via pywin32. However, until I understand how this is supposed to work in C, I'm not going there.</li>
  <li>Synchronous <code>ReadDirectoryChangesW</code> is not an option. If no events are fired, I need the thread this is on to timeout so it can check if it should still be running.</li>
  <li>I am writing in C specifically, not C++.</li>
  <li><code>FindFirstChangeNotification</code> etc not an option either - I don't want to continually be comparing directory listings to work out what has changed.</li>
  </ul>
  
  <p>Other notes:</p>
  
  <ul>
  <li>The Directory exists, that handle is not NULL. Likewise for the comport handle.</li>
  <li>Everything's being passed to the thread </li>
  </ul>
  
  <p>I've taken a look at <a href="http://www.codeproject.com/KB/files/directorychangewatcher.aspx" rel="noreferrer">CDirectoryChangeWatcher</a> from code project, but use of C++ and many more threads aside, I can't see what I'm doing differently. Feel free to point it out if I'm missing something though!</p>
  
  <p>Output, if it helps, is basically on repeat, no matter how much I alter the directory in question.</p>
  
  <pre><code>GetQueuedCompletionStatus(): Failed, Error Code 258
  </code></pre>