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