title: ReadFile() says it failed, but the error code is ERROR_SUCCESS
body: <p>I'm using <code>ReadFile()</code> on Windows to read data from a serial port. This code was working fine at one point in time, but it's now failing and I'm trying to track down the source of the problem, so I doubt it's a problem with the serial configuration or timeouts, since none of that has changed.</p>
  
  <p><code>ReadFile()</code> returns false, indicating that an error occurred. However, when I immediately check the value of <code>GetLastError()</code>, it returns 0, which is <code>ERROR_SUCCESS</code>. The number of bytes read is 0, so I'm inclined to think that indeed something <em>has</em> gone wrong, but that error code is utterly useless.</p>
  
  <p>Any ideas? Thanks.</p>
  
  <p>EDIT: Here are some relevant code snippets:</p>
  
  <pre><code>#define GPS_COM_PORT L"COM3"
  
  // for reference, the device communicates at 115200 baud,
  // no parity, 1 stop bit, no flow control
  
  // open gps com port
  hGpsUart = CreateFile(GPS_COM_PORT, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (hGpsUart == INVALID_HANDLE_VALUE)
  {
      if (GetLastError() == ERROR_FILE_NOT_FOUND)
      {
          msg.setText("GPS COM port does not exist!");
          msg.exec();
          QApplication::quit();
      }
  
      msg.setText("Error occurred while trying to open GPS COM port!");
      msg.exec();
      QApplication::quit();
  }
  
  // set gps com port settings
  dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  if (!GetCommState(hGpsUart, &amp;dcbSerialParams))
  {
      msg.setText("Could not get GPS COM port settings!");
      msg.exec();
      QApplication::quit();
  }
  dcbSerialParams.BaudRate = CBR_115200;
  dcbSerialParams.ByteSize = 8;
  dcbSerialParams.StopBits = ONESTOPBIT;
  dcbSerialParams.Parity = NOPARITY;
  if (!SetCommState(hGpsUart, &amp;dcbSerialParams))
  {
      msg.setText("Could not set GPS COM port settings!");
      msg.exec();
      QApplication::quit();
  }
  
  // set gps com port timeouts
  timeouts.ReadIntervalTimeout = MAXDWORD;
  timeouts.ReadTotalTimeoutConstant = 0;
  timeouts.ReadTotalTimeoutMultiplier = 0;
  timeouts.WriteTotalTimeoutConstant = 50;
  timeouts.WriteTotalTimeoutMultiplier = 10;
  if (!SetCommTimeouts(hGpsUart, &amp;timeouts))
  {
      msg.setText("Could not set GPS COM port timeouts!");
      msg.exec();
      QApplication::quit();
  }
  
  // ... later in the code ...
  
  char buf[161] = {0};
  DWORD bytes_read = 0;
  
  // This returns false...
  if (!ReadFile(hGpsUart, buf, 160, &amp;bytes_read, NULL)) 
  {
      // Yet in here, GetLastError() returns ERROR_SUCCESS (0)
      QMessageBox msg;
      msg.setText("Error reading from GPS UART!");
      msg.exec();
  }
  </code></pre>