通过 C++ 更改我的动态 IP 地址

Change my dynamic IP address by c++

我有一个动态 IP 地址,实际上我可以从我的路由器页面 (http://192.168.1.1) 更改它,点击释放然后更新。

我可以通过 curl 向 http://192.168.1.1 页面发出 http 请求,但这只能解决我使用该路由器的计算机上的问题。

所以我很想知道是否有一种方法可以在不通过路由器页面 (192.168.1.1) 的情况下通过 C++ 更新我的 IP。

我也从命令行尝试过,但没有得到肯定的结果。 我试过的代码如下:

ipconfig /release

ipconfig /renew

我也试过这个代码:

#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;
}

我已经启用了 DHCP 服务器并设置了这些值: DHCP 服务状态 DHCP 状态:已启用 IP 初始化 DHCP:192.168.1.2 IP finale DHCP(Riservato per uso interno):192.168.1.254

我需要 运行 我的程序在 windows XP 和 Windows 7 平台上。

感谢您的帮助

IP Helper 函数包括 IPReleaseAddress and IPRenewAddress 来执行此操作。

您需要先枚举网络适配器,然后将正确的适配器 ID 传递给函数以执行此操作。您通常使用 GetInterfaceInfo.

我想改变我的动态 IP 我也会改变我的 public IP。但事实并非如此。 要更改我的 Public IP 地址,我没有更改动态 IP,但我需要更改 ROUTER IP。 事实上,ISP 为任何路由器连接分配一个 IP,这将是任何连接的设备的 public IP。

(For public IP 我指的是你可以在http://www.ipmy.it/中看到的IP地址)

因此,如果我想更改我的 public IP 地址,我必须更改我的路由器 IP 地址。 要更改它,我必须断开并重新连接我的路由器电源,或者我可以通过路由器面板 (192.168.1.1)。

所以唯一的自动解决方案是编写一个程序来执行该 http 请求。

我使用 libCURL 用 C++ 编写的。

感谢大家的支持