Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

How to use NotifyAddrChange in C#

0.00/5 (No votes)
7 Apr 2009CPOL1 min read 31.9K   623  
A C# class designed to facilitate the use of NotifyAddrChange through PInvoke.

Introduction

The CNotifyAddrChange C# class uses a Thread, AutoResetEvents, and an Event Handler in conjunction with the Microsoft's IP Helper API (Iphlpapi.dll) NotifyAddrChange function to notify you if an IP address has changed.

The demo download is a VS 2008 C# Windows Forms application that demonstrates the use of the CNotiyAddrChange class.

Background

Being fairly new to the C# / .NET world, I knew I wanted to use the Iphlpapi.dll NotifyAddrChange function to find out when an IP address has changed and how to do it in C++, but I could not find anything on how to do it in C#. The CNotifyAddrChange class represents my solution to the problem.

Using the code

Declare an instance of the CNotifyAddrChange object in the scope within you want it to survive.

C#
public partial class Main : Form
{
  protected CNotifyAddrChange m_oNotifyAddrChange = null;

Create the EventHandler you want called when an IP address has changed.

C#
public void OnAddrChangedEvent(object sender, EventArgs e)
{
    // An IP Address has changed, so do something
}

The class will auto start when it is allocated, and it has a Start() procedure which you can call to start it manually:

C#
private void Start_Click(object sender, EventArgs e)
{
  if (m_oNotifyAddrChange == null)
  {
    m_oNotifyAddrChange = new CNotifyAddrChange();
    m_oNotifyAddrChange.AddrChangedEvent += OnAddrChangedEvent;
  }
  else
  {
    m_oNotifyAddrChange.Start();
  }
}

There is a Stop() procedure that you can use to stop it from monitoring for changes.

C#
private void Stop_Click(object sender, EventArgs e)
{
    if (m_oNotifyAddrChange != null)
    {
      m_oNotifyAddrChange.Stop();
    }
}

Points of interest

Tricks to calling the NotifyAddrChange API function

The NotifyAddrChange function takes a pointer to an OVERLAPPED object. Rather than re-creating this structure yourself, it is recommended that you use the .NET NativeOverlapped class. Then, you have to set the EventHandle inside of the OVERLAPPED object, which is an IntPtr. There is no proper cast from the AddrChangeEvent, so the best way to get the IntPtr was to use the AutoResetEvent::SaveWaitHandle.DangerousGetHandle() function.

C#
AutoResetEvent AddrChangeEvent = new AutoResetEvent(false);
NativeOverlapped oOverlapped = new NativeOverlapped();
IntPtr pnHandle = IntPtr.Zero;
oOverlapped.EventHandle = 
  AddrChangeEvent.SafeWaitHandle.DangerousGetHandle();

UInt32 nRetVal = NotifyAddrChange(ref pnHandle, ref oOverlapped);
if(nRetVal == 997)
// 997 == ERROR_IO_PENDING, means that
// it will notify us when an IP Address changes
{
  // Wait for the event to signal
}
else
{
  // An error has occurred
}

History

  • 2009-04-07: Initially published.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)