Closed 0ric1 closed 5 years ago
The 1 == was probably an overreaction to "BOOL cast to bool" warnings, I'll delete them, well spotted.
The resize() is needed to guarantee that there's space for a 100 character message to be returned, it's resize, not reserve because reserve would still leave a string of length 0, so the resize to "len"after calling FormatMessage would fill it with null characters. I've added a MaxMsgLen constant and a comment to make the code more obvious.
An alternative would be to make two calls on FormatMsg, much as is done in GetUserName (the first to get the message length, the second to get the message), but it didn't seem worth it to save a few bytes in an error path.
rtrim modifies its parameter so it was easy to move and I've done that.
the functions ::GetComputerNameEx, ::GetUserName return a BOOL so no need to compare "==" with ERROR_SUCCESS or 1. In WinErrorMsg theMsg.resize(100) is not necessary and rtrim only in the try case if !theMsg.empty().