Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

How to use NotifyAddrChange in C#

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 Apr 2009CPOL1 min read 31.3K   620   12   4
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCancelIPChangeNotify should be called. Pin
anand choubey17-Sep-11 22:46
anand choubey17-Sep-11 22:46 
GeneralLook at System.Net.NetworkInformation Namespace Pin
bjuretko8-Apr-09 0:28
bjuretko8-Apr-09 0:28 
GeneralJust use NetworkChange Pin
leeloo9997-Apr-09 22:51
leeloo9997-Apr-09 22:51 
GeneralRe: Just use NetworkChange Pin
skorec borec9-Sep-10 7:25
skorec borec9-Sep-10 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.