Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect error returned for c'tor on windows #127

Open
dronesalot opened this issue Apr 8, 2016 · 1 comment
Open

Incorrect error returned for c'tor on windows #127

dronesalot opened this issue Apr 8, 2016 · 1 comment

Comments

@dronesalot
Copy link

in win.cc line 85 there is the following line:
ss << "Unknown error opening the serial port: " << errno;

In fact the error code was returned from
DWORD errno_ = GetLastError();
So the line should really read:
ss << "Unknown error opening the serial port: " << errno_;

Ultimately I think it's not a good variable name anyway given that errno is already defined. The proper method should probably call FormatMessage to get the correct error message from the serial driver anyway. I can submit a patch if people think this is a good idea.

@dronesalot
Copy link
Author

The following is functional although not really technically correct. It falls back to a big of an architectural problem because the IOException does not support a UNICODE error string of type wstring so the windows error has to be converted and may not work properly under foreign language versions of windows. I have also not taken into account any non-UNICODE implementations but this falls back to the old behaviour. I've also renamed errno_ to last_error to improve readability.

Someone with the right tools can probably generate a proper patch for this.

if (fd_ == INVALID_HANDLE_VALUE) {
DWORD last_error = GetLastError();
stringstream ss;
LPVOID lpMsgBuf=NULL;
switch (last_error) {
case ERROR_FILE_NOT_FOUND:
// Use this->getPort to convert to a std::string
ss << "Specified port, " << this->getPort() << ", does not exist.";
THROW (IOException, ss.str().c_str());
default:
// get the text error specific to this serial driver
#ifdef UNICODE
DWORD result=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL );
if (result) {
wstring err((WCHAR *)lpMsgBuf,(unsigned int)result);
string errstr(err.begin(),err.end());
ss << "Unknown error opening the serial port: " << errstr;
THROW (IOException, ss.str().c_str());
}
else {
ss << "Unknown error opening the serial port: " << last_error;
THROW (IOException, ss.str().c_str());
}
#else
ss << "Unknown error opening the serial port: " << last_error;
THROW (IOException, ss.str().c_str());
#endif
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant