How to convert an ipv6 address to its standard text presentation under Windows XP

SUMMARY

This article describes how to convert an ipv6 address to its standard text presentation under Windows XP.

InetNtop(), RtlIpv6AddressToString()

To convert an ipv6 address to its standard text presentation, the easiest way is to use InetNtop() or RtlIpv6AddressToString() function.

From MSDN

The InetNtop function converts an IPv4 or IPv6 Internet network address into a string in Internet standard format. The ANSI version of this function is inet_ntop.

PCTSTR WSAAPI InetNtop(
  __in   INT  Family,
  __in   PVOID pAddr,
  __out  PTSTR pStringBuf,
  __in   size_t StringBufSize
);

The RtlIpv6AddressToString function converts an IPv6 address to a string in Internet standard format.

LPTSTR NTAPI RtlIpv6AddressToString(
  __in   const IN6_ADDR *Addr,
  __out  LPTSTR S
);

But Microsoft  imported those two functions after Windows Vista, so they are not available and don’t worked under Windows XP.  When use them under Window XP,  for example inet_ntop(), you will get “The procedure entry point inet_ntop cold not be located in the dynamic link library WS2_32.dll” .

getaddrinfo(), WSAStringToAddress()

getaddrinfo() and WSAStringToAddress() seem good replacements/alternatives for InetNtop() and RtlIpv6AddressToString().

The WSAStringToAddress function converts a network address in its standard text presentation form into its numeric binary form in a sockaddr structure, suitable for passing to Windows Sockets routines that take such a structure.

INT WSAAPI WSAStringToAddress(
  __in      LPTSTR AddressString,
  __in      INT AddressFamily,
  __in_opt  LPWSAPROTOCOL_INFO lpProtocolInfo,
  __out     LPSOCKADDR lpAddress,
  __inout   LPINT lpAddressLength
);

The getaddrinfo function provides protocol-independent translation from an ANSI host name to an address. (Use the AI_NUMERICHOST flag to prevent a DNS lookup)

int WSAAPI getaddrinfo(
  _In_opt_  PCSTR pNodeName,
  _In_opt_  PCSTR pServiceName,
  _In_opt_  const ADDRINFOA *pHints,
  _Out_     PADDRINFOA *ppResult
);

To use getaddrinfo() and WSAStringToAddress() functions convert IPv6 address, IPv6 protocol must also be installed on the local computer, otherwise the function call will return SOCKET_ERROR, And the error number WSAEINVAL can be retrieved by calling WSAGetLastError().