]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * os_win32/wmiquery.h | |
3 | * | |
4 | * Home page of code is: http://smartmontools.sourceforge.net | |
5 | * | |
6 | * Copyright (C) 2011 Christian Franke <smartmontools-support@lists.sourceforge.net> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2, or (at your option) | |
11 | * any later version. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * (for example COPYING); If not, see <http://www.gnu.org/licenses/>. | |
15 | * | |
16 | */ | |
17 | ||
18 | #ifndef WMIQUERY_H | |
19 | #define WMIQUERY_H | |
20 | ||
21 | #define WMIQUERY_H_CVSID "$Id: wmiquery.h 3243 2011-01-19 20:03:47Z chrfranke $" | |
22 | ||
23 | #ifdef HAVE_WBEMCLI_H | |
24 | #include <wbemcli.h> | |
25 | #else | |
26 | #include "wbemcli_small.h" | |
27 | #endif | |
28 | ||
29 | #include <string> | |
30 | ||
31 | #if !defined(__GNUC__) && !defined(__attribute__) | |
32 | #define __attribute__(x) /**/ | |
33 | #endif | |
34 | ||
35 | ///////////////////////////////////////////////////////////////////////////// | |
36 | // com_bstr | |
37 | ||
38 | /// Wrapper class for COM BSTR | |
39 | class com_bstr | |
40 | { | |
41 | public: | |
42 | /// Construct from string. | |
43 | com_bstr(const char * str); | |
44 | ||
45 | /// Destructor frees BSTR. | |
46 | ~com_bstr() | |
47 | { SysFreeString(m_bstr); } | |
48 | ||
49 | /// Implicit conversion to BSTR. | |
50 | operator BSTR() | |
51 | { return m_bstr; } | |
52 | ||
53 | /// Convert BSTR back to std::string. | |
54 | static bool to_str(const BSTR & bstr, std::string & str); | |
55 | ||
56 | private: | |
57 | BSTR m_bstr; | |
58 | ||
59 | com_bstr(const com_bstr &); | |
60 | void operator=(const com_bstr &); | |
61 | }; | |
62 | ||
63 | ||
64 | ///////////////////////////////////////////////////////////////////////////// | |
65 | // com_intf_ptr | |
66 | ||
67 | /// Wrapper class for COM Interface pointer | |
68 | template <class T> | |
69 | class com_intf_ptr | |
70 | { | |
71 | public: | |
72 | /// Construct empty object | |
73 | com_intf_ptr() | |
74 | : m_ptr(0) { } | |
75 | ||
76 | /// Destructor releases the interface. | |
77 | ~com_intf_ptr() | |
78 | { reset(); } | |
79 | ||
80 | /// Release interface and clear the pointer. | |
81 | void reset() | |
82 | { | |
83 | if (m_ptr) { | |
84 | m_ptr->Release(); m_ptr = 0; | |
85 | } | |
86 | } | |
87 | ||
88 | /// Return the pointer. | |
89 | T * get() | |
90 | { return m_ptr; } | |
91 | ||
92 | /// Pointer dereferencing. | |
93 | T * operator->() | |
94 | { return m_ptr; } | |
95 | ||
96 | /// Return address of pointer for replacement. | |
97 | T * * replace() | |
98 | { reset(); return &m_ptr; } | |
99 | ||
100 | /// For (ptr != 0) check. | |
101 | operator bool() const | |
102 | { return !!m_ptr; } | |
103 | ||
104 | /// For (ptr == 0) check. | |
105 | bool operator!() const | |
106 | { return !m_ptr; } | |
107 | ||
108 | private: | |
109 | T * m_ptr; | |
110 | ||
111 | com_intf_ptr(const com_intf_ptr &); | |
112 | void operator=(const com_intf_ptr &); | |
113 | }; | |
114 | ||
115 | ||
116 | ///////////////////////////////////////////////////////////////////////////// | |
117 | // wbem_object | |
118 | ||
119 | class wbem_enumerator; | |
120 | ||
121 | /// Wrapper class for IWbemClassObject | |
122 | class wbem_object | |
123 | { | |
124 | public: | |
125 | /// Get string representation. | |
126 | std::string get_str(const char * name) /*const*/; | |
127 | ||
128 | private: | |
129 | /// Contents is set by wbem_enumerator. | |
130 | friend class wbem_enumerator; | |
131 | com_intf_ptr<IWbemClassObject> m_intf; | |
132 | }; | |
133 | ||
134 | ||
135 | ///////////////////////////////////////////////////////////////////////////// | |
136 | // wbem_enumerator | |
137 | ||
138 | class wbem_services; | |
139 | ||
140 | /// Wrapper class for IEnumWbemClassObject | |
141 | class wbem_enumerator | |
142 | { | |
143 | public: | |
144 | /// Get next object, return false if none or error. | |
145 | bool next(wbem_object & obj); | |
146 | ||
147 | private: | |
148 | /// Contents is set by wbem_services. | |
149 | friend class wbem_services; | |
150 | com_intf_ptr<IEnumWbemClassObject> m_intf; | |
151 | }; | |
152 | ||
153 | ||
154 | ///////////////////////////////////////////////////////////////////////////// | |
155 | // wbem_services | |
156 | ||
157 | /// Wrapper class for IWbemServices | |
158 | class wbem_services | |
159 | { | |
160 | public: | |
161 | /// Connect to service, return false on error. | |
162 | bool connect(); | |
163 | ||
164 | /// Execute query, get result list. | |
165 | /// Return false on error. | |
166 | bool vquery(wbem_enumerator & result, const char * qstr, va_list args) /*const*/; | |
167 | ||
168 | /// Execute query, get single result object. | |
169 | /// Return false on error or result size != 1. | |
170 | bool vquery1(wbem_object & obj, const char * qstr, va_list args) /*const*/; | |
171 | ||
172 | /// Version of vquery() with printf() formatting. | |
173 | bool query(wbem_enumerator & result, const char * qstr, ...) /*const*/ | |
174 | __attribute__ ((format (printf, 3, 4))); | |
175 | ||
176 | /// Version of vquery1() with printf() formatting. | |
177 | bool query1(wbem_object & obj, const char * qstr, ...) /*const*/ | |
178 | __attribute__ ((format (printf, 3, 4))); | |
179 | ||
180 | private: | |
181 | com_intf_ptr<IWbemServices> m_intf; | |
182 | }; | |
183 | ||
184 | #endif // WMIQUERY_H |