0

From the Winsock Reference page, it said:

getaddrinfo function

Return value

Success returns zero. Failure returns a nonzero Windows Sockets error code, as found in the Windows Sockets Error Codes.

Use the gai_strerror function to print error messages based on the EAI codes returned by the getaddrinfo function. The gai_strerror function is provided for compliance with IETF recommendations, but it is not thread safe. Therefore, use of traditional Windows Sockets functions such as WSAGetLastError is recommended.

It said the EAI error codes can be found in the Windows Sockets Error Codes page, but I cannot see any EAI code in that page, such as EAI_AGAIN, EAI_BADFLAGS, etc...

For Windows, the correct way to get the code is to check if getaddrinfo returns a nonzero value, then call WSAGetLastError to get the code?

Community
  • 1
  • 1
MiP
  • 5,846
  • 3
  • 26
  • 41
  • Ya you use WSAGetLastError and here's how to retrieve the corresponding string: https://stackoverflow.com/questions/3400922/how-do-i-retrieve-an-error-string-from-wsagetlasterror?rq=1 – Asesh Aug 08 '17 at 07:15
  • 'It said the EAI error codes can be found in the Windows Sockets Error Codes page'. No it didn't. It provided a mapping *on that MSDN page.* – user207421 Aug 08 '17 at 08:37

1 Answers1

1

Yes, the standard approach is to use getaddrinfo and if it returns nonzero, then call WSAGetLastError to retrieve the Winsock error code.

You can then use FormatMessage to convert the error code into a human-readable string, by passing FORMAT_MESSAGE_FROM_SYSTEM as the dwFlags parameter, and the error code as the dwMessage parameter. You can see the documentation for this behaviour here, in the table under FORMAT_MESSAGE_FROM_SYSTEM. Asesh has pointed out this link which has some sample code demonstrating this.

You can use gai_strerror like on a Linux system, but according to MSDN (search for gai_strerror), it's not thread safe so FormatMessage is preferred.

Alternatively you can just manually cross-reference them with this nice big list.

hnefatl
  • 5,860
  • 2
  • 27
  • 49