@@ -83,104 +83,6 @@ std::string GAPlatformWin32::getConnectionType()
8383 return " " ;
8484}
8585
86- std::string GAPlatformWin32::getDeviceManufacturer ()
87- {
88- #if !GA_SHARED_LIB && defined(GA_USE_WBEM_SERVICES)
89- __try
90- {
91- IWbemLocator* locator = nullptr ;
92- IWbemServices* services = nullptr ;
93-
94- auto hResult = CoInitializeEx (0 , COINIT_MULTITHREADED);
95- if (FAILED (hResult))
96- {
97- return UNKNOWN_VALUE;
98- }
99- hResult = CoCreateInstance (CLSID_WbemLocator, 0 , CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&locator);
100-
101-
102- auto hasFailed = [&hResult]() {
103- if (FAILED (hResult))
104- {
105- return true ;
106- }
107- return false ;
108- };
109-
110- auto getValue = [&hResult, &hasFailed](IWbemClassObject* classObject, LPCWSTR property) {
111- BSTR propertyValueText = L" unknown" ;
112- VARIANT propertyValue;
113- hResult = classObject->Get (property, 0 , &propertyValue, 0 , 0 );
114- if (!hasFailed ()) {
115- if ((propertyValue.vt == VT_NULL) || (propertyValue.vt == VT_EMPTY)) {
116- }
117- else if (propertyValue.vt & VT_ARRAY) {
118- propertyValueText = L" unknown" ; // Array types not supported
119- }
120- else {
121- propertyValueText = propertyValue.bstrVal ;
122- }
123- }
124- VariantClear (&propertyValue);
125- return propertyValueText;
126- };
127-
128- BSTR manufacturer = L" unknown" ;
129- if (!hasFailed ())
130- {
131- // Connect to the root\cimv2 namespace with the current user and obtain pointer pSvc to make IWbemServices calls.
132- hResult = locator->ConnectServer (L" ROOT\\ CIMV2" , nullptr , nullptr , 0 , NULL , 0 , 0 , &services);
133-
134- if (!hasFailed ()) {
135- // Set the IWbemServices proxy so that impersonation of the user (client) occurs.
136- hResult = CoSetProxyBlanket (services, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr , RPC_C_AUTHN_LEVEL_CALL,
137- RPC_C_IMP_LEVEL_IMPERSONATE, nullptr , EOAC_NONE);
138-
139- if (!hasFailed ()) {
140- IEnumWbemClassObject* classObjectEnumerator = nullptr ;
141- hResult = services->ExecQuery (L" WQL" , L" SELECT * FROM Win32_ComputerSystem" , WBEM_FLAG_FORWARD_ONLY |
142- WBEM_FLAG_RETURN_IMMEDIATELY, nullptr , &classObjectEnumerator);
143- if (!hasFailed ())
144- {
145- IWbemClassObject* classObject;
146- ULONG uReturn = 0 ;
147- hResult = classObjectEnumerator->Next (WBEM_INFINITE, 1 , &classObject, &uReturn);
148- if (uReturn != 0 ) {
149- manufacturer = getValue (classObject, (LPCWSTR)L" Manufacturer" );
150- }
151-
152- if (classObject)
153- classObject->Release ();
154- }
155-
156- if (classObjectEnumerator)
157- classObjectEnumerator->Release ();
158- }
159- }
160- }
161-
162- if (locator) {
163- locator->Release ();
164- }
165- if (services) {
166- services->Release ();
167- }
168- CoUninitialize ();
169-
170- std::string manufacturer = _com_util::ConvertBSTRToString (manufacturer);
171- return manufacturer;
172- }
173- __except (EXCEPTION_EXECUTE_HANDLER)
174- {
175- logging::GALogger::e (" Failed to get device's model" );
176- return UNKNOWN_VALUE;
177- }
178-
179- #else
180- return UNKNOWN_VALUE;
181- #endif
182- }
183-
18486std::string GAPlatformWin32::getBuildPlatform ()
18587{
18688 return " windows" ;
@@ -204,33 +106,30 @@ std::string GAPlatformWin32::getPersistentPath()
204106 return path;
205107}
206108
207- std::string GAPlatformWin32::getDeviceModel ( )
109+ std::string getRegistryKey (HKEY key, const TCHAR* subkey, const TCHAR* value )
208110{
209111 __try
210112 {
211- constexpr const TCHAR* subkey = _T (" SYSTEM\\ CurrentControlSet\\ Control\\ SystemInformation" );
212- constexpr const TCHAR* value = _T (" SystemProductName" );
213-
214113 constexpr DWORD maxBufSize = 128 ;
215114
216115 DWORD size = 0 ;
217116 TCHAR buffer[maxBufSize] = _T (" " );
218- RegGetValue (HKEY_LOCAL_MACHINE , subkey, value, RRF_RT_REG_SZ, NULL , buffer, &size);
117+ RegGetValue (key , subkey, value, RRF_RT_REG_SZ, NULL , buffer, &size);
219118
220119 size = std::min (size, maxBufSize);
221120
222121 if (!GetLastError () && size > 0 )
223122 {
224- std::string modelName ;
123+ std::string val ;
225124
226125#ifdef UNICODE
227126 std::wstring wstr (buffer, buffer + size);
228- modelName = utilities::GAUtilities::ws2s (wstr);
127+ val = utilities::GAUtilities::ws2s (wstr);
229128#else
230- modelName = std::string (buffer, buffer + size);
129+ val = std::string (buffer, buffer + size);
231130#endif
232131
233- return modelName ;
132+ return val ;
234133 }
235134 }
236135 __except (EXCEPTION_EXECUTE_HANDLER)
@@ -241,6 +140,30 @@ std::string GAPlatformWin32::getDeviceModel()
241140 return UNKNOWN_VALUE;
242141}
243142
143+ std::string GAPlatformWin32::getDeviceModel ()
144+ {
145+ constexpr const TCHAR* subkey = _T (" SYSTEM\\ CurrentControlSet\\ Control\\ SystemInformation" );
146+ constexpr const TCHAR* value = _T (" SystemProductName" );
147+
148+ return getRegistryKey (HKEY_LOCAL_MACHINE, subkey, value);
149+ }
150+
151+ std::string GAPlatformWin32::getDeviceManufacturer ()
152+ {
153+ constexpr const TCHAR* subkey = _T (" SYSTEM\\ CurrentControlSet\\ Control\\ SystemInformation" );
154+ constexpr const TCHAR* value = _T (" SystemManufacturer" );
155+
156+ return getRegistryKey (HKEY_LOCAL_MACHINE, subkey, value);
157+ }
158+
159+ std::string GAPlatformWin32::getCpuModel () const
160+ {
161+ constexpr const TCHAR* subkey = _T (" HARDWARE\\ DESCRIPTION\\ System\\ CentralProcessor\\ 0" );
162+ constexpr const TCHAR* value = _T (" ProcessorName" );
163+
164+ return getRegistryKey (HKEY_LOCAL_MACHINE, subkey, value);
165+ }
166+
244167void GAPlatformWin32::setupUncaughtExceptionHandler ()
245168{
246169 signal (SIGILL, signalHandler);
@@ -293,12 +216,6 @@ void GAPlatformWin32::signalHandler(int sig)
293216 }
294217}
295218
296- std::string GAPlatformWin32::getCpuModel () const
297- {
298- // todo
299- return " " ;
300- }
301-
302219std::string GAPlatformWin32::getGpuModel () const
303220{
304221 // todo
@@ -307,7 +224,31 @@ std::string GAPlatformWin32::getGpuModel() const
307224
308225int GAPlatformWin32::getNumCpuCores () const
309226{
310- // todo
227+ DWORD len = 0 ;
228+ GetLogicalProcessorInformation (nullptr , &len);
229+
230+ if (len && (GetLastError () == ERROR_INSUFFICIENT_BUFFER))
231+ {
232+ const int size = len / sizeof (SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
233+ auto buffer = std::make_unique<SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]>(size);
234+ if (buffer)
235+ {
236+ if (GetLogicalProcessorInformation (buffer.get (), &len))
237+ {
238+ int numProcessors = 0 ;
239+ for (int i = 0 ; i < size; ++i)
240+ {
241+ if (buffer[i].Relationship == RelationProcessorCore)
242+ {
243+ ++numProcessors;
244+ }
245+ }
246+
247+ return numProcessors;
248+ }
249+ }
250+ }
251+
311252 return 0 ;
312253}
313254
@@ -374,6 +315,22 @@ int64_t GAPlatformWin32::getBootTime() const
374315 return 0ll ;
375316}
376317
318+ std::string GAPlatformWin32::getConnectionType ()
319+ {
320+ DWORD flags = 0 ;
321+ if (InternetGetConnectedState (&flags, 0 ))
322+ {
323+ if (flags & INTERNET_CONNECTION_OFFLINE)
324+ return CONNECTION_OFFLINE;
325+
326+ if (flags & INTERNET_CONNECTION_LAN)
327+ return CONNECTION_LAN;
328+
329+ return CONNECTION_WIFI;
330+ }
331+
332+ return CONNECTION_OFFLINE;
333+ }
377334
378335}
379336#endif
0 commit comments