InetPton()でのInetNtop()の使用



Use Inetntop With Inetpton



WindowsはInet_ntop()とInet_ptonをカプセル化します。これはInetNtop()とInetPton()になり、msdnには例がありません。自分で見て、VS2017での使用例を示しました。ここでは、Ipv4の使用例のみを示し、後続のipv6を示します。

#include 'pch.h' #include #include //Files declared by InetNtop and InetPton #pragma comment(lib,'Ws2_32.lib') using namespace std int main() { //int inet_pton(int af, const char *src, void *dst) PCWSTR src = TEXT('192.168.100.16') WCHAR ip[16] = TEXT('0') //Initialization is a good habit struct in_addr dst int i = 0 i = InetPton(AF_INET, src, &dst) if (1 == i) { cout << 'addr is ' << dst.S_un.S_addr << endl } //const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) if (InetNtop(AF_INET, &dst.S_un.S_addr, ip, 100) == NULL) { cout << 'error' << endl return -1 } Wcout << ip << endl // wide characters remember to output in wide characters }