1
+ #include " hwinfo/platform.h"
2
+
3
+ #ifdef HWINFO_WINDOWS
4
+
5
+ #include < hwinfo/network.h>
6
+ #include < hwinfo/utils/stringutils.h>
7
+ #include < hwinfo/utils/wmi_wrapper.h>
8
+
9
+ namespace hwinfo {
10
+
11
+ // _____________________________________________________________________________________________________________________
12
+ std::vector<Network> getAllNetworks () {
13
+ utils::WMI::_WMI wmi;
14
+ const std::wstring query_string (
15
+ L" SELECT InterfaceIndex, IPAddress, Description, MACAddress "
16
+ L" FROM Win32_NetworkAdapterConfiguration" );
17
+ bool success = wmi.execute_query (query_string);
18
+ if (!success) {
19
+ return {};
20
+ }
21
+ std::vector<Network> networks;
22
+
23
+ ULONG u_return = 0 ;
24
+ IWbemClassObject* obj = nullptr ;
25
+ int network_id = 0 ;
26
+ while (wmi.enumerator ) {
27
+ wmi.enumerator ->Next (WBEM_INFINITE, 1 , &obj, &u_return);
28
+ if (!u_return) {
29
+ break ;
30
+ }
31
+ Network network;
32
+ VARIANT vt_prop;
33
+ HRESULT hr;
34
+ hr = obj->Get (L" InterfaceIndex" , 0 , &vt_prop, nullptr , nullptr );
35
+ if (SUCCEEDED (hr)) {
36
+ network._index = std::to_string (vt_prop.uintVal );
37
+ }
38
+ hr = obj->Get (L" IPAddress" , 0 , &vt_prop, nullptr , nullptr );
39
+ if (SUCCEEDED (hr)) {
40
+ if (vt_prop.vt == (VT_ARRAY | VT_BSTR)) {
41
+ LONG lbound, ubound;
42
+ SafeArrayGetLBound (vt_prop.parray , 1 , &lbound);
43
+ SafeArrayGetUBound (vt_prop.parray , 1 , &ubound);
44
+ std::string ipv4, ipv6;
45
+ for (LONG i = lbound; i <= ubound; ++i) {
46
+ BSTR bstr;
47
+ SafeArrayGetElement (vt_prop.parray , &i, &bstr);
48
+ std::wstring ws (bstr, SysStringLen (bstr));
49
+ std::string ip = utils::wstring_to_std_string (ws);
50
+ if (ip.find (' :' ) != std::string::npos) {
51
+ if (ip.find (" fe80::" ) == 0 ) {
52
+ ipv6 = ip;
53
+ } else {
54
+ ipv6 = " " ;
55
+ }
56
+ } else {
57
+ ipv4 = ip;
58
+ }
59
+ SysFreeString (bstr);
60
+ }
61
+ network._ip4 = ipv4;
62
+ network._ip6 = ipv6;
63
+ }
64
+ }
65
+ hr = obj->Get (L" Description" , 0 , &vt_prop, nullptr , nullptr );
66
+ if (SUCCEEDED (hr)) {
67
+ if (vt_prop.vt == VT_BSTR) {
68
+ network._description = utils::wstring_to_std_string (vt_prop.bstrVal );
69
+ }
70
+ }
71
+ hr = obj->Get (L" MACAddress" , 0 , &vt_prop, nullptr , nullptr );
72
+ if (SUCCEEDED (hr)) {
73
+ if (vt_prop.vt == VT_BSTR) {
74
+ network._mac = utils::wstring_to_std_string (vt_prop.bstrVal );
75
+ }
76
+ }
77
+ VariantClear (&vt_prop);
78
+ obj->Release ();
79
+ networks.push_back (std::move (network));
80
+ }
81
+ return networks;
82
+ }
83
+
84
+ } // namespace hwinfo
85
+
86
+ #endif // HWINFO_WINDOWS
0 commit comments