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.
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.
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
object as shown below. Ensure you have called
CoInitialize() before making these calls.
CComPtr <INetworkListManager> m_pNLM;
Once you created the
CLSID_NetworkListManager object, you can enumerate all connected networks as shown below.
DWORD dwReturn = 0;
hr = pEnumNetworks->Next(1, &pNetwork, &dwReturn);
if(hr == S_OK && dwReturn > 0)
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
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
STDMETHODIMP QueryInterface (REFIID riid, void** pIFace);
STDMETHODIMP ConnectivityChanged(NLM_CONNECTIVITY NewConnectivity);
After creating the Sink Object we can establish a connection to the connection point as follows:
CoNetworkEventHandler * m_pSink;
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;
(WPARAM) bInternet, 0);