]> git.proxmox.com Git - mirror_smartmontools-debian.git/blame - os_win32/wmiquery.h
import smartmontools 7.0
[mirror_smartmontools-debian.git] / os_win32 / wmiquery.h
CommitLineData
cfbba5b9
GI
1/*
2 * os_win32/wmiquery.h
3 *
6b80b4d2 4 * Home page of code is: http://www.smartmontools.org
cfbba5b9 5 *
ff28b140 6 * Copyright (C) 2011-18 Christian Franke
cfbba5b9 7 *
ff28b140 8 * SPDX-License-Identifier: GPL-2.0-or-later
cfbba5b9
GI
9 */
10
11#ifndef WMIQUERY_H
12#define WMIQUERY_H
13
ff28b140 14#define WMIQUERY_H_CVSID "$Id: wmiquery.h 4760 2018-08-19 18:45:53Z chrfranke $"
cfbba5b9 15
cfbba5b9 16#include <wbemcli.h>
cfbba5b9
GI
17
18#include <string>
19
d008864d
GI
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)))
cfbba5b9
GI
27#endif
28
29/////////////////////////////////////////////////////////////////////////////
30// com_bstr
31
32/// Wrapper class for COM BSTR
33class com_bstr
34{
35public:
36 /// Construct from string.
6b80b4d2 37 explicit com_bstr(const char * str);
cfbba5b9
GI
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
50private:
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
62template <class T>
63class com_intf_ptr
64{
65public:
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
102private:
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
113class wbem_enumerator;
114
115/// Wrapper class for IWbemClassObject
116class wbem_object
117{
118public:
119 /// Get string representation.
120 std::string get_str(const char * name) /*const*/;
121
122private:
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
132class wbem_services;
133
134/// Wrapper class for IEnumWbemClassObject
135class wbem_enumerator
136{
137public:
138 /// Get next object, return false if none or error.
139 bool next(wbem_object & obj);
140
141private:
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
152class wbem_services
153{
154public:
155 /// Connect to service, return false on error.
156 bool connect();
157
158 /// Execute query, get result list.
159 /// Return false on error.
ff28b140
TL
160 bool vquery(wbem_enumerator & result, const char * qstr, va_list args) /*const*/
161 __attribute_format_printf(3, 0);
cfbba5b9
GI
162
163 /// Execute query, get single result object.
164 /// Return false on error or result size != 1.
ff28b140
TL
165 bool vquery1(wbem_object & obj, const char * qstr, va_list args) /*const*/
166 __attribute_format_printf(3, 0);
cfbba5b9
GI
167
168 /// Version of vquery() with printf() formatting.
169 bool query(wbem_enumerator & result, const char * qstr, ...) /*const*/
d008864d 170 __attribute_format_printf(3, 4);
cfbba5b9
GI
171
172 /// Version of vquery1() with printf() formatting.
173 bool query1(wbem_object & obj, const char * qstr, ...) /*const*/
d008864d 174 __attribute_format_printf(3, 4);
cfbba5b9
GI
175
176private:
177 com_intf_ptr<IWbemServices> m_intf;
178};
179
180#endif // WMIQUERY_H