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

走远了吗. 提交于 2019-12-01 09:48:56

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!

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!