mirror of
https://github.com/HolographicHat/Yae.git
synced 2025-12-13 18:08:15 +08:00
native: getDeviceID and getDeviceInfo
This commit is contained in:
2
native.d.ts
vendored
2
native.d.ts
vendored
@@ -1,3 +1,5 @@
|
|||||||
export function selectGameExecutable(): string
|
export function selectGameExecutable(): string
|
||||||
export function checkGameIsRunning(processName: string): boolean
|
export function checkGameIsRunning(processName: string): boolean
|
||||||
export function whoUseThePort(port: number): object
|
export function whoUseThePort(port: number): object
|
||||||
|
export function getDeviceInfo(): object
|
||||||
|
export function getDeviceID(): string
|
||||||
|
|||||||
@@ -3,10 +3,18 @@
|
|||||||
{
|
{
|
||||||
"target_name": "native",
|
"target_name": "native",
|
||||||
"sources": [
|
"sources": [
|
||||||
"main.cc",
|
"src/main.cc",
|
||||||
"utils.cc",
|
"src/utils.h",
|
||||||
"utils.h",
|
"src/utils.cc",
|
||||||
"define.h"
|
"src/define.h",
|
||||||
|
"src/wmi/wmi.cpp",
|
||||||
|
"src/wmi/wmi.hpp",
|
||||||
|
"src/wmi/unistd.h",
|
||||||
|
"src/wmi/wmiresult.cpp",
|
||||||
|
"src/wmi/wmiresult.hpp",
|
||||||
|
"src/wmi/wmiclasses.hpp",
|
||||||
|
"src/wmi/wmiexception.hpp",
|
||||||
|
"src/registry/registry.hpp"
|
||||||
],
|
],
|
||||||
"cflags!": [
|
"cflags!": [
|
||||||
"-fno-exceptions"
|
"-fno-exceptions"
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "define.h"
|
#include "define.h"
|
||||||
|
#include "wmi/wmi.hpp"
|
||||||
|
#include "wmi/wmiclasses.hpp"
|
||||||
|
#include "registry/registry.hpp"
|
||||||
#include <iphlpapi.h>
|
#include <iphlpapi.h>
|
||||||
#pragma comment(lib,"ws2_32.lib")
|
#pragma comment(lib,"ws2_32.lib")
|
||||||
#pragma comment(lib,"iphlpapi.lib")
|
#pragma comment(lib,"iphlpapi.lib")
|
||||||
|
|
||||||
|
using Wmi::Win32_ComputerSystem, Wmi::Win32_OperatingSystem, Wmi::retrieveWmi;
|
||||||
|
|
||||||
namespace native {
|
namespace native {
|
||||||
|
|
||||||
Value checkGameIsRunning(const CallbackInfo &info) {
|
Value checkGameIsRunning(const CallbackInfo &info) {
|
||||||
@@ -83,8 +88,49 @@ namespace native {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value getDeviceID(const CallbackInfo &info) {
|
||||||
|
Env env = info.Env();
|
||||||
|
wstring uuid;
|
||||||
|
if (RegUtils::GetString(HKEY_CURRENT_USER, L"SOFTWARE\\miHoYoSDK", L"MIHOYOSDK_DEVICE_ID", uuid) != ERROR_SUCCESS) {
|
||||||
|
return env.Null();
|
||||||
|
}
|
||||||
|
return Napi::String::New(env, WStringToString(uuid));
|
||||||
|
}
|
||||||
|
|
||||||
|
Value getDeviceInfo(const CallbackInfo &info) {
|
||||||
|
Env env = info.Env();
|
||||||
|
HDC desktop = GetDC(nullptr);
|
||||||
|
int sw = GetDeviceCaps(desktop, DESKTOPHORZRES);
|
||||||
|
int sh = GetDeviceCaps(desktop, DESKTOPVERTRES);
|
||||||
|
ReleaseDC(nullptr, desktop);
|
||||||
|
DWORD buildNum = RegUtils::GetInt(env, HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", L"UBR");
|
||||||
|
wstring locale;
|
||||||
|
if (RegUtils::GetString(HKEY_CURRENT_USER, L"Control Panel\\International", L"LocaleName", locale) != ERROR_SUCCESS) {
|
||||||
|
locale = L"zh-CN";
|
||||||
|
}
|
||||||
|
wstring country;
|
||||||
|
if (RegUtils::GetString(HKEY_CURRENT_USER, L"Control Panel\\International\\Geo", L"Name", country) != ERROR_SUCCESS) {
|
||||||
|
country = L"CN";
|
||||||
|
}
|
||||||
|
auto computer = retrieveWmi<Win32_ComputerSystem>();
|
||||||
|
auto os = retrieveWmi<Win32_OperatingSystem>();
|
||||||
|
string osv = os.Version;
|
||||||
|
Object obj = Object::New(env);
|
||||||
|
obj.Set("model", Napi::String::New(env, computer.Model));
|
||||||
|
obj.Set("locale", Napi::String::New(env, WStringToString(locale)));
|
||||||
|
obj.Set("oemName", Napi::String::New(env, computer.Manufacturer));
|
||||||
|
obj.Set("osBuild", Napi::String::New(env, osv + "." + to_string(buildNum)));
|
||||||
|
obj.Set("osVersion", Napi::String::New(env, osv));
|
||||||
|
obj.Set("screenSize", Napi::String::New(env, to_string(sw) + "x" + to_string(sh)));
|
||||||
|
obj.Set("carrierCountry", Napi::String::New(env, WStringToString(country)));
|
||||||
|
obj.Set("timeZoneOffset", Napi::Number::New(env, os.CurrentTimeZone));
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
Object init(Env env, Object exports) {
|
Object init(Env env, Object exports) {
|
||||||
EnablePrivilege(env, L"SeDebugPrivilege");
|
EnablePrivilege(env, L"SeDebugPrivilege");
|
||||||
|
exports.Set("getDeviceID", Function::New(env, getDeviceID));
|
||||||
|
exports.Set("getDeviceInfo", Function::New(env, getDeviceInfo));
|
||||||
exports.Set("whoUseThePort", Function::New(env, whoUseThePort));
|
exports.Set("whoUseThePort", Function::New(env, whoUseThePort));
|
||||||
exports.Set("checkGameIsRunning", Function::New(env, checkGameIsRunning));
|
exports.Set("checkGameIsRunning", Function::New(env, checkGameIsRunning));
|
||||||
exports.Set("selectGameExecutable", Function::New(env, selectGameExecutable));
|
exports.Set("selectGameExecutable", Function::New(env, selectGameExecutable));
|
||||||
40
native/src/registry/registry.hpp
Normal file
40
native/src/registry/registry.hpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef REGISTRY_HPP
|
||||||
|
#define REGISTRY_HPP
|
||||||
|
|
||||||
|
#include "../define.h"
|
||||||
|
|
||||||
|
namespace RegUtils {
|
||||||
|
|
||||||
|
DWORD GetInt(Env env, HKEY hKey, const wstring &path, const wstring &value) {
|
||||||
|
DWORD data = 0;
|
||||||
|
DWORD size = sizeof(DWORD);
|
||||||
|
LSTATUS retcode = RegGetValue(hKey, path.c_str(), value.c_str(), RRF_RT_REG_DWORD, nullptr, &data, &size);
|
||||||
|
if (retcode != ERROR_SUCCESS) {
|
||||||
|
Error::New(env, "RegGetValue error: " + to_string(retcode)).ThrowAsJavaScriptException();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
LSTATUS GetString(HKEY hKey, const wstring &path, const wstring &value, wstring &result) {
|
||||||
|
DWORD size = 0;
|
||||||
|
LSTATUS retcode = RegGetValue(hKey, path.c_str(), value.c_str(), RRF_RT_REG_SZ, nullptr, nullptr, &size);
|
||||||
|
if (retcode != ERROR_SUCCESS) {
|
||||||
|
return retcode;
|
||||||
|
}
|
||||||
|
wstring data;
|
||||||
|
DWORD len = size / sizeof(WCHAR);
|
||||||
|
data.resize(len);
|
||||||
|
retcode = RegGetValue(hKey, path.c_str(), value.c_str(), RRF_RT_REG_SZ, nullptr, &data[0], &size);
|
||||||
|
if (retcode != ERROR_SUCCESS) {
|
||||||
|
return retcode;
|
||||||
|
}
|
||||||
|
data.resize((len-1));
|
||||||
|
result = data;
|
||||||
|
return ERROR_SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //REGISTRY_HPP
|
||||||
59
native/src/wmi/unistd.h
Normal file
59
native/src/wmi/unistd.h
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma ide diagnostic ignored "OCUnusedMacroInspection"
|
||||||
|
#ifndef _UNISTD_H
|
||||||
|
#define _UNISTD_H 1
|
||||||
|
|
||||||
|
/* This is intended as a drop-in replacement for unistd.h on Windows.
|
||||||
|
* Please add functionality as neeeded.
|
||||||
|
* https://stackoverflow.com/a/826027/1202830
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <io.h>
|
||||||
|
#include <process.h> /* for getpid() and the exec..() family */
|
||||||
|
#include <direct.h> /* for _getcwd() and _chdir() */
|
||||||
|
|
||||||
|
#define srandom srand
|
||||||
|
#define random rand
|
||||||
|
|
||||||
|
/* Values for the second argument to access.
|
||||||
|
These may be OR'd together. */
|
||||||
|
#define R_OK 4 /* Test for read permission. */
|
||||||
|
#define W_OK 2 /* Test for write permission. */
|
||||||
|
//#define X_OK 1 /* execute permission - unsupported in windows*/
|
||||||
|
#define F_OK 0 /* Test for existence. */
|
||||||
|
|
||||||
|
#define access _access
|
||||||
|
#define dup2 _dup2
|
||||||
|
#define execve _execve
|
||||||
|
#define ftruncate _chsize
|
||||||
|
#define unlink _unlink
|
||||||
|
#define fileno _fileno
|
||||||
|
#define getcwd _getcwd
|
||||||
|
#define chdir _chdir
|
||||||
|
#define isatty _isatty
|
||||||
|
#define lseek _lseek
|
||||||
|
/* read, write, and close are NOT being #defined here, because while there are file handle specific versions for Windows, they probably don't work for sockets. You need to look at your app and consider whether to call e.g. closesocket(). */
|
||||||
|
|
||||||
|
#ifdef _WIN64
|
||||||
|
#define ssize_t __int64
|
||||||
|
#else
|
||||||
|
#define ssize_t long
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define STDIN_FILENO 0
|
||||||
|
#define STDOUT_FILENO 1
|
||||||
|
#define STDERR_FILENO 2
|
||||||
|
/*
|
||||||
|
//should be in some equivalent to <sys/types.h>
|
||||||
|
typedef __int8 int8_t;
|
||||||
|
typedef __int16 int16_t;
|
||||||
|
typedef __int32 int32_t;
|
||||||
|
typedef __int64 int64_t;
|
||||||
|
typedef unsigned __int8 uint8_t;
|
||||||
|
typedef unsigned __int16 uint16_t;
|
||||||
|
typedef unsigned __int32 uint32_t;
|
||||||
|
typedef unsigned __int64 uint64_t;
|
||||||
|
*/
|
||||||
|
#endif /* _UNISTD_H */
|
||||||
|
#pragma clang diagnostic pop
|
||||||
445
native/src/wmi/wmi.cpp
Normal file
445
native/src/wmi/wmi.cpp
Normal file
@@ -0,0 +1,445 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* WMI
|
||||||
|
* @author Thomas Sparber (2016)
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
|
||||||
|
#include <comdef.h>
|
||||||
|
#include <functional>
|
||||||
|
#include <WbemCli.h>
|
||||||
|
|
||||||
|
#include "wmi.hpp"
|
||||||
|
|
||||||
|
#pragma comment(lib,"ole32.lib")
|
||||||
|
#pragma comment(lib,"oleaut32.lib")
|
||||||
|
#pragma comment(lib,"wbemuuid.lib")
|
||||||
|
|
||||||
|
using std::function, std::string, std::wstring, std::wstringstream;
|
||||||
|
|
||||||
|
using namespace Wmi;
|
||||||
|
|
||||||
|
wstring escape(wstring str) {
|
||||||
|
size_t start_pos = 0;
|
||||||
|
const wstring from = L"\"";
|
||||||
|
const wstring to = L"\\\"";
|
||||||
|
|
||||||
|
while ((start_pos = str.find(from, start_pos)) != string::npos) {
|
||||||
|
str.replace(start_pos, from.length(), to);
|
||||||
|
start_pos += to.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
IWbemLocator *createWbemLocator() {
|
||||||
|
IWbemLocator *pLocator = nullptr;
|
||||||
|
HRESULT hr = CoCreateInstance(CLSID_WbemLocator, nullptr, CLSCTX_INPROC_SERVER, IID_IWbemLocator,
|
||||||
|
(void **) &pLocator);
|
||||||
|
if (FAILED(hr)) {
|
||||||
|
switch (hr) {
|
||||||
|
case REGDB_E_CLASSNOTREG:
|
||||||
|
throw WmiException("Error initializing IWbemLocator: REGDB_E_CLASSNOTREG", hr);
|
||||||
|
case CLASS_E_NOAGGREGATION:
|
||||||
|
throw WmiException("Error initializing IWbemLocator: CLASS_E_NOAGGREGATION", hr);
|
||||||
|
case E_NOINTERFACE:
|
||||||
|
throw WmiException("Error initializing IWbemLocator: E_NOINTERFACE", hr);
|
||||||
|
case E_POINTER:
|
||||||
|
throw WmiException("Error initializing IWbemLocator: E_POINTER", hr);
|
||||||
|
default:
|
||||||
|
throw WmiException("Error initializing IWbemLocator: Unknown Error", hr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pLocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
IWbemServices *connect(IWbemLocator *pLocator) {
|
||||||
|
IWbemServices *pServices;
|
||||||
|
HRESULT hr = pLocator->ConnectServer(_bstr_t(R"(\\.\root\cimv2)"), nullptr, nullptr, nullptr, 0, nullptr, nullptr,
|
||||||
|
&pServices);
|
||||||
|
if (FAILED(hr)) {
|
||||||
|
switch (hr) {
|
||||||
|
case (HRESULT) WBEM_E_ACCESS_DENIED:
|
||||||
|
throw WmiException("Error initializing IWbemServices: WBEM_E_ACCESS_DENIED", hr);
|
||||||
|
case (HRESULT) WBEM_E_FAILED:
|
||||||
|
throw WmiException("Error initializing IWbemServices: WBEM_E_FAILED", hr);
|
||||||
|
case (HRESULT) WBEM_E_INVALID_NAMESPACE:
|
||||||
|
throw WmiException("Error initializing IWbemServices: WBEM_E_INVALID_NAMESPACE", hr);
|
||||||
|
case (HRESULT) WBEM_E_INVALID_PARAMETER:
|
||||||
|
throw WmiException("Error initializing IWbemServices: WBEM_E_INVALID_PARAMETER", hr);
|
||||||
|
case (HRESULT) WBEM_E_OUT_OF_MEMORY:
|
||||||
|
throw WmiException("Error initializing IWbemServices: WBEM_E_OUT_OF_MEMORY", hr);
|
||||||
|
case (HRESULT) WBEM_E_TRANSPORT_FAILURE:
|
||||||
|
throw WmiException("Error initializing IWbemServices: WBEM_E_TRANSPORT_FAILURE", hr);
|
||||||
|
case (HRESULT) WBEM_E_LOCAL_CREDENTIALS:
|
||||||
|
throw WmiException("Error initializing IWbemServices: WBEM_E_LOCAL_CREDENTIALS", hr);
|
||||||
|
default:
|
||||||
|
throw WmiException("Error initializing IWbemServices: Unknown Error", hr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Set authentication proxy
|
||||||
|
hr = CoSetProxyBlanket(pServices,
|
||||||
|
RPC_C_AUTHN_DEFAULT,
|
||||||
|
RPC_C_AUTHZ_NONE,
|
||||||
|
COLE_DEFAULT_PRINCIPAL,
|
||||||
|
RPC_C_AUTHN_LEVEL_DEFAULT,
|
||||||
|
RPC_C_IMP_LEVEL_IMPERSONATE,
|
||||||
|
nullptr,
|
||||||
|
EOAC_NONE
|
||||||
|
);
|
||||||
|
|
||||||
|
if (FAILED(hr)) {
|
||||||
|
if (hr == E_INVALIDARG) {
|
||||||
|
throw WmiException("Could not set proxy blanket: E_INVALIDARG", hr);
|
||||||
|
} else {
|
||||||
|
throw WmiException("Could not set proxy blanket: Unknown Error", hr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pServices;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumWbemClassObject *execute(IWbemServices *pServices, const string &q) {
|
||||||
|
IEnumWbemClassObject *pClassObject;
|
||||||
|
HRESULT hr = pServices->ExecQuery(_bstr_t("WQL"), _bstr_t(q.c_str()),
|
||||||
|
WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY, nullptr, &pClassObject);
|
||||||
|
if (FAILED(hr)) {
|
||||||
|
switch (hr) {
|
||||||
|
case (HRESULT) WBEM_E_ACCESS_DENIED:
|
||||||
|
throw WmiException("Error executing query: WBEM_E_ACCESS_DENIED", hr);
|
||||||
|
case (HRESULT) WBEM_E_FAILED:
|
||||||
|
throw WmiException("Error executing query: WBEM_E_FAILED", hr);
|
||||||
|
case (HRESULT) WBEM_E_INVALID_CLASS:
|
||||||
|
throw WmiException("Error executing query: WBEM_E_INVALID_CLASS", hr);
|
||||||
|
case (HRESULT) WBEM_E_INVALID_PARAMETER:
|
||||||
|
throw WmiException("Error executing query: WBEM_E_INVALID_PARAMETER", hr);
|
||||||
|
case (HRESULT) WBEM_E_OUT_OF_MEMORY:
|
||||||
|
throw WmiException("Error executing query: WBEM_E_OUT_OF_MEMORY", hr);
|
||||||
|
case (HRESULT) WBEM_E_SHUTTING_DOWN:
|
||||||
|
throw WmiException("Error executing query: WBEM_E_SHUTTING_DOWN", hr);
|
||||||
|
case (HRESULT) WBEM_E_TRANSPORT_FAILURE:
|
||||||
|
throw WmiException("Error executing query: WBEM_E_TRANSPORT_FAILURE", hr);
|
||||||
|
default:
|
||||||
|
throw WmiException("Error executing query: Unknown Error", hr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pClassObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
void foreachObject(IEnumWbemClassObject *pClassObject, const function<bool(IWbemClassObject *)>& fn) {
|
||||||
|
bool cont = true;
|
||||||
|
HRESULT hr = WBEM_S_NO_ERROR;
|
||||||
|
|
||||||
|
//The final Next will return WBEM_S_FALSE
|
||||||
|
while (cont && hr == WBEM_S_NO_ERROR) {
|
||||||
|
ULONG uReturned = 0;
|
||||||
|
IWbemClassObject *apObj;
|
||||||
|
hr = pClassObject->Next(WBEM_INFINITE, 1, &apObj, &uReturned);
|
||||||
|
|
||||||
|
if (FAILED(hr)) {
|
||||||
|
switch (hr) {
|
||||||
|
case WBEM_S_FALSE:
|
||||||
|
break;
|
||||||
|
case (HRESULT) WBEM_E_INVALID_PARAMETER:
|
||||||
|
throw WmiException("Error getting next element: WBEM_E_INVALID_PARAMETER", hr);
|
||||||
|
case (HRESULT) WBEM_E_OUT_OF_MEMORY:
|
||||||
|
throw WmiException("Error getting next element: WBEM_E_OUT_OF_MEMORY", hr);
|
||||||
|
case (HRESULT) WBEM_E_UNEXPECTED:
|
||||||
|
throw WmiException("Error getting next element: WBEM_E_UNEXPECTED", hr);
|
||||||
|
case (HRESULT) WBEM_E_TRANSPORT_FAILURE:
|
||||||
|
throw WmiException("Error getting next element: WBEM_E_TRANSPORT_FAILURE", hr);
|
||||||
|
case WBEM_S_TIMEDOUT:
|
||||||
|
throw WmiException("Error getting next element: WBEM_S_TIMEDOUT", hr);
|
||||||
|
default:
|
||||||
|
throw WmiException("Error getting next element: Unknown Error", hr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uReturned == 1) {
|
||||||
|
cont = fn(apObj);
|
||||||
|
apObj->Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wstring convertVariant(const VARIANT &value) {
|
||||||
|
bool handled = true;
|
||||||
|
std::wstringstream ss;
|
||||||
|
|
||||||
|
switch (value.vt) {
|
||||||
|
case VT_EMPTY:
|
||||||
|
break;
|
||||||
|
case VT_NULL:
|
||||||
|
ss << "NULL";
|
||||||
|
break;
|
||||||
|
case VT_I2:
|
||||||
|
ss << value.iVal;
|
||||||
|
break;
|
||||||
|
case VT_I4:
|
||||||
|
ss << value.lVal;
|
||||||
|
break;
|
||||||
|
case VT_R4:
|
||||||
|
ss << value.fltVal;
|
||||||
|
break;
|
||||||
|
case VT_R8:
|
||||||
|
ss << value.dblVal;
|
||||||
|
break;
|
||||||
|
case VT_BSTR:
|
||||||
|
ss << value.bstrVal;
|
||||||
|
break;
|
||||||
|
case VT_BOOL:
|
||||||
|
ss << (value.boolVal ? "true" : "false");
|
||||||
|
break;
|
||||||
|
case VT_DECIMAL:
|
||||||
|
ss << value.pdecVal;
|
||||||
|
break;
|
||||||
|
case VT_I1:
|
||||||
|
ss << value.cVal;
|
||||||
|
break;
|
||||||
|
case VT_UI1:
|
||||||
|
ss << value.bVal;
|
||||||
|
break;
|
||||||
|
case VT_UI2:
|
||||||
|
ss << value.uiVal;
|
||||||
|
break;
|
||||||
|
case VT_UI4:
|
||||||
|
ss << value.ulVal;
|
||||||
|
break;
|
||||||
|
case VT_I8:
|
||||||
|
ss << value.llVal;
|
||||||
|
break;
|
||||||
|
case VT_UI8:
|
||||||
|
ss << value.ullVal;
|
||||||
|
break;
|
||||||
|
case VT_INT:
|
||||||
|
ss << value.intVal;
|
||||||
|
break;
|
||||||
|
case VT_UINT:
|
||||||
|
ss << value.uintVal;
|
||||||
|
break;
|
||||||
|
case VT_VOID:
|
||||||
|
break;
|
||||||
|
case VT_ARRAY | VT_BSTR: {
|
||||||
|
long lLower, lUpper;
|
||||||
|
SafeArrayGetLBound(value.parray, 1, &lLower);
|
||||||
|
SafeArrayGetUBound(value.parray, 1, &lUpper);
|
||||||
|
|
||||||
|
ss << "[";
|
||||||
|
for (long i = lLower; i <= lUpper; ++i) {
|
||||||
|
BSTR inner;
|
||||||
|
HRESULT hr = SafeArrayGetElement(value.parray, &i, &inner);
|
||||||
|
|
||||||
|
if (!FAILED(hr)) {
|
||||||
|
ss << "\"" << escape(inner) << "\"";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i != lUpper)ss << ",";
|
||||||
|
}
|
||||||
|
ss << "]";
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case VT_ARRAY | VT_I4: {
|
||||||
|
long lLower, lUpper;
|
||||||
|
SafeArrayGetLBound(value.parray, 1, &lLower);
|
||||||
|
SafeArrayGetUBound(value.parray, 1, &lUpper);
|
||||||
|
|
||||||
|
ss << "[";
|
||||||
|
for (long i = lLower; i <= lUpper; ++i) {
|
||||||
|
ULONG inner;
|
||||||
|
HRESULT hr = SafeArrayGetElement(value.parray, &i, &inner);
|
||||||
|
|
||||||
|
if (!FAILED(hr)) {
|
||||||
|
ss << inner;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i != lUpper)ss << ",";
|
||||||
|
}
|
||||||
|
ss << "]";
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
case VT_CY:
|
||||||
|
throw WmiException("Data type not yet supported: VT_CY", value.vt);
|
||||||
|
case VT_DATE:
|
||||||
|
throw WmiException("Data type not yet supported: VT_DATE", value.vt);
|
||||||
|
case VT_DISPATCH:
|
||||||
|
throw WmiException("Data type not yet supported: VT_DISPATCH", value.vt);
|
||||||
|
case VT_ERROR:
|
||||||
|
throw WmiException("Data type not yet supported: VT_ERROR", value.vt);
|
||||||
|
case VT_VARIANT:
|
||||||
|
throw WmiException("Data type not yet supported: VT_VARIANT", value.vt);
|
||||||
|
case VT_UNKNOWN:
|
||||||
|
throw WmiException("Data type not yet supported: VT_UNKNOWN", value.vt);
|
||||||
|
case VT_HRESULT:
|
||||||
|
throw WmiException("Data type not yet supported: VT_HRESULT", value.vt);
|
||||||
|
case VT_PTR:
|
||||||
|
throw WmiException("Data type not yet supported: VT_PTR", value.vt);
|
||||||
|
case VT_SAFEARRAY:
|
||||||
|
throw WmiException("Data type not yet supported: VT_SAFEARRAY", value.vt);
|
||||||
|
case VT_CARRAY:
|
||||||
|
throw WmiException("Data type not yet supported: VT_CARRAY", value.vt);
|
||||||
|
case VT_USERDEFINED:
|
||||||
|
throw WmiException("Data type not yet supported: VT_USERDEFINED", value.vt);
|
||||||
|
case VT_RECORD:
|
||||||
|
throw WmiException("Data type not yet supported: VT_RECORD", value.vt);
|
||||||
|
case VT_INT_PTR:
|
||||||
|
throw WmiException("Data type not yet supported: VT_INT_PTR", value.vt);
|
||||||
|
case VT_UINT_PTR:
|
||||||
|
throw WmiException("Data type not yet supported: VT_UINT_PTR", value.vt);
|
||||||
|
case VT_LPSTR:
|
||||||
|
throw WmiException("Data type not yet supported: VT_LPSTR", value.vt);
|
||||||
|
case VT_LPWSTR:
|
||||||
|
throw WmiException("Data type not yet supported: VT_LPWSTR", value.vt);
|
||||||
|
case VT_BYREF:
|
||||||
|
//for local use only
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
handled = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!handled) {
|
||||||
|
throw WmiException("Unknown data type", value.vt);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
void foreachProperty(IWbemClassObject *object, const function<bool(const wstring &, const std::wstring &)>& fn) {
|
||||||
|
SAFEARRAY *psaNames = nullptr;
|
||||||
|
HRESULT hr = object->GetNames(nullptr, WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY, nullptr, &psaNames);
|
||||||
|
|
||||||
|
if (FAILED(hr)) {
|
||||||
|
switch (hr) {
|
||||||
|
case (HRESULT) WBEM_E_FAILED:
|
||||||
|
throw WmiException("Could not get properties: WBEM_E_FAILED", hr);
|
||||||
|
case (HRESULT) WBEM_E_INVALID_PARAMETER:
|
||||||
|
throw WmiException("Could not get properties: WBEM_E_INVALID_PARAMETER", hr);
|
||||||
|
case (HRESULT) WBEM_E_OUT_OF_MEMORY:
|
||||||
|
throw WmiException("Could not get properties: WBEM_E_OUT_OF_MEMORY", hr);
|
||||||
|
default:
|
||||||
|
throw WmiException("Could not get properties: WBEM_E_FAILED", hr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long lLower, lUpper;
|
||||||
|
BSTR propName = nullptr;
|
||||||
|
SafeArrayGetLBound(psaNames, 1, &lLower);
|
||||||
|
SafeArrayGetUBound(psaNames, 1, &lUpper);
|
||||||
|
|
||||||
|
for (long i = lLower; i <= lUpper; ++i) {
|
||||||
|
hr = SafeArrayGetElement(psaNames, &i, &propName);
|
||||||
|
|
||||||
|
if (FAILED(hr)) {
|
||||||
|
switch (hr) {
|
||||||
|
case DISP_E_BADINDEX:
|
||||||
|
throw WmiException("Could not get name from SafeArray: DISP_E_BADINDEX", hr);
|
||||||
|
case E_INVALIDARG:
|
||||||
|
throw WmiException("Could not get name from SafeArray: E_INVALIDARG", hr);
|
||||||
|
case E_OUTOFMEMORY:
|
||||||
|
throw WmiException("Could not get name from SafeArray: E_OUTOFMEMORY", hr);
|
||||||
|
default:
|
||||||
|
throw WmiException("Could not get name from SafeArray: Unknown Error", hr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VARIANT value;
|
||||||
|
hr = object->Get(propName, 0, &value, nullptr, nullptr);
|
||||||
|
|
||||||
|
if (FAILED(hr)) {
|
||||||
|
switch (hr) {
|
||||||
|
case (HRESULT) WBEM_E_FAILED:
|
||||||
|
throw WmiException("Could not get property: WBEM_E_FAILED", hr);
|
||||||
|
case (HRESULT) WBEM_E_INVALID_PARAMETER:
|
||||||
|
throw WmiException("Could not get property: WBEM_E_INVALID_PARAMETER", hr);
|
||||||
|
case (HRESULT) WBEM_E_NOT_FOUND:
|
||||||
|
throw WmiException("Could not get property: WBEM_E_NOT_FOUND", hr);
|
||||||
|
case (HRESULT) WBEM_E_OUT_OF_MEMORY:
|
||||||
|
throw WmiException("Could not get property: WBEM_E_OUT_OF_MEMORY", hr);
|
||||||
|
default:
|
||||||
|
throw WmiException("Could not get property: Unknown Error", hr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cont;
|
||||||
|
try {
|
||||||
|
cont = fn(propName, convertVariant(value));
|
||||||
|
} catch (const WmiException &e) {
|
||||||
|
wstring temp(propName);
|
||||||
|
throw WmiException(
|
||||||
|
string("Can't convert parameter: ") + string(temp.begin(), temp.end()) + ": " + e.errorMessage,
|
||||||
|
e.errorCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
VariantClear(&value);
|
||||||
|
SysFreeString(propName);
|
||||||
|
if (!cont)break;
|
||||||
|
}
|
||||||
|
|
||||||
|
SafeArrayDestroy(psaNames);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Wmi::query(const string &q, WmiResult &out) {
|
||||||
|
CoInitialize(nullptr);
|
||||||
|
|
||||||
|
IWbemLocator *pLocator;
|
||||||
|
IWbemServices *pServices;
|
||||||
|
IEnumWbemClassObject *pClassObject;
|
||||||
|
|
||||||
|
//Create the WBEM locator
|
||||||
|
try {
|
||||||
|
pLocator = createWbemLocator();
|
||||||
|
} catch (const WmiException &) {
|
||||||
|
CoUninitialize();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Open connection to computer
|
||||||
|
try {
|
||||||
|
pServices = connect(pLocator);
|
||||||
|
} catch (const WmiException &) {
|
||||||
|
pLocator->Release();
|
||||||
|
CoUninitialize();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Execute the query
|
||||||
|
try {
|
||||||
|
pClassObject = execute(pServices, q);
|
||||||
|
} catch (const WmiException &) {
|
||||||
|
pServices->Release();
|
||||||
|
pLocator->Release();
|
||||||
|
CoUninitialize();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
std::size_t index = 0;
|
||||||
|
|
||||||
|
foreachObject(pClassObject, [&out, &index](IWbemClassObject *object) {
|
||||||
|
foreachProperty(object, [&out, index](const wstring &name, const std::wstring &value) {
|
||||||
|
out.set(index, name, value);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
index++;
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
} catch (const WmiException &) {
|
||||||
|
pServices->Release();
|
||||||
|
pLocator->Release();
|
||||||
|
CoUninitialize();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
pClassObject->Release();
|
||||||
|
|
||||||
|
pServices->Release();
|
||||||
|
pLocator->Release();
|
||||||
|
CoUninitialize();
|
||||||
|
}
|
||||||
102
native/src/wmi/wmi.hpp
Normal file
102
native/src/wmi/wmi.hpp
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* WMI
|
||||||
|
* @author Thomas Sparber (2016)
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
|
||||||
|
#ifndef WMI_HPP
|
||||||
|
#define WMI_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "wmiexception.hpp"
|
||||||
|
#include "wmiresult.hpp"
|
||||||
|
|
||||||
|
namespace Wmi {
|
||||||
|
|
||||||
|
void query(const std::string &q, WmiResult &out);
|
||||||
|
|
||||||
|
inline WmiResult query(const std::string &q) {
|
||||||
|
WmiResult result;
|
||||||
|
query(q, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class WmiClass>
|
||||||
|
inline void retrieveWmi(WmiClass &out) {
|
||||||
|
WmiResult result;
|
||||||
|
const std::string q = std::string("Select * From ") + WmiClass::getWmiClassName();
|
||||||
|
query(q, result);
|
||||||
|
out.setProperties(result, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class WmiClass>
|
||||||
|
inline void retrieveWmi(WmiClass &out, const std::string &columns) {
|
||||||
|
WmiResult result;
|
||||||
|
const std::string q = std::string("Select ") + columns + std::string(" From ") + WmiClass::getWmiClassName();
|
||||||
|
query(q, result);
|
||||||
|
out.setProperties(result, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class WmiClass>
|
||||||
|
inline WmiClass retrieveWmi() {
|
||||||
|
WmiClass temp;
|
||||||
|
retrieveWmi(temp);
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class WmiClass>
|
||||||
|
inline WmiClass retrieveWmi(std::string columns) {
|
||||||
|
WmiClass temp;
|
||||||
|
retrieveWmi(temp, columns);
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class WmiClass>
|
||||||
|
inline void retrieveAllWmi(std::vector<WmiClass> &out) {
|
||||||
|
WmiResult result;
|
||||||
|
const std::string q = std::string("Select * From ") + WmiClass::getWmiClassName();
|
||||||
|
query(q, result);
|
||||||
|
|
||||||
|
out.clear();
|
||||||
|
for (std::size_t index = 0; index < result.size(); ++index) {
|
||||||
|
WmiClass temp;
|
||||||
|
temp.setProperties(result, index);
|
||||||
|
out.push_back(std::move(temp));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class WmiClass>
|
||||||
|
inline void retrieveAllWmi(std::vector<WmiClass> &out, const std::string &columns) {
|
||||||
|
WmiResult result;
|
||||||
|
const std::string q = std::string("Select ") + columns + std::string(" From ") + WmiClass::getWmiClassName();
|
||||||
|
query(q, result);
|
||||||
|
|
||||||
|
out.clear();
|
||||||
|
for (std::size_t index = 0; index < result.size(); ++index) {
|
||||||
|
WmiClass temp;
|
||||||
|
temp.setProperties(result, index);
|
||||||
|
out.push_back(std::move(temp));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class WmiClass>
|
||||||
|
inline std::vector<WmiClass> retrieveAllWmi() {
|
||||||
|
std::vector<WmiClass> ret;
|
||||||
|
retrieveAllWmi(ret);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class WmiClass>
|
||||||
|
inline std::vector<WmiClass> retrieveAllWmi(std::string columns) {
|
||||||
|
std::vector<WmiClass> ret;
|
||||||
|
retrieveAllWmi(ret, columns);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
} //end namespace Wmi
|
||||||
|
|
||||||
|
#endif //WMI_HPP
|
||||||
1307
native/src/wmi/wmiclasses.hpp
Normal file
1307
native/src/wmi/wmiclasses.hpp
Normal file
File diff suppressed because it is too large
Load Diff
37
native/src/wmi/wmiexception.hpp
Normal file
37
native/src/wmi/wmiexception.hpp
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* WMI
|
||||||
|
* @author Thomas Sparber (2016)
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
|
||||||
|
#ifndef WMIEXCEPTION_HPP
|
||||||
|
#define WMIEXCEPTION_HPP
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace Wmi {
|
||||||
|
|
||||||
|
struct WmiException {
|
||||||
|
|
||||||
|
WmiException(std::string str_errorMessage, long l_errorCode) :
|
||||||
|
errorMessage(std::move(str_errorMessage)),
|
||||||
|
errorCode(l_errorCode) {}
|
||||||
|
|
||||||
|
std::string hexErrorCode() const {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "0x" << std::hex << errorCode;
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string errorMessage;
|
||||||
|
|
||||||
|
long errorCode;
|
||||||
|
|
||||||
|
}; //end struct WmiException
|
||||||
|
|
||||||
|
} //end namespace Wmi
|
||||||
|
|
||||||
|
#endif //WMIEXCEPTION_HPP
|
||||||
251
native/src/wmi/wmiresult.cpp
Normal file
251
native/src/wmi/wmiresult.cpp
Normal file
@@ -0,0 +1,251 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* WMI
|
||||||
|
* @author Thomas Sparber (2016)
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include "../utils.h"
|
||||||
|
#include "wmiresult.hpp"
|
||||||
|
|
||||||
|
using std::string, std::transform, std::vector, std::wstring;
|
||||||
|
|
||||||
|
using namespace Wmi;
|
||||||
|
|
||||||
|
wstring unescape(wstring str) {
|
||||||
|
size_t start_pos = 0;
|
||||||
|
const wstring from = L"\\\"";
|
||||||
|
const wstring to = L"\"";
|
||||||
|
|
||||||
|
while ((start_pos = str.find(from, start_pos)) != string::npos) {
|
||||||
|
str.replace(start_pos, from.length(), to);
|
||||||
|
start_pos += to.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool tokenize(const wstring &str, vector<wstring> &out) {
|
||||||
|
size_t start_pos = str.find(L'[');
|
||||||
|
|
||||||
|
if (start_pos == string::npos)return false;
|
||||||
|
|
||||||
|
do {
|
||||||
|
start_pos++;
|
||||||
|
size_t end_pos = start_pos;
|
||||||
|
size_t temp_pos = start_pos;
|
||||||
|
|
||||||
|
while (end_pos != start_pos && temp_pos != string::npos && temp_pos == end_pos) {
|
||||||
|
end_pos = str.find(L'\"', temp_pos + 2);
|
||||||
|
temp_pos = str.find(L"\\\"", temp_pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
temp_pos = str.find(L',', end_pos);
|
||||||
|
if (temp_pos == string::npos)temp_pos = str.find(L']', end_pos);
|
||||||
|
end_pos = temp_pos;
|
||||||
|
|
||||||
|
wstring token = str.substr(str[start_pos] == L'\"' ? start_pos + 1 : start_pos,
|
||||||
|
str[start_pos] == L'\"' ? (end_pos - start_pos - 2) : (end_pos - start_pos));
|
||||||
|
out.push_back(unescape(token));
|
||||||
|
|
||||||
|
start_pos = end_pos;
|
||||||
|
end_pos = start_pos;
|
||||||
|
temp_pos = start_pos;
|
||||||
|
} while (start_pos != string::npos && start_pos + 1 != str.length());
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WmiResult::set(std::size_t index, wstring name, const wstring &value) {
|
||||||
|
while (index >= result.size())result.emplace_back();
|
||||||
|
|
||||||
|
transform(name.begin(), name.end(), name.begin(), ::tolower);
|
||||||
|
result[index][name] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, wstring &out) const {
|
||||||
|
if (index >= result.size())return false;
|
||||||
|
|
||||||
|
wstring key(name.cbegin(), name.cend());
|
||||||
|
transform(key.begin(), key.end(), key.begin(), ::tolower);
|
||||||
|
|
||||||
|
auto found = result[index].find(key);
|
||||||
|
if (found == result[index].cend())return false;
|
||||||
|
|
||||||
|
out = found->second;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, string &out) const {
|
||||||
|
wstring temp;
|
||||||
|
if (!extract(index, name, temp))return false;
|
||||||
|
out = WStringToString(temp);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, int &out) const {
|
||||||
|
string temp;
|
||||||
|
if (!extract(index, name, temp))return false;
|
||||||
|
|
||||||
|
char *test;
|
||||||
|
out = strtol(temp.c_str(), &test, 0);
|
||||||
|
return (test == temp.c_str() + temp.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, bool &out) const {
|
||||||
|
string temp;
|
||||||
|
if (!extract(index, name, temp))return false;
|
||||||
|
|
||||||
|
transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
|
||||||
|
if (temp == "true" || temp == "1")out = true;
|
||||||
|
else if (temp == "false" || temp == "0")out = false;
|
||||||
|
else return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, uint64_t &out) const {
|
||||||
|
string temp;
|
||||||
|
if (!extract(index, name, temp))return false;
|
||||||
|
|
||||||
|
char *test;
|
||||||
|
out = strtoull(temp.c_str(), &test, 0);
|
||||||
|
return (test == temp.c_str() + temp.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, uint32_t &out) const {
|
||||||
|
string temp;
|
||||||
|
if (!extract(index, name, temp))return false;
|
||||||
|
|
||||||
|
char *test;
|
||||||
|
out = (uint32_t) std::strtoul(temp.c_str(), &test, 0);
|
||||||
|
return (test == temp.c_str() + temp.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, uint16_t &out) const {
|
||||||
|
string temp;
|
||||||
|
if (!extract(index, name, temp))return false;
|
||||||
|
|
||||||
|
char *test;
|
||||||
|
out = (uint16_t) std::strtoul(temp.c_str(), &test, 0);
|
||||||
|
return (test == temp.c_str() + temp.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, uint8_t &out) const {
|
||||||
|
string temp;
|
||||||
|
if (!extract(index, name, temp))return false;
|
||||||
|
|
||||||
|
char *test;
|
||||||
|
out = (uint8_t) std::strtoul(temp.c_str(), &test, 0);
|
||||||
|
return (test == temp.c_str() + temp.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, vector<wstring> &out) const {
|
||||||
|
wstring temp;
|
||||||
|
if (!extract(index, name, temp))return false;
|
||||||
|
|
||||||
|
if (!tokenize(temp, out))return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, vector<string> &out) const {
|
||||||
|
vector<wstring> tokens;
|
||||||
|
if (!extract(index, name, tokens))return false;
|
||||||
|
|
||||||
|
out.resize(tokens.size());
|
||||||
|
for (std::size_t i = 0; i < tokens.size(); ++i) {
|
||||||
|
const wstring &temp = tokens[i];
|
||||||
|
out[i] = WStringToString(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, vector<int> &out) const {
|
||||||
|
vector<string> tokens;
|
||||||
|
if (!extract(index, name, tokens))return false;
|
||||||
|
|
||||||
|
out.resize(tokens.size());
|
||||||
|
for (std::size_t i = 0; i < tokens.size(); ++i) {
|
||||||
|
char *test;
|
||||||
|
out[i] = strtol(tokens[i].c_str(), &test, 0);
|
||||||
|
return (test == tokens[i].c_str() + tokens[i].length());
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, vector<bool> &out) const {
|
||||||
|
vector<string> tokens;
|
||||||
|
if (!extract(index, name, tokens))return false;
|
||||||
|
|
||||||
|
out.resize(tokens.size());
|
||||||
|
for (std::size_t i = 0; i < tokens.size(); ++i) {
|
||||||
|
string temp = tokens[i];
|
||||||
|
transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
|
||||||
|
if (temp == "true" || temp == "1")out[i] = true;
|
||||||
|
else if (temp == "false" || temp == "0")out[i] = false;
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, vector<uint64_t> &out) const {
|
||||||
|
vector<string> tokens;
|
||||||
|
if (!extract(index, name, tokens))return false;
|
||||||
|
|
||||||
|
out.resize(tokens.size());
|
||||||
|
for (std::size_t i = 0; i < tokens.size(); ++i) {
|
||||||
|
char *test;
|
||||||
|
out[i] = strtoull(tokens[i].c_str(), &test, 0);
|
||||||
|
if (test != tokens[i].c_str() + tokens[i].length()) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, vector<uint32_t> &out) const {
|
||||||
|
vector<string> tokens;
|
||||||
|
if (!extract(index, name, tokens))return false;
|
||||||
|
|
||||||
|
out.resize(tokens.size());
|
||||||
|
for (std::size_t i = 0; i < tokens.size(); ++i) {
|
||||||
|
char *test;
|
||||||
|
out[i] = (uint32_t) strtoul(tokens[i].c_str(), &test, 0);
|
||||||
|
if (test != tokens[i].c_str() + tokens[i].length()) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, vector<uint16_t> &out) const {
|
||||||
|
vector<string> tokens;
|
||||||
|
if (!extract(index, name, tokens))return false;
|
||||||
|
|
||||||
|
out.resize(tokens.size());
|
||||||
|
for (std::size_t i = 0; i < tokens.size(); ++i) {
|
||||||
|
char *test;
|
||||||
|
out[i] = (uint16_t) strtoul(tokens[i].c_str(), &test, 0);
|
||||||
|
if (test != tokens[i].c_str() + tokens[i].length()) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WmiResult::extract(std::size_t index, const string &name, vector<uint8_t> &out) const {
|
||||||
|
vector<string> tokens;
|
||||||
|
if (!extract(index, name, tokens))return false;
|
||||||
|
|
||||||
|
out.resize(tokens.size());
|
||||||
|
for (std::size_t i = 0; i < tokens.size(); ++i) {
|
||||||
|
char *test;
|
||||||
|
out[i] = (uint8_t) strtoul(tokens[i].c_str(), &test, 0);
|
||||||
|
if (test != tokens[i].c_str() + tokens[i].length()) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
84
native/src/wmi/wmiresult.hpp
Normal file
84
native/src/wmi/wmiresult.hpp
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* WMI
|
||||||
|
* @author Thomas Sparber (2016)
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
|
||||||
|
#ifndef WMIRESULT_HPP
|
||||||
|
#define WMIRESULT_HPP
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Wmi {
|
||||||
|
|
||||||
|
class WmiResult {
|
||||||
|
|
||||||
|
public:
|
||||||
|
WmiResult() :
|
||||||
|
result() {}
|
||||||
|
|
||||||
|
void set(std::size_t index, std::wstring name, const std::wstring &value);
|
||||||
|
|
||||||
|
std::vector<std::map<std::wstring, std::wstring> >::iterator begin() {
|
||||||
|
return result.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::map<std::wstring, std::wstring> >::iterator end() {
|
||||||
|
return result.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::map<std::wstring, std::wstring> >::const_iterator cbegin() const {
|
||||||
|
return result.cbegin();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::map<std::wstring, std::wstring> >::const_iterator cend() const {
|
||||||
|
return result.cend();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t size() const {
|
||||||
|
return result.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, std::wstring &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, std::string &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, int &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, bool &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, uint64_t &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, uint8_t &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, uint32_t &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, uint16_t &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, std::vector<std::wstring> &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, std::vector<std::string> &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, std::vector<int> &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, std::vector<bool> &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, std::vector<uint64_t> &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, std::vector<uint32_t> &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, std::vector<uint16_t> &out) const;
|
||||||
|
|
||||||
|
bool extract(std::size_t index, const std::string &name, std::vector<uint8_t> &out) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<std::map<std::wstring, std::wstring> > result;
|
||||||
|
|
||||||
|
}; //end class WmiResult
|
||||||
|
|
||||||
|
}; //end namespace Wmi
|
||||||
|
|
||||||
|
#endif //WMIRESULT_HPP
|
||||||
Reference in New Issue
Block a user