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

Manage DHCP leases with IpReleaseAddress, IpRenewAddress

· 52CE 我爱测

The IpReleaseAddress and IpRenewAddress functions are used to release and renew the current Dynamic Host Configuration Protocol (DHCP) lease.

The IpReleaseAddress and IpRenewAddress functions are used to release and renew the current Dynamic Host Configuration Protocol (DHCP) lease. The IpReleaseAddress function releases an IPv4 address previously obtained through DHCP. The IpRenewAddress function renews a lease on an IPv4 address previously obtained through DHCP. It is common to use these two functions together, first releasing the lease with a call to IpReleaseAddress, and then renewing the lease with a call to the IpRenewAddress function.

When a DHCP client has previously obtained a DHCP lease and IpReleaseAddress is not called before the IpRenewAddress function, the DHCP client request is sent to the DHCP server that issued the initial DHCP lease. This DHCP server may not available or the DHCP request may fail. When a host has previously obtained a DHCP lease and IpReleaseAddress is called before the IpRenewAddress function, the DHCP client first releases the IP address obtained and sends a DHCP client request for a response from any available DHCP server.

Note:
The IpReleaseAddress and IpRenewAddress functions require that DHCP be enabled to perform correctly.

The IpReleaseAddress function takes a pointer to an IP_ADAPTER_INDEX_MAP structure as its only parameter. To obtain this parameter, first call GetInterfaceInfo. For help with the GetInterfaceInfo function, see Managing Interfaces Using GetInterfaceInfo.

To use IpReleaseAddress

  1. Obtain a pointer to an IP_ADAPTER_INDEX_MAP structure using the GetInterfaceInfo function. (For help with the GetInterfaceInfo function, see Managing Interfaces Using GetInterfaceInfo). Create a DWORD object dwRetVal (used for error checking). It is assumed that the variable returned by GetInterfaceInfo is called pInfo.

    DWORD dwRetVal;
    
    
  2. If DHCP is enabled, call the IpReleaseAddress function, passing the IP_ADAPTER_INDEX_MAP variable Adapter as its parameter. Check for general errors and return its value to the DWORD variable dwRetVal (for more extensive error checking).

    [!Note]
    The GetAdaptersInfo function returns a parameter that can be used to check whether DHCP is enabled before calling these functions. For help with GetAdaptersInfo, see Managing Network Adapters Using GetAdaptersInfo.

    if ((dwRetVal = IpReleaseAddress(&pInfo->Adapter[0])) == NO_ERROR) {
        printf("Ip Release succeeded.\n");
    }
    
    

Note:
It is common to use these two functions together, calling the IpReleaseAddress function and then calling the IpRenewAddress function, passing the same structure as the parameter to both functions. The following procedure assumes that the functions are not used together; however, if the functions are used together, skip step 1.

To use IpRenewAddress

  1. Obtain a pointer to an IP_ADAPTER_INDEX_MAP structure using the GetInterfaceInfo function. (For help with the GetInterfaceInfo function, see Managing Interfaces Using GetInterfaceInfo). Declare a DWORD object dwRetVal (used for error checking) if this variable has not been declared. It is assumed that the variable returned by GetInterfaceInfo is called pInfo.

    DWORD dwRetVal;
    
    
  2. Call the IpRenewAddress function, passing the IP_ADAPTER_INDEX_MAP variable Adapter as its parameter. Check for general errors and return its value to the DWORD variable dwRetVal (for more extensive error checking).

    if ((dwRetVal = IpRenewAddress(&pInfo->Adapter[0])) == NO_ERROR) {
        printf("Ip Renew succeeded.\n");
    }
    

Next Step: Managing IP Addresses Using AddIPAddress and DeleteIPAddress

Previous Step: Managing IP Addresses Using GetIpAddrTable

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)

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

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