Skip to content

Instantly share code, notes, and snippets.

@eahydra
Created September 19, 2012 05:12
Show Gist options
  • Save eahydra/3747800 to your computer and use it in GitHub Desktop.
Save eahydra/3747800 to your computer and use it in GitHub Desktop.
network change
class CNetworkListManagerEvent : public INetworkListManagerEvents
{
public:
CNetworkListManagerEvent() : m_ref(1)
{
}
~CNetworkListManagerEvent()
{
}
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject)
{
HRESULT Result = S_OK;
if (IsEqualIID(riid, IID_IUnknown))
{
*ppvObject = (IUnknown *)this;
}
else if (IsEqualIID(riid ,IID_INetworkListManagerEvents))
{
*ppvObject = (INetworkListManagerEvents *)this;
}
else
{
Result = E_NOINTERFACE;
}
return Result;
}
ULONG STDMETHODCALLTYPE AddRef()
{
return (ULONG)InterlockedIncrement(&m_ref);
}
ULONG STDMETHODCALLTYPE Release()
{
LONG Result = InterlockedDecrement(&m_ref);
if (Result == 0)
delete this;
return (ULONG)Result;
}
virtual HRESULT STDMETHODCALLTYPE ConnectivityChanged(
/* [in] */ NLM_CONNECTIVITY newConnectivity)
{
return S_OK;
}
private:
LONG m_ref;
};
int _tmain(int argc, TCHAR** argv, TCHAR** Env)
{
CoInitialize(NULL);
//
// 通过NLA接口获取网络状态
//
IUnknown *pUnknown = NULL;
HRESULT Result = CoCreateInstance(CLSID_NetworkListManager,
NULL, CLSCTX_ALL, IID_IUnknown, (void **)&pUnknown);
if (SUCCEEDED(Result))
{
INetworkListManager *pNetworkListManager = NULL;
Result = pUnknown->QueryInterface(IID_INetworkListManager,
(void **)&pNetworkListManager);
if (SUCCEEDED(Result))
{
VARIANT_BOOL IsConnect = VARIANT_FALSE;
Result = pNetworkListManager->get_IsConnectedToInternet(&IsConnect);
if (SUCCEEDED(Result))
{
printf("IsConnect Result %s\n", IsConnect == VARIANT_TRUE ? "TRUE" : "FALSE");
}
IConnectionPointContainer *pCPContainer = NULL;
Result = pNetworkListManager->QueryInterface(IID_IConnectionPointContainer,
(void **)&pCPContainer);
if (SUCCEEDED(Result))
{
IConnectionPoint *pConnectPoint = NULL;
Result = pCPContainer->FindConnectionPoint(IID_INetworkListManagerEvents,
&pConnectPoint);
if(SUCCEEDED(Result))
{
DWORD Cookie = NULL;
CNetworkListManagerEvent *NetEvent = new CNetworkListManagerEvent;
Result = pConnectPoint->Advise((IUnknown *)NetEvent, &Cookie);
if (SUCCEEDED(Result))
{
printf("Loop Message\n");
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
{
break;
}
}
pConnectPoint->Unadvise(Cookie);
pConnectPoint->Release();
}
}
pCPContainer->Release();
}
pNetworkListManager->Release();
}
pUnknown->Release();
}
CoUninitialize();
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment