title: CreateFile() returns INVALID_HANDLE_VALUE but GetLastError() is ERROR_SUCCESS
body: <p>I am opening a serial port using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx">CreateFile()</a>. I've got a testcase (too complicated to redistribute) that consistently causes <code>CreateFile()</code> to return <code>INVALID_HANDLE_VALUE</code> and <code>GetLastError()</code> to return <code>ERROR_SUCCESS</code>. By the looks of it, this bug only occurs if one thread opens the port at the exact same time that another port closes it. The thread opening the port runs across this problem.</p>
  
  <p>I don't know if this makes a difference, but later on in the code I associate the port with a CompletionPort using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363862%28v=vs.85%29.aspx">CreateIoCompletionPort</a>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>HANDLE port = CreateFile(L"\\\\.\\COM1",
                           GENERIC_READ | GENERIC_WRITE,
                           0,                    // must be opened with exclusive-access
                           0,                    // default security attributes
                           OPEN_EXISTING,        // must use OPEN_EXISTING
                           FILE_FLAG_OVERLAPPED, // overlapped I/O
                           0);                   // hTemplate must be NULL for comm devices
  if (port == INVALID_HANDLE_VALUE)
  {
      DWORD errorCode = GetLastError();
      cerr &lt;&lt; L"CreateFile() failed with error: " &lt;&lt; errorCode &lt;&lt; endl;
  }
  </code></pre>
  
  <p>I'm pretty sure this sort of thing should not happen. Am I doing anything wrong? How do I get the API to return a correct result?</p>
  
  <hr>
  
  <p><strong>MORE DETAILS</strong>: This code is taken from a serial-port library I've developed: <a href="http://code.google.com/p/jperipheral/">JPeripheral</a></p>
  
  <p>Here is the actual (unsanitized) source-code:</p>
  
  <pre><code>JLong SerialChannel::nativeOpen(String name)
  {
      cerr &lt;&lt; "nativeOpen(" &lt;&lt; name &lt;&lt; ")" &lt;&lt; endl;
      wstring nameWstring = name;
      HANDLE port = CreateFile((L"\\\\.\\" + nameWstring).c_str(),
          GENERIC_READ | GENERIC_WRITE,
          0,                                          // must be opened with exclusive-access
          0,                                          // default security attributes
          OPEN_EXISTING,                  // must use OPEN_EXISTING
          FILE_FLAG_OVERLAPPED,       // overlapped I/O
          0);                                         // hTemplate must be NULL for comm devices
      cerr &lt;&lt; "nativeOpen.afterCreateFile(" &lt;&lt; name &lt;&lt; ")" &lt;&lt; endl;
      cerr &lt;&lt; "port: " &lt;&lt; port &lt;&lt; ", errorCode: " &lt;&lt; GetLastError() &lt;&lt; endl;
      if (port == INVALID_HANDLE_VALUE)
      {
          DWORD errorCode = GetLastError();
  
          switch (errorCode)
          {
              case ERROR_FILE_NOT_FOUND:
                  throw PeripheralNotFoundException(jace::java_new&lt;PeripheralNotFoundException&gt;(name, Throwable()));
              case ERROR_ACCESS_DENIED:
              case ERROR_SHARING_VIOLATION:
                  throw PeripheralInUseException(jace::java_new&lt;PeripheralInUseException&gt;(name, Throwable()));
              default:
              {
                  throw IOException(jace::java_new&lt;IOException&gt;(L"CreateFile() failed with error: " +
                      getErrorMessage(GetLastError())));
              }
          }
      }
  
      // Associate the file handle with the existing completion port
      HANDLE completionPort = CreateIoCompletionPort(port, ::jperipheral::worker-&gt;completionPort, Task::COMPLETION, 0);
      if (completionPort==0)
      {
          throw AssertionError(jace::java_new&lt;AssertionError&gt;(L"CreateIoCompletionPort() failed with error: " +
              getErrorMessage(GetLastError())));
      }
      cerr &lt;&lt; "nativeOpen.afterCompletionPort(" &lt;&lt; name &lt;&lt; ")" &lt;&lt; endl;
  
      // Bind the native serial port to Java serial port
      SerialPortContext* result = new SerialPortContext(port);
      cerr &lt;&lt; "nativeOpen.afterContext(" &lt;&lt; name &lt;&lt; ")" &lt;&lt; endl;
      return reinterpret_cast&lt;intptr_t&gt;(result);
  }
  </code></pre>
  
  <p>Here is the actual output I get:</p>
  
  <pre><code>nativeOpen(COM1)
  nativeOpen.afterCreateFile(COM1)
  port: 00000374, errorCode: 0
  nativeOpen.afterCompletionPort(COM1)
  nativeOpen.afterContext(COM1)
  [...]
  nativeOpen(COM1)
  nativeOpen.afterCreateFile(COM1)
  port: FFFFFFFF, errorCode: 0
  java.io.IOException: CreateFile() failed with error: The operation completed successfully.
  </code></pre>