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;
}