Click here to Skip to main content
15,860,844 members
Articles / Desktop Programming / MFC

Using Network List Manager (C++)

Rate me:
Please Sign up or sign in to vote.
4.81/5 (13 votes)
8 Apr 2013CPOL2 min read 58.2K   3.2K   46   15
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.  

Image 1

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.

C++
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. 

C++
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.  

C++
HRESULT STDMETHODCALLTYPE ConnectivityChanged( NLM_CONNECTIVITY NewConnectivity );

We implemented our Sink class as follows:

C++
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:

C++
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. 

C++
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)


Written By
Technical Lead
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

 
GeneralMy vote of 5 Pin
rp_suman3-Jul-19 19:48
rp_suman3-Jul-19 19:48 
QuestionRedundant code Pin
rp_suman3-Jul-19 19:46
rp_suman3-Jul-19 19:46 
QuestionRunning this in console App Pin
Avinash Pachar27-Sep-16 1:49
Avinash Pachar27-Sep-16 1:49 
AnswerRe: Running this in console App Pin
Member 1385684227-Jun-18 15:25
Member 1385684227-Jun-18 15:25 
QuestionConcern with Virtual Private Network Pin
DebajyotiSahu14-May-14 18:02
DebajyotiSahu14-May-14 18:02 
QuestionGood one! Pin
KingsGambit16-Feb-14 23:48
KingsGambit16-Feb-14 23:48 
QuestionThanks Pin
marc ochsenmeier9-Dec-13 21:38
marc ochsenmeier9-Dec-13 21:38 
QuestionFailed to include <Afxwin.h> Pin
HamidYaseen9-Apr-13 3:06
professionalHamidYaseen9-Apr-13 3:06 
AnswerRe: Failed to include <Afxwin.h> Pin
Jithesh Chandrasekharan9-Apr-13 3:19
Jithesh Chandrasekharan9-Apr-13 3:19 
QuestionWIFI Connection Pin
fmaeseel9-Apr-13 1:09
fmaeseel9-Apr-13 1:09 
AnswerRe: WIFI Connection Pin
Jithesh Chandrasekharan9-Apr-13 7:19
Jithesh Chandrasekharan9-Apr-13 7:19 
GeneralMy vote of 4 Pin
Sanjay K. Gupta9-Apr-13 0:23
professionalSanjay K. Gupta9-Apr-13 0:23 
QuestionVC6 project ? Pin
_Flaviu8-Apr-13 20:17
_Flaviu8-Apr-13 20:17 
AnswerRe: VC6 project ? Pin
Jithesh Chandrasekharan9-Apr-13 3:23
Jithesh Chandrasekharan9-Apr-13 3:23 
SuggestionRe: VC6 project ? Pin
BMW7408-Mar-18 23:42
BMW7408-Mar-18 23:42 

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.