I'm porting some sockets code from Linux to Windows.
In Linux, I could use strerror()
to convert an errno code into a human-readable string.
MSDN documentation shows equivalent strings for each error code returned from WSAGetLastError()
, but I don't see anything about how to retrieve those strings. Will strerror()
work here too?
How can I retrieve human-readable error strings from Winsock?
As the documentation for
WSAGetLastError
says you can useFormatMessage
to obtain a text version of the error message.You need to set
FORMAT_MESSAGE_FROM_SYSTEM
in thedwFlags
parameter and pass the error code as thedwMessage
parameter.A slightly simpler version of mxcl's answer, which removes the need for malloc/free and the risks implicit therein, and which handles the case where no message text is available (since Microsoft doesn't document what happens then):