ReadFile() says it failed, but the error code is ERROR_SUCCESS

后端 未结 2 470
被撕碎了的回忆
被撕碎了的回忆 2021-01-15 11:41

I\'m using ReadFile() 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 trac

相关标签:
2条回答
  • 2021-01-15 12:17

    The constructor of QMessageBox may be doing something that clears `GetLastError'. Try this:

    if (!ReadFile(hGpsUart, buf, 160, &bytes_read, NULL)) 
    {
        int LastError = GetLastError() ;
        QMessageBox msg;
        msg.setText(QString("Error %1 reading from GPS UART!").arg(LastError));
        msg.exec();
    }
    
    0 讨论(0)
  • 2021-01-15 12:39

    I think the key to your observations is the phrase in your source that says "Yet in here, GetLastError() returns ERROR_SUCCESS (0)"

    The call to GetLastError has to be the very next Win32 call made after the (presumably) failing call. As an experiment, try putting an explicit call to GetLastError() within your failure handler, but just before the message box call. I suspect you'll see the true failure code.

    Good luck!

    0 讨论(0)
提交回复
热议问题