]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - os_win32/wmiquery.h
3000a37545ff62f795117b45bf43dc427d938a7a
[mirror_smartmontools-debian.git] / os_win32 / wmiquery.h
1 /*
2 * os_win32/wmiquery.h
3 *
4 * Home page of code is: http://www.smartmontools.org
5 *
6 * Copyright (C) 2011-15 Christian Franke
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 4152 2015-10-17 16:08:21Z 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 #ifndef __GNUC__
32 #define __attribute_format_printf(x, y) /**/
33 #elif defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO
34 // Check format of __mingw_*printf() instead of MSVCRT.DLL:*printf()
35 #define __attribute_format_printf(x, y) __attribute__((format (gnu_printf, x, y)))
36 #else
37 #define __attribute_format_printf(x, y) __attribute__((format (printf, x, y)))
38 #endif
39
40 /////////////////////////////////////////////////////////////////////////////
41 // com_bstr
42
43 /// Wrapper class for COM BSTR
44 class com_bstr
45 {
46 public:
47 /// Construct from string.
48 explicit com_bstr(const char * str);
49
50 /// Destructor frees BSTR.
51 ~com_bstr()
52 { SysFreeString(m_bstr); }
53
54 /// Implicit conversion to BSTR.
55 operator BSTR()
56 { return m_bstr; }
57
58 /// Convert BSTR back to std::string.
59 static bool to_str(const BSTR & bstr, std::string & str);
60
61 private:
62 BSTR m_bstr;
63
64 com_bstr(const com_bstr &);
65 void operator=(const com_bstr &);
66 };
67
68
69 /////////////////////////////////////////////////////////////////////////////
70 // com_intf_ptr
71
72 /// Wrapper class for COM Interface pointer
73 template <class T>
74 class com_intf_ptr
75 {
76 public:
77 /// Construct empty object
78 com_intf_ptr()
79 : m_ptr(0) { }
80
81 /// Destructor releases the interface.
82 ~com_intf_ptr()
83 { reset(); }
84
85 /// Release interface and clear the pointer.
86 void reset()
87 {
88 if (m_ptr) {
89 m_ptr->Release(); m_ptr = 0;
90 }
91 }
92
93 /// Return the pointer.
94 T * get()
95 { return m_ptr; }
96
97 /// Pointer dereferencing.
98 T * operator->()
99 { return m_ptr; }
100
101 /// Return address of pointer for replacement.
102 T * * replace()
103 { reset(); return &m_ptr; }
104
105 /// For (ptr != 0) check.
106 operator bool() const
107 { return !!m_ptr; }
108
109 /// For (ptr == 0) check.
110 bool operator!() const
111 { return !m_ptr; }
112
113 private:
114 T * m_ptr;
115
116 com_intf_ptr(const com_intf_ptr &);
117 void operator=(const com_intf_ptr &);
118 };
119
120
121 /////////////////////////////////////////////////////////////////////////////
122 // wbem_object
123
124 class wbem_enumerator;
125
126 /// Wrapper class for IWbemClassObject
127 class wbem_object
128 {
129 public:
130 /// Get string representation.
131 std::string get_str(const char * name) /*const*/;
132
133 private:
134 /// Contents is set by wbem_enumerator.
135 friend class wbem_enumerator;
136 com_intf_ptr<IWbemClassObject> m_intf;
137 };
138
139
140 /////////////////////////////////////////////////////////////////////////////
141 // wbem_enumerator
142
143 class wbem_services;
144
145 /// Wrapper class for IEnumWbemClassObject
146 class wbem_enumerator
147 {
148 public:
149 /// Get next object, return false if none or error.
150 bool next(wbem_object & obj);
151
152 private:
153 /// Contents is set by wbem_services.
154 friend class wbem_services;
155 com_intf_ptr<IEnumWbemClassObject> m_intf;
156 };
157
158
159 /////////////////////////////////////////////////////////////////////////////
160 // wbem_services
161
162 /// Wrapper class for IWbemServices
163 class wbem_services
164 {
165 public:
166 /// Connect to service, return false on error.
167 bool connect();
168
169 /// Execute query, get result list.
170 /// Return false on error.
171 bool vquery(wbem_enumerator & result, const char * qstr, va_list args) /*const*/;
172
173 /// Execute query, get single result object.
174 /// Return false on error or result size != 1.
175 bool vquery1(wbem_object & obj, const char * qstr, va_list args) /*const*/;
176
177 /// Version of vquery() with printf() formatting.
178 bool query(wbem_enumerator & result, const char * qstr, ...) /*const*/
179 __attribute_format_printf(3, 4);
180
181 /// Version of vquery1() with printf() formatting.
182 bool query1(wbem_object & obj, const char * qstr, ...) /*const*/
183 __attribute_format_printf(3, 4);
184
185 private:
186 com_intf_ptr<IWbemServices> m_intf;
187 };
188
189 #endif // WMIQUERY_H