The fix to the problem described in
has caused another problem.
The new code simply checks GetLastError() return value without checking for return value of FormatMessage. As a result this function can (and has for us) throw out of memory exception when there is actually no problem and a formatted string is successfully returned in the supplied buffer.
I am guessing some internal Windows API sets an error during FormatMessage execution.
The correct way of fixing this would be to check for return value of FormatMessage and if it is 0 then check GetLastError() return value and only then throw an error.