Click here to Skip to main content
13,042,819 members (78,622 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 7 Apr 2009

How to use NotifyAddrChange in C#

, 7 Apr 2009
Rate this:
Please Sign up or sign in to vote.
A C# class designed to facilitate the use of NotifyAddrChange through PInvoke.


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.


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.

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

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

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:

private void Start_Click(object sender, EventArgs e)
  if (m_oNotifyAddrChange == null)
    m_oNotifyAddrChange = new CNotifyAddrChange();
    m_oNotifyAddrChange.AddrChangedEvent += OnAddrChangedEvent;

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

private void Stop_Click(object sender, EventArgs e)
    if (m_oNotifyAddrChange != null)

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.

AutoResetEvent AddrChangeEvent = new AutoResetEvent(false);
NativeOverlapped oOverlapped = new NativeOverlapped();
IntPtr pnHandle = IntPtr.Zero;
oOverlapped.EventHandle = 

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
  // An error has occurred


  • 2009-04-07: Initially published.


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


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionCancelIPChangeNotify should be called. Pin
anand choubey17-Sep-11 22:46
memberanand choubey17-Sep-11 22:46 
GeneralLook at System.Net.NetworkInformation Namespace Pin
Benedict Juretko8-Apr-09 0:28
memberBenedict Juretko8-Apr-09 0:28 
GeneralJust use NetworkChange Pin
leeloo9997-Apr-09 22:51
memberleeloo9997-Apr-09 22:51 
GeneralRe: Just use NetworkChange Pin
skorec borec9-Sep-10 7:25
memberskorec 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 7 Apr 2009
Article Copyright 2009 by Ski_Hog
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid