forked from chrisoldwood/WMI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.cpp
More file actions
262 lines (196 loc) · 7.37 KB
/
Connection.cpp
File metadata and controls
262 lines (196 loc) · 7.37 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
////////////////////////////////////////////////////////////////////////////////
//! \file Connection.cpp
//! \brief The Connection class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "Connection.hpp"
#include <WCL/ComStr.hpp>
#include "Exception.hpp"
#include <Core/StringUtils.hpp>
#include "ObjectIterator.hpp"
#ifdef _MSC_VER
// Add .lib to linker.
#pragma comment(lib, "wbemuuid.lib")
#endif
namespace WMI
{
////////////////////////////////////////////////////////////////////////////////
// Constants.
//! The local computer path.
const tstring Connection::LOCALHOST = TXT(".");
//! The default namespace.
const tstring Connection::DEFAULT_NAMESPACE = TXT("\\root\\cimv2");
////////////////////////////////////////////////////////////////////////////////
//! Default constructor.
Connection::Connection()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Open a connection to a specific host using the current credentials.
Connection::Connection(const tstring& host)
{
open(host);
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
Connection::~Connection()
{
close();
}
////////////////////////////////////////////////////////////////////////////////
//! Query if the connection is open.
bool Connection::isOpen() const
{
return ((m_locator.get() != nullptr) && (m_services.get() != nullptr));
}
////////////////////////////////////////////////////////////////////////////////
//! Open a connection to the current host.
void Connection::open()
{
open(LOCALHOST, TXT(""), TXT(""), DEFAULT_NAMESPACE);
}
////////////////////////////////////////////////////////////////////////////////
//! Open a connection to a specific host using the current credentials.
void Connection::open(const tstring& host)
{
open(host, TXT(""), TXT(""), DEFAULT_NAMESPACE);
}
////////////////////////////////////////////////////////////////////////////////
//! Open a connection to a specific host.
void Connection::open(const tstring& host, const tstring& login, const tstring& password)
{
open(host, login, password, DEFAULT_NAMESPACE);
}
////////////////////////////////////////////////////////////////////////////////
//! Open a connection to a specific host and namespace.
void Connection::open(const tstring& host, const tstring& login, const tstring& password, const tstring& nmspace)
{
ASSERT(!isOpen());
// Format the full connection path.
tstring path = host + nmspace;
if (path.compare(0, 2, TXT("\\\\")) != 0)
path = TXT("\\\\") + path;
// Create the connection.
IWbemServicesPtr services;
IWbemLocatorPtr locator(CLSID_WbemLocator);
WCL::ComStr bstrPath(path);
WCL::ComStr bstrAuth(TXT(""));
HRESULT result;
if (login.empty())
{
result = locator->ConnectServer(bstrPath.Get(), nullptr, nullptr, nullptr, 0,
bstrAuth.Get(), nullptr, AttachTo(services));
}
else
{
WCL::ComStr bstrLogin(login);
WCL::ComStr bstrPassword(password);
result = locator->ConnectServer(bstrPath.Get(), bstrLogin.Get(), bstrPassword.Get(), nullptr, 0,
bstrAuth.Get(), nullptr, AttachTo(services));
}
if (FAILED(result))
throw Exception(result, locator, Core::fmt(TXT("Failed to connect to the WMI provider on '%s'"), host.c_str()).c_str());
// Enable impersonation on the connection.
result = ::CoSetProxyBlanket(services.get(), RPC_C_AUTHN_DEFAULT, RPC_C_AUTHZ_DEFAULT, nullptr,
RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE,
nullptr, EOAC_NONE);
if (FAILED(result))
throw Exception(result, locator, TXT("Failed to enable impersonation on the WMI connection"));
// Update state.
m_locator = locator;
m_services = services;
}
////////////////////////////////////////////////////////////////////////////////
//! Close the connection.
void Connection::close()
{
m_services.Release();
m_locator.Release();
}
////////////////////////////////////////////////////////////////////////////////
//! Get a single object using it's unique path.
Object Connection::getObject(const tstring& path) const
{
const WCL::ComStr objectPath(path);
IWbemClassObjectPtr object;
HRESULT result = m_services->GetObject(objectPath.Get(), WBEM_FLAG_RETURN_WBEM_COMPLETE,
nullptr, AttachTo(object), nullptr);
if (FAILED(result))
{
const tstring message = Core::fmt(TXT("Failed to get object from path '%s'"), path.c_str());
throw Exception(result, m_services, message.c_str());
}
return Object(object, *this);
}
////////////////////////////////////////////////////////////////////////////////
//! Execute the query.
ObjectIterator Connection::execQuery(const tstring& query) const
{
return execQuery(query.c_str());
}
////////////////////////////////////////////////////////////////////////////////
//! Execute the query.
ObjectIterator Connection::execQuery(const tchar* query) const
{
ASSERT(isOpen());
WCL::ComStr language(L"WQL");
WCL::ComStr queryText(query);
long flags(WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY);
IEnumWbemClassObjectPtr enumerator;
// Execute it.
HRESULT result = m_services->ExecQuery(language.Get(), queryText.Get(), flags,
nullptr, AttachTo(enumerator));
if (FAILED(result))
throw Exception(result, m_services, TXT("Failed to execute a WMI query"));
return ObjectIterator(enumerator, *this);
}
////////////////////////////////////////////////////////////////////////////////
//! Execute a method on an object.
void Connection::execMethod(IWbemServicesPtr connection, IWbemClassObjectPtr object,
const tchar* path, const tchar* method, WCL::Variant& returnValue)
{
ASSERT(connection.get() != nullptr);
const WCL::ComStr objectPath(path);
const WCL::ComStr methodName(method);
IWbemClassObjectPtr output;
HRESULT result = connection->ExecMethod(objectPath.Get(), methodName.Get(), 0,
nullptr, nullptr, AttachTo(output), nullptr);
if (FAILED(result))
{
const tstring message = Core::fmt(TXT("Failed to execute method '%s' on object '%s'"), method, path);
throw Exception(result, connection, message.c_str());
}
const WCL::ComStr RETURN_VALUE(TXT("ReturnValue"));
result = output->Get(RETURN_VALUE.Get(), 0, &returnValue, NULL, 0);
if (FAILED(result))
{
const tstring message = Core::fmt(TXT("Failed to get '%s' method return value"), method);
throw Exception(result, connection, message.c_str());
}
}
////////////////////////////////////////////////////////////////////////////////
//! Execute a method on the object.
void Connection::execMethod(IWbemServicesPtr connection, IWbemClassObjectPtr object,
const tchar* path, const tchar* method, IWbemClassObjectPtr arguments, WCL::Variant& returnValue)
{
ASSERT(connection.get() != nullptr);
const WCL::ComStr objectPath(path);
const WCL::ComStr methodName(method);
IWbemClassObjectPtr output;
HRESULT result = connection->ExecMethod(objectPath.Get(), methodName.Get(), 0,
nullptr, arguments.get(), AttachTo(output), nullptr);
if (FAILED(result))
{
const tstring message = Core::fmt(TXT("Failed to execute method '%s' on object '%s'"), method, path);
throw Exception(result, connection, message.c_str());
}
const WCL::ComStr RETURN_VALUE(TXT("ReturnValue"));
result = output->Get(RETURN_VALUE.Get(), 0, &returnValue, NULL, 0);
if (FAILED(result))
{
const tstring message = Core::fmt(TXT("Failed to get '%s' method return value"), method);
throw Exception(result, connection, message.c_str());
}
}
//namespace WMI
}