-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwmi_client.cpp
More file actions
240 lines (211 loc) · 7.49 KB
/
wmi_client.cpp
File metadata and controls
240 lines (211 loc) · 7.49 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* @file
* @brief
*
* This file contains
*
* @author Yonhgwhan, Noh (fixbrain@gmail.com)
* @date 2016:06:04 10:02 created.
* @copyright All rights reserved by Yonghwan, Noh.
**/
#include "stdafx.h"
#include "wmi_client.h"
WmiClient::WmiClient():_initialized(false), _locator(NULL), _svc(NULL)
{
}
WmiClient::~WmiClient()
{
finalize();
}
bool WmiClient::initialize()
{
// #1, init COM
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (!SUCCEEDED(hres))
{
log_err "CoInitializeEx() failed. hres=%u", hres log_end;
return false;
}
// #2, set general COM security levels
hres = CoInitializeSecurity(NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impoersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (!SUCCEEDED(hres))
{
log_err "CoInitializeSeciruty() failed. hres=%u", hres log_end;
CoUninitialize();
return false;
}
// #3, Obtain the initial locator to WMI
hres = CoCreateInstance(CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID*)&_locator
);
if (!SUCCEEDED(hres))
{
log_err "CoCreateInstance() failed. hres=%u", hres log_end;
CoUninitialize();
return false;
}
// #4, Connect to WMI through IWbemLocator::ConnectServer
//
// Connect to the `root\cimv2` namespace with the current user and
// obtain pointer _svc to make IWbemServices calls.
hres = _locator->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Serucirty flags.
0, // Authority (for example, Kerberos)
0, // Context object
&_svc // Pointer to IWbemServices proxy
);
if (!SUCCEEDED(hres))
{
log_err "_locator->ConnectServer() failed. hres=%u", hres log_end;
_locator->Release(); _locator = NULL;
CoUninitialize();
return false;
}
//log_dbg "Connected to ROOT\\CIMV2 WMI name space." log_end;
// #5, Security levels on the proxy
hres = CoSetProxyBlanket(_svc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_XXX
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_XXX
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_XXX
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_XXX
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (!SUCCEEDED(hres))
{
log_err "CoSetProxyBlanket() failed. hres=%u", hres log_end;
_svc->Release(); _svc = NULL;
_locator->Release(); _locator = NULL;
CoUninitialize();
return false;
}
_initialized = true;
return true;
}
void WmiClient::finalize()
{
if (_initialized)
{
_ASSERTE(NULL != _locator);
_ASSERTE(NULL != _svc);
_svc->Release();
_locator->Release();
_svc = NULL;
_locator = NULL;
CoUninitialize();
_initialized = false;
}
}
bool WmiClient::query(_In_ const wchar_t* query, _Out_ IEnumWbemClassObject*& enumerator)
{
_ASSERTE(true == _initialized);
if (true != _initialized) return false;
_ASSERTE(NULL != query);
if (NULL == query) return false;
// Use the IWbemServices pointer to make request of WMI
HRESULT hres = _svc->ExecQuery(bstr_t("WQL"),
bstr_t(query),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&enumerator
);
if (!SUCCEEDED(hres))
{
log_err "_svc->ExecQuery() failed. hres=%u", hres log_end;
return false;
}
return true;
}
/// @brief
char* WcsToMbs(_In_ const wchar_t* wcs)
{
_ASSERTE(NULL != wcs);
if (NULL == wcs) return NULL;
int outLen = WideCharToMultiByte(CP_ACP, 0, wcs, -1, NULL, 0, NULL, NULL);
if (0 == outLen) return NULL;
char* outChar = (char*)malloc(outLen * sizeof(char));
if (NULL == outChar) return NULL;
RtlZeroMemory(outChar, outLen);
if (0 == WideCharToMultiByte(CP_ACP, 0, wcs, -1, outChar, outLen, NULL, NULL))
{
log_err "WideCharToMultiByte() failed, errcode=0x%08x", GetLastError() log_end
free(outChar);
return NULL;
}
return outChar;
}
/// @brief
typedef boost::shared_ptr< boost::remove_pointer<char*>::type > raii_char_ptr;
void raii_free(_In_ void* void_ptr)
{
if (NULL == void_ptr) return;
free(void_ptr);
}
/// @brief
std::string WcsToMbsEx(_In_ const wchar_t *wcs)
{
raii_char_ptr tmp(WcsToMbs(wcs), raii_free);
if (NULL == tmp.get())
{
return std::string("");
}
else
{
return std::string(tmp.get());
}
}
/// @brief
std::string variant_to_str(const VARIANT& var)
{
char buf[128] = { 0 };
switch (var.vt)
{
case VT_NULL:
case VT_EMPTY:
return "NULL";
case VT_I2:
StringCbPrintfA(buf, sizeof(buf), "0x%x", (int16_t)var.iVal);
return std::string(buf);
case VT_I4:
StringCbPrintfA(buf, sizeof(buf), "0x%x", (int32_t)var.lVal);
return std::string(buf);
case VT_UI1:
StringCbPrintfA(buf, sizeof(buf), "0x%x", (uint8_t)var.cVal);
return std::string(buf);
case VT_UI2:
StringCbPrintfA(buf, sizeof(buf), "0x%x", (uint16_t)var.uiVal);
return std::string(buf);
case VT_UI4:
StringCbPrintfA(buf, sizeof(buf), "0x%x", (uint32_t)var.ulVal);
return std::string(buf);
case VT_INT:
StringCbPrintfA(buf, sizeof(buf), "0x%x", (int32_t)var.intVal);
return std::string(buf);
case VT_UINT:
StringCbPrintfA(buf, sizeof(buf), "0x%x", (uint32_t)var.uintVal);
return std::string(buf);
case VT_BSTR:
return std::string(WcsToMbsEx(var.bstrVal));
default:
StringCbPrintfA(buf, sizeof(buf), "unknwon type (vt=0x%x)", var.vt);
return std::string(buf);
};
#pragma todo("complete this function...")
return "";
}