iphelper api

来源:互联网 发布:ttc提取ttf mac 编辑:程序博客网 时间:2024/05/01 02:30

http://msdn2.microsoft.com/en-us/library/aa366309.aspx

Managing IP Addresses Using GetIpAddrTable

The GetIpAddrTable function fills a pointer to an MIB_IPADDRTABLE structure with information about the current IP addresses associated with the system.

 

To use GetIpAddrTable

  1. Declare a pointer to an MIB_IPADDRTABLE object called pIPAddrTable, and a DWORD object called dwSize. These variables are passed as parameters to the GetIpAddrTable function. Also create a DWORD variable called dwRetVal (used for error checking).

     

    Copy Code
     MIB_IPADDRTABLE  *pIPAddrTable;DWORD            dwSize = 0;DWORD            dwRetVal;
  2. Allocate memory for the structure.

    Note  The size of dwSize is not sufficient to hold the information. See the next step.

     

    Copy Code
     pIPAddrTable = (MIB_IPADDRTABLE*) malloc( sizeof(MIB_IPADDRTABLE) );
  3. Make an initial call to GetIpAddrTable to get the size needed into the dwSize variable.

    Note  This call to the function is meant to fail, and is used to ensure that the dwSize variable specifies a size sufficient for holding all the information returned to pIPAddrTable. This is a common programming model for data structures and functions of this type.

     

    Copy Code
     if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {    free( pIPAddrTable );    pIPAddrTable = (MIB_IPADDRTABLE *) malloc ( dwSize );}
  4. Make a second call to GetIpAddrTable with general error checking and return its value to the DWORD variable dwRetVal (for more advanced error checking).

     

    Copy Code
     if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) != NO_ERROR ) {     printf("GetIpAddrTable call failed with %d/n", dwRetVal);}
  5. If the call was succesful, access the data from the pIPAddrTable data structure.

     

    Copy Code
     printf("IP Address:         %ld/n", pIPAddrTable->table[0].dwAddr);printf("IP Mask:            %ld/n", pIPAddrTable->table[0].dwMask);printf("IF Index:           %ld/n", pIPAddrTable->table[0].dwIndex);printf("Broadcast Addr:     %ld/n", pIPAddrTable->table[0].dwBCastAddr);printf("Re-assembly size:   %ld/n", pIPAddrTable->table[0].dwReasmSize);

 

Note  The DWORD objects dwAddr and dwMask are returned as numerical values in host byte order, not network byte order. These values are not dotted IP addresses.

 http://msdn2.microsoft.com/en-us/library/aa366306.aspx

Managing IP Addresses Using AddIPAddress and DeleteIPAddress

The AddIPAddress function adds the specified IP address to the specified adapter. The DeleteIPAddress function deletes the specified IP address from the specified adapter.

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 variables for the IPAddr and IPMask structures named iaIPAddress and imIPMask, respectively. These values are simply unsigned integers. Initialize the iaIPAddress and imIPMask variables using the inet_addr function.

     

    Copy Code
     UINT iaIPAddress;UINT imIPMask;iaIPAddress = inet_addr("192.168.0.5");imIPMask    = inet_addr("255.255.255.0");
  2. 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.

     

    Copy Code
     ULONG NTEContext  = 0;ULONG NTEInstance = 0;

    Note  After AddIPAddress has been called successfully, DHCP will be disabled; therefore, functions such as IpReleaseAddress, which require DHCP to be enabled, will not be functional.

  3. Make a call to the AddIPAddress function, checking for general errors and returning its value to the DWORD variable dwRetVal (for more extensive error checking).

     

    Copy Code
     if ( (dwRetVal = AddIPAddress(iaIPAddress, imIPMask, pIPAddrTable->table[0].dwIndex, &NTEContext, &NTEInstance) ) != 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

    • Make a call to the DeleteIPAddress function, passing the NTEContext variable as its parameter and checking for general errors. Return its value to the DWORD variable dwRetVal (for more extensive error checking).

       

      Copy Code
       if ((dwRetVal = DeleteIPAddress(NTEContext)) != NO_ERROR) {    printf("DeleteIPAddress call failed with %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.

http://msdn2.microsoft.com/en-us/library/aa366290.aspx

Managing DHCP Leases Using IpReleaseAddress and IpRenewAddress

The IpReleaseAddress and IpRenewAddress functions are used to release and renew the current Dynamic Host Configuration Protocol (DHCP) lease. It is common to use these two functions together, first releasing the lease, then renewing it.

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.
    Copy Code
     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.

     

    Copy Code
     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 hasn't been declared. It is assumed that the variable returned by GetInterfaceInfo is called pInfo.

     

    Copy Code
     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).

     

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