大网数据广告活动
网络与域名解析

Manage IP addresses with AddIPAddress, DeleteIPAddress

· 52CE 我爱测

The AddIPAddress function adds the specified IPv4 address to the specified adapter.

The AddIPAddress function adds the specified IPv4 address to the specified adapter. The DeleteIPAddress function deletes the specified IPv4 address from the specified adapter. These functions can be used to add and delete IPv4 addresses to a network adapter.

An IPv4 address added by the AddIPAddress function is not persistent. The IPv4 address exists only as long as the adapter object exists. Restarting the computer destroys the IPv4 address, as does manually resetting the network interface card (NIC).

After AddIPAddress has been called successfully, DHCP will be disabled for the IP address that was added. Therefore, functions such as IpReleaseAddress, which require DHCP to be enabled, will not be functional on the added IP address. The DeleteIPAddress function can be used to delete the added IPv4 address.

Note:
Group policies, enterprise policies, and other restrictions on the network may prevent these functions from completing successfully. Ensure that the application has the necessary network permissions before attempting to use these functions.

To use AddIPAddress

  1. Declare ULONG variables named NTEContext and NTEInstance, both initialized to zero.

    [!Note]
    The NTEContext variable is the only parameter to the DeleteIPAddress function; to delete the IP address that is added, NTEContext must be stored and unchanged.

        ULONG NTEContext = 0;
        ULONG NTEInstance = 0;
    
    

    [!Note]

  2. Declare variables for the IPAddr and IPMask structures named iaIPAddress and iaIPMask, respectively. These values are simply unsigned integers. Initialize the iaIPAddress and iaIPMask variables using the inet_addr function.

    UINT iaIPAddress;
    UINT iaIPMask;
    
    iaIPAddress = inet_addr("192.168.0.5");
    iaIPMask    = inet_addr("255.255.255.0");
    
  3. Call the AddIPAddress function to add the IPv4 address. Check for errors and return the error value to the DWORD variable dwRetVal (for more extensive error checking).

    dwRetVal = AddIPAddress(iaIPAddress, iaIPMask, pIPAddrTable->table[0].dwIndex, 
                                 &NTEContext, &NTEInstance);
    if (dwRetVal != NO_ERROR) {
        printf("AddIPAddress call failed with %d\n", dwRetVal);
    }
    

    [!Note]
    The third parameter is the adapter index, which can be obtained by calling the GetIpAddrTable function. It is assumed that the variable returned by this function is named pIPAddrTable. For help with the GetIpAddrTable function, see Managing IP Address Using GetIpAddrTable.

To use DeleteIpAddress

  • Call the DeleteIPAddress function, passing the NTEContext variable as its parameter. Check for errors and return the error value to the DWORD variable dwRetVal (for more extensive error checking).
    dwRetVal = DeleteIPAddress(NTEContext);
    if (dwRetVal != NO_ERROR) {
            printf("\tDeleteIPAddress failed with error: %d\n", dwRetVal);
    } 
    

Note:
To use DeleteIPAddress, AddIPAddress must first be called to get the handle NTEContext. The previous procedure assumes that AddIPAddress has already been called somewhere in the code, and NTEContext has been saved and remains uncorrupted.

Next Step: Retrieving Information Using GetIpStatistics

Previous Step: Managing DHCP Leases Using IpReleaseAddress and IpRenewAddress

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。