{"id":659,"date":"2016-12-12T15:32:21","date_gmt":"2016-12-12T05:32:21","guid":{"rendered":"http:\/\/www.nickdu.com\/?p=659"},"modified":"2016-12-12T15:32:21","modified_gmt":"2016-12-12T05:32:21","slug":"update-ip-address-mask-address-c-windows","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=659","title":{"rendered":"Change IP address &#038; Mask address (C++) Windows"},"content":{"rendered":"<p><b>Introduction<\/b><\/p>\n<p>This is one way of changing IP address &amp; mask address in C++ using Iphlpapi.h. It&#8217;s a simple program, no UI for you, sorry&#8230;<\/p>\n<ul>\n<li>Use GetAdaptersInfo to get adapters information.<\/li>\n<li>Then use GetPerAdapterInfo to get information for every ethernet network card. The information contains IP address.<\/li>\n<li>Use DeleteIPAddress remove the old IP address.<\/li>\n<li>Use AddIPAddress to add new IP&amp;Mask address.<\/li>\n<\/ul>\n<pre lang=\"c\">\/\/ ChangeIPAddress(\"192.168.0.123\", \"192.168.0.44\",\"255,255,255,0\");\nbool ChangeIPAddress(char oldIPAddress[], char newIPAddress[], char newMaskAddress[])\n{\n    DWORD dwRetVal = 0;\n    PIP_ADAPTER_INFO pAdapter = NULL;\n\n    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);\n    PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));\n    if (pAdapterInfo == NULL)\n    {\n        return false;\n    }\n\n    if (GetAdaptersInfo(pAdapterInfo, &amp;ulOutBufLen) == ERROR_BUFFER_OVERFLOW)\n    {\n        free(pAdapterInfo);\n        pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);\n        if (pAdapterInfo == NULL)\n        {\n            return false;\n        }\n    }\n\n    if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &amp;ulOutBufLen)) == NO_ERROR)\n    {\n        pAdapter = pAdapterInfo;\n        while (pAdapter)\n        {\n            if (strcmp(oldIPAddress, pAdapter-&gt;IpAddressList.IpAddress.String) == 0)\n            {\n                IPAddr addr = inet_addr(newIPAddress);\n                IPMask mask = inet_addr(newMaskAddress);\n                ULONG context, instance;\n\n                if (DeleteIPAddress(pAdapter-&gt;IpAddressList.Context) != NO_ERROR\n                    || AddIPAddress(addr, mask, pAdapter-&gt;Index, &amp;context, &amp;instance) != NO_ERROR)\n                {\n                    return false;\n                }\n                return true;\n            }\n            pAdapter = pAdapter-&gt;Next;\n        }\n        return false;\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Introduction This is one way of changing IP address &amp; mask address in C++ using Iphlpapi.h. It&#8217;s a simple program, no UI for you, sorry&#8230; Use GetAdaptersInfo to get adapters information. Then use GetPerAdapterInfo to get information for every ethernet network card. The information contains IP address. Use DeleteIPAddress remove the old IP address. Use &hellip; <a href=\"https:\/\/nickdu.com\/?p=659\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Change IP address &#038; Mask address (C++) Windows&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,2],"tags":[],"class_list":["post-659","post","type-post","status-publish","format-standard","hentry","category-cc","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=659"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/659\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}