Click here to Skip to main content
Click here to Skip to main content

How to use NotifyAddrChange in C#

, 7 Apr 2009 CPOL
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

Comments and Discussions

QuestionCancelIPChangeNotify should be called. Pinmemberanand choubey17-Sep-11 23:46 
GeneralLook at System.Net.NetworkInformation Namespace PinmemberBenedict Juretko8-Apr-09 1:28 
GeneralJust use NetworkChange Pinmemberleeloo9997-Apr-09 23:51 
GeneralRe: Just use NetworkChange Pinmemberskorec borec9-Sep-10 8:25 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 7 Apr 2009
Article Copyright 2009 by Ski_Hog
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid