Click here to Skip to main content
Click here to Skip to main content
Go to top

Using Network List Manager (C++)

, 8 Apr 2013
Rate this:
Please Sign up or sign in to vote.
This article explains how to use Network List Manager service to identify and retrieve properties of each network that PC Connects to.

Introduction 

The sample application enumerates the networks to which the PC is connected and show some of the properties of the network selected. It also listens for the connection status and updates if connection changed.

Background 

Network List Manager runs as a service in the context of Svchost.exe and is started during the computer boot procedure. The Network List Manager service maintains a table of available networks and network attributes that are retrieved by applications through the Network List Manager API.  INetworkListManager is the interface we are using to communicate with the Network List Manager Service for Querying the Network details.

Note: INetworkListManager is introduced in Windows Vista.  

Using the code  

The sample Application is developed as an MFC dialog app, using Visual Studio 2012.  INetworkListManager is implemented on the CLSID_NetworkListManager CoClass. In order to use this interface, it is necessary to create the CLSID_NetworkListManager COM object as shown below. Ensure you have called CoInitialize() before making these calls.

CComPtr <INetworkListManager> m_pNLM;
m_pNLM.CoCreateInstance(CLSID_NetworkListManager); 

Once you created the CLSID_NetworkListManager object, you can enumerate all connected networks as shown below. 

CComPtr<IEnumNetworks> pEnumNetworks;
if(SUCCEEDED(m_pNLM->GetNetworks(NLM_ENUM_NETWORK_CONNECTED, &pEnumNetworks)))
{
    DWORD dwReturn = 0;
    while(true)
    {
       CComPtr<INetwork> pNetwork;
       hr = pEnumNetworks->Next(1, &pNetwork, &dwReturn);
       if(hr == S_OK && dwReturn > 0)
       {
           Dump_NW_Info(pNetwork);
       }
       else
       {
        break;
       }
    }
}

The INetwork interface represents a network on the local machine. It can also represent a collection of network connections with a similar network signature. You can get the Category, Connectivity types, Description, Domain Type and Name of the network using the GET methods of INetwork interface. Dump_NW_Info in the sample collects all these information. The INetwork interface has a property called get_IsConnectedToInternet, which specifies if the network has internet connectivity. 

Listening for Connection Events 

INetworkListManagerEvents is a message sink interface that a client implements to get overall machine state related events. Applications that are interested on higher-level events, for example internet connectivity, implement this interface.  This interface inherits from IUnknown interface and has one method.  

HRESULT STDMETHODCALLTYPE ConnectivityChanged( NLM_CONNECTIVITY NewConnectivity );

We implemented our Sink class as follows:

class CoNetworkEventHandler : public INetworkListManagerEvents
{
  public:
    CoNetworkEventHandler(void);
    virtual ~CoNetworkEventHandler(void);
    STDMETHODIMP QueryInterface (REFIID riid, void** pIFace);
    STDMETHODIMP_(ULONG) AddRef();
    STDMETHODIMP_(ULONG) Release();
    STDMETHODIMP ConnectivityChanged(NLM_CONNECTIVITY NewConnectivity);
    
  private:
    long m_lRefCnt;
    DWORD m_dwCookie;
};

After creating the Sink Object we can establish a connection to the connection point as follows:

CoNetworkEventHandler * m_pSink;
LPUNKNOWN m_pUnkSink; 
m_pSink = new CoNetworkEventHandler(); 
if (SUCCEEDED (m_pSink->QueryInterface(IID_IUnknown, (void**) &m_pUnkSink)))
{
    AfxConnectionAdvise (m_pNLM, __uuidof(INetworkListManagerEvents),
                             m_pUnkSink, FALSE, &m_dwCookie); 
}

Once a network event occurs this call back function will get called. 

HRESULT CoNetworkEventHandler::ConnectivityChanged( NLM_CONNECTIVITY NewConnectivity)
{
    bool bInternet = false;
    if((NewConnectivity & NLM_CONNECTIVITY_IPV4_INTERNET) || 
       (NewConnectivity & NLM_CONNECTIVITY_IPV4_INTERNET))
    {
        bInternet = true;
    }
    PostMessage(AfxGetApp()->GetMainWnd()->m_hWnd, UM_NETWORK_NOTIFY, 
                (WPARAM) bInternet, 0);
    return S_OK;
}

License

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

Share

About the Author

Jithesh Chandrasekharan
Technical Lead
United States United States
No Biography provided

Comments and Discussions

 
QuestionConcern with Virtual Private Network PinmemberDebajyotiSahu14-May-14 18:02 
QuestionGood one! PinmemberRejeesh.T.S16-Feb-14 23:48 
QuestionThanks Pinmembermarc ochsenmeier9-Dec-13 21:38 
QuestionFailed to include <Afxwin.h> PinmemberHamidYaseen9-Apr-13 3:06 
AnswerRe: Failed to include <Afxwin.h> PinmemberJithesh Chandrasekharan9-Apr-13 3:19 
QuestionWIFI Connection Pinmemberfmaeseel9-Apr-13 1:09 
AnswerRe: WIFI Connection PinmemberJithesh Chandrasekharan9-Apr-13 7:19 
GeneralMy vote of 4 PinprofessionalSanjay K. Gupta9-Apr-13 0:23 
QuestionVC6 project ? PinmemberFlaviu28-Apr-13 20:17 
AnswerRe: VC6 project ? PinmemberJithesh Chandrasekharan9-Apr-13 3:23 

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 | Mobile
Web03 | 2.8.140922.1 | Last Updated 8 Apr 2013
Article Copyright 2013 by Jithesh Chandrasekharan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid