File: Win32NetState.cpp

package info (click to toggle)
i2pd 2.58.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,612 kB
  • sloc: cpp: 59,663; makefile: 224; sh: 138
file content (103 lines) | stat: -rw-r--r-- 2,904 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (c) 2013-2024, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/

#if WINVER != 0x0501 // supported since Vista
#include "Win32NetState.h"
#include <windows.h>
#include "Log.h"

IUnknown *pUnknown = nullptr;
INetworkListManager *pNetworkListManager = nullptr;
IConnectionPointContainer *pCPContainer = nullptr;
IConnectionPoint *pConnectPoint = nullptr;
CNetworkListManagerEvent *pNetEvent = nullptr;
DWORD Cookie = 0;

void SubscribeToEvents()
{
	LogPrint(eLogInfo, "NetState: Trying to subscribe to NetworkListManagerEvents");
	CoInitialize(NULL);

	HRESULT Result = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_IUnknown, (void **)&pUnknown);
	if (SUCCEEDED(Result))
	{
		Result = pUnknown->QueryInterface(IID_INetworkListManager, (void **)&pNetworkListManager);
		if (SUCCEEDED(Result))
		{
			VARIANT_BOOL IsConnect = VARIANT_FALSE;
#if defined(_MSC_VER)
			Result = pNetworkListManager->get_IsConnectedToInternet(&IsConnect);
#else
			Result = pNetworkListManager->IsConnectedToInternet(&IsConnect);
#endif
			if (SUCCEEDED(Result)) {
				i2p::transport::transports.SetOnline (true);
				LogPrint(eLogInfo, "NetState: Current state: ", IsConnect == VARIANT_TRUE ? "connected" : "disconnected");
			}

			Result = pNetworkListManager->QueryInterface(IID_IConnectionPointContainer, (void **)&pCPContainer);
			if (SUCCEEDED(Result))
			{
				Result = pCPContainer->FindConnectionPoint(IID_INetworkListManagerEvents, &pConnectPoint);
				if(SUCCEEDED(Result))
				{
					pNetEvent = new CNetworkListManagerEvent;
					Result = pConnectPoint->Advise((IUnknown *)pNetEvent, &Cookie);
					if (SUCCEEDED(Result))
						LogPrint(eLogInfo, "NetState: Successfully subscribed to NetworkListManagerEvent messages");
					else
						LogPrint(eLogError, "NetState: Unable to subscribe to NetworkListManagerEvent messages");
				} else
					LogPrint(eLogError, "NetState: Unable to find interface connection point");
			} else
				LogPrint(eLogError, "NetState: Unable to query NetworkListManager interface");
		} else
			LogPrint(eLogError, "NetState: Unable to query global interface");
	} else
		LogPrint(eLogError, "NetState: Unable to create INetworkListManager interface");
}

void UnSubscribeFromEvents()
{
	LogPrint(eLogInfo, "NetState: Unsubscribing from NetworkListManagerEvents");
	try
	{
		if (pConnectPoint) {
			pConnectPoint->Unadvise(Cookie);
			pConnectPoint->Release();
		}

		if (pNetEvent)
		{
			pNetEvent->Release();
		}

		if (pCPContainer)
		{
			pCPContainer->Release();
		}

		if (pNetworkListManager)
		{
			pNetworkListManager->Release();
		}

		if (pUnknown)
		{
			pUnknown->Release();
		}

		CoUninitialize();
	}
	catch (std::exception& ex)
	{
		LogPrint (eLogError, "NetState: Received exception: ", ex.what ());
	}
}

#endif // WINVER