VC++監控網絡卡狀態
VC++監控網絡卡狀態
一般情況下,我們只獲取網絡卡IP就夠了,但是今天有個需求,需要監控網絡卡狀態,經查詢,需要使用COM技術實現,核心Class如下:
微軟在WINDOWS VISTA之後提供了一個叫NLA(Network List Manager API)的介面,用於獲取網路狀態變化通知的一個介面。以COM技術實現。
主要匯出的COM介面如下:
IEnumNetworkConnections
IEnumNetworks
INetwork
INetworkConnection
INetworkConnectionEvents
INetworkEvents
INetworkListManager
INetworkListManagerEvents
1
2
3
4
5
6
7
8
9
10
參考Url :
1. https://msdn.microsoft.com/en-us/library/windows/desktop/aa370799(v=vs.85).aspx
2. https://www.codeproject.com/Articles/574446/Using-Network-List-Manager-Cplusplus
程式碼
// Demo1.cpp : 定義控制檯應用程式的入口點。
//
#include "stdafx.h"
#include <windows.h>
#include <netlistmgr.h>
#pragma comment(lib, "ole32.lib")
class MyNetWorkEvent : public INetworkListManagerEvents
{
public:
MyNetWorkEvent() {
m_ref = 1;
}
/*
typedef enum NLM_CONNECTIVITY {
NLM_CONNECTIVITY_DISCONNECTED = 0x0000, // 斷開連線
NLM_CONNECTIVITY_IPV4_NOTRAFFIC = 0x0001, // 不通
NLM_CONNECTIVITY_IPV6_NOTRAFFIC = 0x0002,
NLM_CONNECTIVITY_IPV4_SUBNET = 0x0010,
NLM_CONNECTIVITY_IPV4_LOCALNETWORK = 0x0020,
NLM_CONNECTIVITY_IPV4_INTERNET = 0x0040, // 有網
NLM_CONNECTIVITY_IPV6_SUBNET = 0x0100,
NLM_CONNECTIVITY_IPV6_LOCALNETWORK = 0x0200,
NLM_CONNECTIVITY_IPV6_INTERNET = 0x0400
} NLM_CONNECTIVITY;
ConnectivityChanged: 0003 // 區域網,沒網
ConnectivityChanged: 0000 // 斷開連線
ConnectivityChanged: 0042 // 區域網,能上網
*/
virtual HRESULT STDMETHODCALLTYPE ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
{
printf("ConnectivityChanged: %04X\n", newConnectivity);
return S_OK;
}
virtual 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;
}
virtual ULONG STDMETHODCALLTYPE AddRef(void)
{
return (ULONG)InterlockedIncrement(&m_ref);
}
virtual ULONG STDMETHODCALLTYPE Release(void)
{
LONG Result = InterlockedDecrement(&m_ref);
if (Result == 0)
delete this;
return (ULONG)Result;
}
private:
LONG m_ref;
};
int main()
{
CoInitialize(NULL);
INetworkListManager *pNetworkListManager = NULL;
HRESULT hr = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_INetworkListManager, (LPVOID *)&pNetworkListManager);
printf("CLSID_NetworkListManager: %d\n", FAILED(hr));
VARIANT_BOOL bConnected = VARIANT_FALSE;
hr = pNetworkListManager->get_IsConnected(&bConnected);
printf("is Connected: %d %d\n", bConnected, FAILED(hr));
IConnectionPointContainer *pCPContainer = NULL;
hr = pNetworkListManager->QueryInterface(IID_IConnectionPointContainer, (void**)&pCPContainer);
IConnectionPoint *pConnectPoint = NULL;
hr = pCPContainer->FindConnectionPoint(IID_INetworkListManagerEvents, &pConnectPoint);
DWORD Cookie = 0;
MyNetWorkEvent* pMyEvent = new MyNetWorkEvent();
pConnectPoint->Advise((IUnknown*)pMyEvent, &Cookie);
// 必須有下面這個訊息迴圈
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();
system("pause");
CoUninitialize();
return 0;
}
---------------------
作者:笨笨D幸福
來源:CSDN
原文:https://blog.csdn.net/bbdxf/article/details/79002833
版權宣告:本文為博主原創文章,轉載請