inet_ptonw Function

Header: WinSock2.h

Library: Ws2_32.lib

DLL: Ws2_32.dll

The inet_ptonw function converts a Unicode string representation of an IPv6 address into its binary form.

Syntax


int inet_ptonw(
  [in]  PCWSTR pString,
  [out] PVOID  pAddr,
  [in]  size_t Size
);
                

Parameters

Return Value

Remarks

The inet_ptonw function is used to convert an IPv6 address from its string representation to its binary format. This is a Unicode version of the inet_pton function.

The pAddr buffer should be large enough to hold a 16-byte IPv6 address (sizeof(struct in6_addr)).

Example

The following code snippet demonstrates how to use the inet_ptonw function to convert an IPv6 address string.


#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

#pragma comment(lib, "Ws2_32.lib")

int main() {
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        printf("WSAStartup failed.\n");
        return 1;
    }

    PCWSTR ipv6AddressString = L"2001:0db8:85a3:0000:0000:8a2e:0370:7334";
    struct sockaddr_in6 ipv6Addr;
    int result;

    result = inet_ptonw(AF_INET6, ipv6AddressString, &(ipv6Addr.sin6_addr), sizeof(struct in6_addr));

    if (result == 1) {
        wprintf(L"Successfully converted '%ls' to binary IPv6 address.\n", ipv6AddressString);
        // You can now use the binary address in ipv6Addr.sin6_addr
    } else if (result == 0) {
        wprintf(L"Input string '%ls' is not a valid IPv6 address.\n", ipv6AddressString);
    } else {
        int error = WSAGetLastError();
        wprintf(L"inet_ptonw failed with error: %d\n", error);
    }

    WSACleanup();
    return 0;
}
            

Requirements

Attribute Value
Minimum supported client Windows Vista [desktop apps | UWP apps]
Minimum supported server Windows Server 2008 [desktop apps | UWP apps]
Target Platform Windows
Header WinSock2.h
Library Ws2_32.lib
DLL Ws2_32.dll

See Also