All Forums
 Microsoft Windows CE
 eVC Application Development
 (C,VB.NET) Programmatically release/renew IP addr
 Forum Locked
 Send Topic to a Friend
 Printer Friendly
Author Topic  

ctacke

877 Posts

Posted - 01 Apr 2003 :  10:57:32  Show Profile  Email Poster
Q: I can release or renew my device's IP address by using ipconfig /release from the command line. How do I programmatically do the same thing?

The following code will do what you're after. You will also need to write a function called OutputMessage or tweak the code.


#include "iphlpapi.h"

void RenewAddress(DWORD AdapterIndex)
{
  ULONG sizeofbuffer=0;
  PIP_INTERFACE_INFO pbuffer=NULL;
  int returnvalue;
  BOOL bfound =FALSE;

  returnvalue= GetInterfaceInfo(pbuffer, &sizeofbuffer);
  if (returnvalue==ERROR_INSUFFICIENT_BUFFER)
  {
    pbuffer=(PIP_INTERFACE_INFO) malloc(sizeofbuffer);
    if (pbuffer != NULL)
    {
      returnvalue=GetInterfaceInfo(pbuffer, &sizeofbuffer);
    }
  }
  if (returnvalue!=NO_ERROR)
  {
    // error
    exit(1);
  }

  for (int index=0;indexNumAdapters; index++)
  {
    if ((pbuffer->Adapter[index].Index==AdapterIndex)||(AdapterIndex==0))
    {
      bfound =TRUE;

      int returnvalue=IpRenewAddress(&(pbuffer->Adapter[index]));
      if (returnvalue==NO_ERROR)
        OutputMessage(TEXT("Successfully Renewed Adapter with Index Number %lu\n"),
        pbuffer->Adapter[index].Index);
      else {
        OutputMessage(TEXT("Failed to renew Adpter with Index Number %lu\n"),
        pbuffer->Adapter[index].Index);
      }
    }

  }
  if (!bfound)
    OutputMessage(TEXT("Adapter with given Index Number not found\n"));

  if (pbuffer)
    free(pbuffer);
}


void ReleaseAddress( DWORD AdapterIndex)
{
  BOOL bfound =FALSE;
  ULONG sizeofbuffer=0;
  PIP_INTERFACE_INFO prelease=NULL;
  int returnvalue;

  returnvalue= GetInterfaceInfo(prelease, &sizeofbuffer);
  if (returnvalue==ERROR_INSUFFICIENT_BUFFER)
  {
    prelease=(PIP_INTERFACE_INFO) malloc(sizeofbuffer);
    if (prelease != NULL) {
      returnvalue=GetInterfaceInfo(prelease, &sizeofbuffer);
    }
  }
  if (returnvalue!=NO_ERROR) {
    // error
    exit(1);
  }

  for (int index=0;indexNumAdapters; index++) {
    if ((AdapterIndex==0) || (prelease->Adapter[index].Index==AdapterIndex)) {
      bfound=TRUE;

      DWORD returnvalue=IpReleaseAddress(&(prelease->Adapter[index]));
      if (returnvalue==NO_ERROR)
        OutputMessage(TEXT("Successfully Released adapter with index Number %lu\n"),
        prelease->Adapter[index].Index);
      else {
        OutputMessage(TEXT("Failed to release Adapter with index Number %lu\n"),
        prelease->Adapter[index].Index);
      }
    }
  }

  if (!bfound)
    OutputMessage(TEXT("Adapter with the given index Number not found\n"));

  if (prelease)
    free(prelease);
}

ctacke

877 Posts

Posted - 07 Apr 2003 :  15:03:00  Show Profile  Email Poster
This code also requires that you link to IPHLPAPI.LIB which is included in our CE.NET SDK
Go to Top of Page

wtang

17 Posts

Posted - 11 Jul 2003 :  12:27:25  Show Profile  Email Poster
How to determine "indexNumAdapters". BTW, I run this code and change "OutputMessage" into "RETAILMSG". The system crashes when I call "ReleaseAddress" function.
The target board I'm running is ADS AGX build 4.10.00
Go to Top of Page

ggitsme

3 Posts

Posted - 31 Jan 2005 :  16:09:17  Show Profile
Is this library available for Windows CE 3.0. I am using an ADS GC+ with the hpc2000 sdk ?
Go to Top of Page

ken.yeung

20 Posts

Posted - 04 Mar 2005 :  15:39:45  Show Profile  Email Poster
I am working on CE 4.20.08 and 4.20.09 on AGX with VB.NET. Do I need to install the CE.NET SDK to use the function IpRenewAddress in IPHLPAPI.LIB?
Go to Top of Page

ctacke

877 Posts

Posted - 04 Mar 2005 :  15:56:11  Show Profile  Email Poster
No. SDKs simply provide headers and LIBs for compiling and linking C/C++ applications. From managed code you'll be P/Invoking, which will load the library at run time based on the method signature you provide in your code.
Go to Top of Page

ken.yeung

20 Posts

Posted - 04 Mar 2005 :  16:44:40  Show Profile  Email Poster
Thanks, Chris. I was testing the function IpRenewAddress and got the NotSupportedException right at the statement invoking IpRenewAddress(map). Any idea to fix this? The code is attached:


############################################
Imports System.Runtime.InteropServices

Module Module1

Sub Main()
Console.WriteLine("Begin...")
Dim map As New IP_ADAPTER_INDEX_MAP
map.Index = 2
map.Name = "SMC90001"

Dim result As Integer = _
IpRenewAddress(map)

Console.WriteLine("Result = " & result)
Console.ReadLine()

End Sub


Public Structure IP_ADAPTER_INDEX_MAP
Public Index As Integer
Public Name As String
End Structure


Declare Function IpRenewAddress Lib "Iphlpapi.lib" (ByRef AdapterInfo As IP_ADAPTER_INDEX_MAP) As Integer

End Module
####################################################

Go to Top of Page

ken.yeung

20 Posts

Posted - 04 Mar 2005 :  17:48:56  Show Profile  Email Poster
I have solved the problem by reading this: http://msdn.microsoft.com/mobility/understanding/articles/default.aspx?pull=/library/en-us/dnnetcomp/html/netcfadvinterop.asp

However, I can use IpReleaseAddress to release the IP address but when I call IpRenewAddress I cannot get back the IP address. Any idea?
Go to Top of Page

ctacke

877 Posts

Posted - 04 Mar 2005 :  17:52:30  Show Profile  Email Poster
Just looking at it, your definition of IP_ADAPTER_INDEX_MAP is wrong. Name is not a string, it's an array of 260 (MAX_PATH) TCHARs. You'll need to define it like this:

Public Structure IP_ADAPTER_INDEX_MAP
Public Index As Integer
Public Name As Byte(520)
End Structure

Then use Encoding.Unicode to change the array to a string.
Go to Top of Page

ken.yeung

20 Posts

Posted - 18 Mar 2005 :  15:00:40  Show Profile  Email Poster
Sorry, I didn't finish the previous reply!

I did the followings:


Dim ac As AdapterCollection = OpenNETCF.Net.Networking.GetAdapters

For Each ad As Adapter In ac
Dim map As New IP_ADAPTER_INDEX_MAP
map.Index = ad.Index
Dim coding As New ASCIIEncoding
map.name = Encoding.Convert(Encoding.ASCII, Encoding.Unicode, coding.GetBytes(ad.Name.PadRight(260, Chr(0))))

Dim result As Integer
result = IpReleaseAddress(map)
...
result = IpRenewAddress(map)
...
Next
....
Public Structure IP_ADAPTER_INDEX_MAP
Public Index As Integer
<VBFixedArray(519)> Public name() As Byte
End Structure


The MissingMethodException was thrown at IpReleaseAddress.

However if I use the StringPtr described in the msdn webpage above for the name in IP_ADAPTER_INDEX_MAP, no exception was thrown; but I got result=C0000001 (Win32Exception) for IpReleaseAddress and result=0 (Success) for IpRenewAddress (I was wrong in the previous reply). However even it returns success for IpRenewAddress, the address is not renewed.


Go to Top of Page

ken.yeung

20 Posts

Posted - 18 Mar 2005 :  15:04:00  Show Profile  Email Poster
I put '<VBFixedArray(519)>' before 'Public name() As Byte' in the code provided in the above message. But it was not displayed after I posted the message.
Go to Top of Page

ken.yeung

20 Posts

Posted - 18 Mar 2005 :  15:07:17  Show Profile  Email Poster
I am sorry that the VBFixedArray(519) attribute (before Public name() As Byte) was not shown in the above two messages since the asp did not accept a pair of greater than and less than symbols.
Go to Top of Page

akidder

1519 Posts

Posted - 18 Mar 2005 :  15:25:41  Show Profile  Email Poster
Yeah, the forums will eat characters that look like HTML. I've reformatted your posts above to maintain the formatting you provided (pre directive) and put in the HTML characters to display the greater than and less than symbols (> and <).
Go to Top of Page
  Topic  
 Forum Locked
 Send Topic to a Friend
 Printer Friendly
Jump To:
Eurotech Support Forums © Eurotech Inc. Go To Top Of Page
This page was generated in 0.05 seconds. Snitz Forums 2000