• 检测Windows电脑网络是否在线、离线方法(c++)-->ConnectivityChanged


    1 方法:调用Windows的com接口,

            CLSID_NetworkListManager

            IID_INetworkListManager

            IID_IConnectionPointContainer

            IID_INetworkListManagerEvents

    2 代码:如下:

    1. // testNetStatus.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    2. //
    3. #include
    4. #include
    5. #include
    6. #pragma comment(lib, "ole32.lib")
    7. class MyNetWorkEvent : public INetworkListManagerEvents
    8. {
    9. public:
    10. MyNetWorkEvent() {
    11. m_ref = 1;
    12. }
    13. /*
    14. typedef enum NLM_CONNECTIVITY {
    15. NLM_CONNECTIVITY_DISCONNECTED = 0x0000, // 断开连接
    16. NLM_CONNECTIVITY_IPV4_NOTRAFFIC = 0x0001, // 不通
    17. NLM_CONNECTIVITY_IPV6_NOTRAFFIC = 0x0002,
    18. NLM_CONNECTIVITY_IPV4_SUBNET = 0x0010,
    19. NLM_CONNECTIVITY_IPV4_LOCALNETWORK = 0x0020,
    20. NLM_CONNECTIVITY_IPV4_INTERNET = 0x0040, // 有网
    21. NLM_CONNECTIVITY_IPV6_SUBNET = 0x0100,
    22. NLM_CONNECTIVITY_IPV6_LOCALNETWORK = 0x0200,
    23. NLM_CONNECTIVITY_IPV6_INTERNET = 0x0400
    24. } NLM_CONNECTIVITY;
    25. ConnectivityChanged: 0003 // 局域网,没网
    26. ConnectivityChanged: 0000 // 断开连接
    27. ConnectivityChanged: 0042 // 局域网,能上网
    28. */
    29. virtual HRESULT STDMETHODCALLTYPE ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
    30. {
    31. printf("ConnectivityChanged: %04X\n", newConnectivity);
    32. return S_OK;
    33. }
    34. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject)
    35. {
    36. HRESULT Result = S_OK;
    37. if (IsEqualIID(riid, IID_IUnknown))
    38. {
    39. *ppvObject = (IUnknown *)this;
    40. }
    41. else if (IsEqualIID(riid, IID_INetworkListManagerEvents))
    42. {
    43. *ppvObject = (INetworkListManagerEvents *)this;
    44. }
    45. else
    46. {
    47. Result = E_NOINTERFACE;
    48. }
    49. return Result;
    50. }
    51. virtual ULONG STDMETHODCALLTYPE AddRef(void)
    52. {
    53. return (ULONG)InterlockedIncrement(&m_ref);
    54. }
    55. virtual ULONG STDMETHODCALLTYPE Release(void)
    56. {
    57. LONG Result = InterlockedDecrement(&m_ref);
    58. if (Result == 0)
    59. delete this;
    60. return (ULONG)Result;
    61. }
    62. private:
    63. LONG m_ref;
    64. };
    65. int main()
    66. {
    67. CoInitialize(NULL);
    68. INetworkListManager *pNetworkListManager = NULL;
    69. HRESULT hr = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_INetworkListManager, (LPVOID *)&pNetworkListManager);
    70. printf("CLSID_NetworkListManager: %d\n", FAILED(hr));
    71. VARIANT_BOOL bConnected = VARIANT_FALSE;
    72. hr = pNetworkListManager->get_IsConnected(&bConnected);
    73. printf("is Connected: %d %d\n", bConnected, FAILED(hr));
    74. IConnectionPointContainer *pCPContainer = NULL;
    75. hr = pNetworkListManager->QueryInterface(IID_IConnectionPointContainer, (void**)&pCPContainer);
    76. IConnectionPoint *pConnectPoint = NULL;
    77. hr = pCPContainer->FindConnectionPoint(IID_INetworkListManagerEvents, &pConnectPoint);
    78. DWORD Cookie = 0;
    79. MyNetWorkEvent* pMyEvent = new MyNetWorkEvent();
    80. pConnectPoint->Advise((IUnknown*)pMyEvent, &Cookie);
    81. MSG msg;
    82. while (GetMessage(&msg, NULL, 0, 0))
    83. {
    84. TranslateMessage(&msg);
    85. DispatchMessage(&msg);
    86. if (msg.message == WM_QUIT)
    87. {
    88. break;
    89. }
    90. }
    91. pConnectPoint->Unadvise(Cookie);
    92. pConnectPoint->Release();
    93. pCPContainer->Release();
    94. pNetworkListManager->Release();
    95. system("pause");
    96. CoUninitialize();
    97. return 0;
    98. }

    3 测试结果截图:能检测到网络状态变化的消息:

    • 断开电脑网络 : 0x0000
    • 然后重新连接 : 0x0003->0x0004

     

  • 相关阅读:
    Pthread 并发编程(三)——深入理解线程取消机制
    【深度强化学习】Python:OpenAI Gym-CarRacing 自动驾驶 | 提供项目完整代码 | 车道检测功能 | 路径训练功能 | 车辆控制功能
    图扑软件数字孪生民航飞联网,构建智慧民航新业态
    操作系统 - 进程
    GO语言篇之WebAssembly
    区块链3.0时代 基于GoMars构建的新概念TravelFi能否注入新力量?
    Iceberg 源码阅读 python API—— TypeID
    spring boot监测系统运行时长
    webpack打包之sourceMap
    从0开始学Java:运算符(Operator)与标点符号(Separators)
  • 原文地址:https://blog.csdn.net/GUMU12345/article/details/126470687