I've got a dynamic IP address, infact I can change it from my router page (http://192.168.1.1) clicking on release and then renew.
I could made the http request by curl to the http://192.168.1.1 page but this solve the problem only on my computers that use that router.
So I'm interested to know if is there a way to update my IP by c++ without passing through the router page (192.168.1.1).
I've tried also from command line without positive result. The code I've tried is the following:
ipconfig /release
ipconfig /renew
I've also tried this code:
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include "stdafx.h"
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib, "iphlpapi.lib")
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
// Before calling IpReleaseAddress and IpRenewAddress we use
// GetInterfaceInfo to retrieve a handle to the adapter
void __cdecl main()
{
ULONG ulOutBufLen = 0;
DWORD dwRetVal = 0;
PIP_INTERFACE_INFO pInfo;
pInfo = (IP_INTERFACE_INFO *)MALLOC(sizeof(IP_INTERFACE_INFO));
// Make an initial call to GetInterfaceInfo to get
// the necessary size into the ulOutBufLen variable
if (GetInterfaceInfo(pInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER) {
FREE(pInfo);
pInfo = (IP_INTERFACE_INFO *)MALLOC(ulOutBufLen);
}
// Make a second call to GetInterfaceInfo to get the
// actual data we want
if ((dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen)) == NO_ERROR) {
printf("\tAdapter Name: %ws\n", pInfo->Adapter[0].Name);
printf("\tAdapter Index: %ld\n", pInfo->Adapter[0].Index);
printf("\tNum Adapters: %ld\n", pInfo->NumAdapters);
}
else if (dwRetVal == ERROR_NO_DATA) {
printf("There are no network adapters with IPv4 enabled on the local system\n");
return;
}
else {
LPVOID lpMsgBuf;
printf("GetInterfaceInfo failed.\n");
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwRetVal,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR)&lpMsgBuf,
0,
NULL)) {
printf("\tError: %s", lpMsgBuf);
}
LocalFree(lpMsgBuf);
return;
}
// Call IpReleaseAddress and IpRenewAddress to release and renew
// the IP address on the first network adapter returned
// by the call to GetInterfaceInfo.
if ((dwRetVal = IpReleaseAddress(&pInfo->Adapter[0])) == NO_ERROR) {
printf("IP release succeeded.\n");
}
else {
printf("IP release failed: %ld\n", dwRetVal);
}
if ((dwRetVal = IpRenewAddress(&pInfo->Adapter[0])) == NO_ERROR) {
printf("IP renew succeeded.\n");
}
else {
printf("IP renew failed: %ld\n", dwRetVal);
}
// Free memory for IP_INTERFACE_INFO
if (pInfo != NULL) {
FREE(pInfo);
}
std::cout << ("\n Processo terminato\n");
std::system("PAUSE");
return;
}
I've DHCP Server enabled whit these values: DHCP Service State DHCP State: Enabled IP iniziale DHCP: 192.168.1.2 IP finale DHCP (Riservato per uso interno): 192.168.1.254
I need run my program on windows XP and Windows 7 platform.
Thank's for your help
Some ISPs assign static IP addresses to their subscribers. Home users are commonly configured with a dynamic IP address. Either way, you may be able to contact your ISP to request a new static IP or a dynamic IP change. You can't change your external internet IP address on your own.
The IP Helper functions include IPReleaseAddress and IPRenewAddress to do exactly this.
You'll need to start by enumerating the network adapters though, and pass the correct adapter's ID to the functions to do that. You usually do that with GetInterfaceInfo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With