-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathObject.cpp
185 lines (137 loc) · 5.48 KB
/
Object.cpp
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
////////////////////////////////////////////////////////////////////////////////
//! \file Object.cpp
//! \brief The Object class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "Object.hpp"
#include "Exception.hpp"
#include <WCL/VariantVector.hpp>
#include <Core/StringUtils.hpp>
#include <iterator>
#include <algorithm>
#ifndef _MSC_VER
WCL_DECLARE_IFACETRAITS(IWbemServices, IID_IWbemServices);
WCL_DECLARE_IFACETRAITS(IWbemClassObject, IID_IWbemClassObject);
#endif
namespace WMI
{
////////////////////////////////////////////////////////////////////////////////
//! Default constructor.
Object::Object()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Construction from the underlying COM object and connection.
Object::Object(IWbemClassObjectPtr object, const Connection& connection)
: m_object(object)
, m_connection(connection)
{
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
Object::~Object()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Query if the object has the named property.
bool Object::hasProperty(const tstring& name) const
{
ASSERT(m_object.get() != nullptr);
// Request the property names from the underlying object.
SAFEARRAY* array = nullptr;
HRESULT result = m_object->GetNames(nullptr, ALL_PROPERTIES, nullptr, &array);
if (FAILED(result))
throw Exception(result, m_object, TXT("Failed to retrieve the objects' property names"));
// Copy the property names to the output buffer.
WCL::VariantVector<BSTR> strings(array, VT_BSTR, true);
#ifdef ANSI_BUILD
#error ANSI build not supported
#else
return (std::find(strings.begin(), strings.end(), name) != strings.end());
#endif
}
////////////////////////////////////////////////////////////////////////////////
//! Get the names of the supported properties.
size_t Object::getPropertyNames(PropertyNames& names, PropertyTypes types) const
{
ASSERT(m_object.get() != nullptr);
// Request the property names from the underlying object.
SAFEARRAY* array = nullptr;
HRESULT result = m_object->GetNames(nullptr, types, nullptr, &array);
if (FAILED(result))
throw Exception(result, m_object, TXT("Failed to retrieve the objects' property names"));
// Copy the property names to the output buffer.
WCL::VariantVector<BSTR> strings(array, VT_BSTR, true);
#ifdef ANSI_BUILD
#error ANSI build not supported
#else
std::copy(strings.begin(), strings.end(), std::insert_iterator<PropertyNames>(names, names.end()));
#endif
return names.size();
}
////////////////////////////////////////////////////////////////////////////////
//! Get the value for a property. This is the generic method used to access a
//! property when the return value type is unknown.
void Object::getProperty(const tstring& name, WCL::Variant& value) const
{
HRESULT result = m_object->Get(WCL::ComStr(name).Get(), 0, &value, nullptr, nullptr);
if (FAILED(result))
throw Exception(result, m_object, TXT("Failed to retrieve an objects' property value"));
}
////////////////////////////////////////////////////////////////////////////////
//! Create the object used to pass arguments to a method.
IWbemClassObjectPtr Object::createArgumentsObject(const tstring& className, const tstring& methodName) const
{
IWbemServicesPtr connection = m_connection.get();
WCL::ComStr comClassName(className);
IWbemClassObjectPtr objectClass;
HRESULT result = connection->GetObject(comClassName.Get(), 0, nullptr, AttachTo(objectClass), nullptr);
if (FAILED(result))
{
const tstring message = Core::fmt(TXT("Failed to get WMI class definition for '%s'"), className.c_str());
throw Exception(result, connection, message.c_str());
}
WCL::ComStr comMethodName(methodName);
IWbemClassObjectPtr arguments;
result = objectClass->GetMethod(comMethodName.Get(), 0, AttachTo(arguments), nullptr);
if (FAILED(result))
{
const tstring message = Core::fmt(TXT("Failed to create WMI arguments object for method '%s'"), methodName.c_str());
throw Exception(result, connection, message.c_str());
}
return arguments;
}
////////////////////////////////////////////////////////////////////////////////
//! Execute a method on the object.
void Object::execMethod(const tchar* method, WCL::Variant& returnValue)
{
ASSERT(m_connection.isOpen());
Connection::execMethod(m_connection.get(), get(), relativePath().c_str(), method, returnValue);
}
////////////////////////////////////////////////////////////////////////////////
//! Execute a method on the object.
void Object::execMethod(const tchar* method, IWbemClassObjectPtr arguments, WCL::Variant& returnValue)
{
ASSERT(m_connection.isOpen());
Connection::execMethod(m_connection.get(), get(), relativePath().c_str(), method, arguments, returnValue);
}
////////////////////////////////////////////////////////////////////////////////
//! Set an argument's value.
void Object::setArgument(IWbemClassObjectPtr arguments, const tstring& name, const WCL::Variant& value)
{
WCL::ComStr comName(name);
HRESULT result = arguments->Put(comName.Get(), 0, const_cast<WCL::Variant*>(&value), 0);
if (FAILED(result))
{
const tstring message = Core::fmt(TXT("Failed to set '%s' argument"), name.c_str());
throw Exception(result, arguments, message.c_str());
}
}
////////////////////////////////////////////////////////////////////////////////
//! Refresh the state of the object.
void Object::refresh()
{
m_object = m_connection.getObject(relativePath()).get();
}
//namespace WMI
}