Click here to Skip to main content
13,550,486 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


25 bookmarked
Posted 31 Mar 2009
Licenced CPOL

How to use the Windows NLM API to get notified of new network connectivity

, 31 Mar 2009
Rate this:
Please Sign up or sign in to vote.
Use the Windows NLM API to tell if a machine has internet connectivity.


An application often needs to know if the machine has internet connectivity and take actions depending on that. In this sample, we are looking at the usage of the Windows NLM API in managed code so that an application can choose to respond to internet connectivity changes. There are many other specific NLM APIs for checking domain connectivity, network adapter interfaces etc., that haven't been mentioned in this article; you can refer to this link for further details. The downloadable zip file has the source code.

Using the code

For a managed code project, you need to reference the netprofm.dll in the \windows\system32\ directory. Here, m_nlm is a NetworkListManager COM object created to provide NLM functionalities. In the Main function, we can get an enumerator of all the networks and try to list their names one by one. You can use the API to list the properties of each network. Your machine might be connected to a free library hotspot, and it might also be connected to a corporate network via VPN or DirectAccess. In this case, there will be two networks listed, one with public internet access and the other with domain network access.

    m_nlm = new NetworkListManager();
static void Main(string[] args)
    AppNetworkListUser nlmUser = new AppNetworkListUser();
    Console.WriteLine("Is the machine connected to internet? " + 
    //List the connected networks. There are many other APIs 
    //can be called to get network information.
    IEnumNetworks Networks = 
    foreach (INetwork item in Networks)
        Console.WriteLine("Connected Network:" + item.GetName() );
    Console.WriteLine("Press any key and enter to finish the program");
    String temp; 
    temp = Console.ReadLine();

In the Main function, we also used AdviseforNetworklistManager to subscribe to any network connectivity changes for the machine, so that your ConnectivityChanged(NLM_CONNECTIVITY) method will be invoked if there is any network connectivity change. This is the method that you could use to take some actions if your machine gets connected to the internet. Before your application leaves the scene, you need to unsubscribe the event notifications about network connectivity changes. The event subscription method will find the connection point to get the INetworkListManager event as in the code below:

public void AdviseforNetworklistManager()
    Console.WriteLine("Subscribing the INetworkListManagerEvents");
    IConnectionPointContainer icpc = (IConnectionPointContainer)m_nlm;
    //similar event subscription can be used 
    //for INetworkEvents and INetworkConnectionEvents
    Guid tempGuid = typeof(INetworkListManagerEvents).GUID;
    icpc.FindConnectionPoint(ref tempGuid , out m_icp);
    m_icp.Advise(this, out m_cookie);

In ConnectivityChanged(NLM_CONNECTIVITY), we provide a sample to output the current machine's internet connectivity to the Console. Your app can take custom actions such as start downloading new data when connected to the internet etc.

public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
    Console.WriteLine("-------------------------------------------" + 
    Console.WriteLine("NetworkList just informed the connectivity change." + 
                      " The new connectivity is:");
        Console.WriteLine("The machine is disconnected from Network");
    if (((int)newConnectivity & 
        Console.WriteLine("The machine is connected to internet " + 
                          "with IPv4 capability ");
    if (((int)newConnectivity & 
        Console.WriteLine("The machine is connected to internet " + 
                          "with IPv6 capability ");
    if ((((int)newConnectivity & 
          (((int)newConnectivity & 
        Console.WriteLine("The machine is not connected to internet yet ");


Here is the output after we unplug the Ethernet cable from the computer and plug it back again. You can see the app get notified of the internet connectivity changes:



  • 0.1 - Added a brief description of the NLM API sample.


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

QuestionInterop.NETWORKLIST.dll is generated Pin
Stéphane Lenclud9-Feb-15 5:25
memberStéphane Lenclud9-Feb-15 5:25 
QuestionXP Not Supported Pin
Member 229746523-Jan-14 3:15
memberMember 229746523-Jan-14 3:15 
QuestionDetect cable plug Pin
Marco Fragnoli29-Oct-13 4:09
memberMarco Fragnoli29-Oct-13 4:09 
QuestionVery good... Pin
pdoxtader24-Sep-13 3:39
memberpdoxtader24-Sep-13 3:39 
Question10 on 10 Pin
rajeev tapadar21-Feb-13 18:24
memberrajeev tapadar21-Feb-13 18:24 
GeneralMy vote of 5 Pin
CuteD34th4-Dec-12 1:41
memberCuteD34th4-Dec-12 1:41 
QuestionThank you Pin
ShabanaParveen1-Oct-12 1:00
memberShabanaParveen1-Oct-12 1:00 
GeneralMy vote of 5 Pin
ShabanaParveen1-Oct-12 0:54
memberShabanaParveen1-Oct-12 0:54 
GeneralMy vote of 5 Pin
lansoprazole6-Nov-11 5:47
memberlansoprazole6-Nov-11 5:47 
GeneralUse the Windows API Code Pack for Microsoft .NET Framework to access Network List Manager (NLM) from Managed code Pin
Curtis Gray5-Mar-11 10:43
memberCurtis Gray5-Mar-11 10:43 
GeneralThe .NET Framwork already supports this Pin
Ami Bar1-Apr-09 2:10
memberAmi Bar1-Apr-09 2:10 
RantRe: The .NET Framwork already supports this Pin
Mark Berndt14-May-09 6:50
memberMark Berndt14-May-09 6:50 

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.180515.1 | Last Updated 31 Mar 2009
Article Copyright 2009 by ryhuang
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid