dcsimg
October 19, 2017
Hot Topics:

IP Helper API: Managing IP Addresses

  • September 21, 2004
  • By Alex Gusev
  • Send Email »
  • More Articles »

Adding and Deleting IP Addresses

The IP table we got in the previous section is read-only. Nevetheless, you may add or remove an IP address for the desired adapter. You have AddIPAddress and DeleteIPAddress functions to carry it out. It's important to note that the IP address created by AddIPAddress is not persistent. This address exists only as long as the adapter object exists. It is destroyed after a "soft" (and obviously after a "cold") reset of the PDA. After you have successfully created a new IP and assigned it to an adapter, you can remove it by a DeleteIPAddress call. The corresponding code is presented below:

void CNWParamsDlg::OnAddIP()
{
   IPAddr Address = inet_addr("10.0.0.1");
   IPMask IpMask = 0xFFFFFFFF;
   ULONG NTEInstance = 0;
   DWORD IfIndex = 2;

   DWORD dwRes = AddIPAddress(Address,IpMask,IfIndex,&m_NTEContext,
                              &NTEInstance);
   if ( dwRes == NO_ERROR )
   {
      TRACE(L"OK\n");
   }
   else
   {
      TRACE(L"Error: %X\n", dwRes);
   }
}

void CNWParamsDlg::OnDelIP()
{
   DWORD dwRes = DeleteIPAddress(m_NTEContext);
   if ( dwRes == NO_ERROR )
   {
      TRACE(L"OK\n");
   }
   else
   {
      TRACE(L"Error: %X\n", dwRes);
   }
}

Facing DHCP

A PDA may have either a static IP address or dynamically obtain it from a DHCP server. In the last case, your application is able to release its current IP or renew it by calling IpReleaseAddress and IpRenewAddress, respectively. These are pretty simple operations:

void CNWParamsDlg::OnReleaseIP()
{
   IP_ADAPTER_INDEX_MAP AdapterInfo;
   memset(&AdapterInfo,0,sizeof(AdapterInfo));
   AdapterInfo.Index = 2;

   DWORD dwRes = IpReleaseAddress(&AdapterInfo);
   m_ListBox.ResetContent();
   if ( dwRes == NO_ERROR )
   {
      TRACE(L"OK\n");
      m_ListBox.AddString(L"OK");
   }
   else
   {
      CString sErr;
      sErr.Format(L"Error: %X", dwRes);
      TRACE(sErr + L"\n");
      m_ListBox.AddString(sErr);
   }
}

void CNWParamsDlg::OnRenewIP()
{
   IP_ADAPTER_INDEX_MAP AdapterInfo;
   memset(&AdapterInfo,0,sizeof(AdapterInfo));
   AdapterInfo.Index = 2;

   DWORD dwRes = IpRenewAddress(&AdapterInfo);
   m_ListBox.ResetContent();
   if ( dwRes == NO_ERROR )
   {
      TRACE(L"OK\n");
      m_ListBox.AddString(L"OK");
   }
   else
   {
      CString sErr;
      sErr.Format(L"Error: %X", dwRes);
      TRACE(sErr + L"\n");
      m_ListBox.AddString(sErr);
   }
}




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel