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 186 187 188 189
|
/*
* os_win32/wmiquery.h
*
* Home page of code is: http://www.smartmontools.org
*
* Copyright (C) 2011-15 Christian Franke
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* You should have received a copy of the GNU General Public License
* (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef WMIQUERY_H
#define WMIQUERY_H
#define WMIQUERY_H_CVSID "$Id: wmiquery.h 4152 2015-10-17 16:08:21Z chrfranke $"
#ifdef HAVE_WBEMCLI_H
#include <wbemcli.h>
#else
#include "wbemcli_small.h"
#endif
#include <string>
#ifndef __GNUC__
#define __attribute_format_printf(x, y) /**/
#elif defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO
// Check format of __mingw_*printf() instead of MSVCRT.DLL:*printf()
#define __attribute_format_printf(x, y) __attribute__((format (gnu_printf, x, y)))
#else
#define __attribute_format_printf(x, y) __attribute__((format (printf, x, y)))
#endif
/////////////////////////////////////////////////////////////////////////////
// com_bstr
/// Wrapper class for COM BSTR
class com_bstr
{
public:
/// Construct from string.
explicit com_bstr(const char * str);
/// Destructor frees BSTR.
~com_bstr()
{ SysFreeString(m_bstr); }
/// Implicit conversion to BSTR.
operator BSTR()
{ return m_bstr; }
/// Convert BSTR back to std::string.
static bool to_str(const BSTR & bstr, std::string & str);
private:
BSTR m_bstr;
com_bstr(const com_bstr &);
void operator=(const com_bstr &);
};
/////////////////////////////////////////////////////////////////////////////
// com_intf_ptr
/// Wrapper class for COM Interface pointer
template <class T>
class com_intf_ptr
{
public:
/// Construct empty object
com_intf_ptr()
: m_ptr(0) { }
/// Destructor releases the interface.
~com_intf_ptr()
{ reset(); }
/// Release interface and clear the pointer.
void reset()
{
if (m_ptr) {
m_ptr->Release(); m_ptr = 0;
}
}
/// Return the pointer.
T * get()
{ return m_ptr; }
/// Pointer dereferencing.
T * operator->()
{ return m_ptr; }
/// Return address of pointer for replacement.
T * * replace()
{ reset(); return &m_ptr; }
/// For (ptr != 0) check.
operator bool() const
{ return !!m_ptr; }
/// For (ptr == 0) check.
bool operator!() const
{ return !m_ptr; }
private:
T * m_ptr;
com_intf_ptr(const com_intf_ptr &);
void operator=(const com_intf_ptr &);
};
/////////////////////////////////////////////////////////////////////////////
// wbem_object
class wbem_enumerator;
/// Wrapper class for IWbemClassObject
class wbem_object
{
public:
/// Get string representation.
std::string get_str(const char * name) /*const*/;
private:
/// Contents is set by wbem_enumerator.
friend class wbem_enumerator;
com_intf_ptr<IWbemClassObject> m_intf;
};
/////////////////////////////////////////////////////////////////////////////
// wbem_enumerator
class wbem_services;
/// Wrapper class for IEnumWbemClassObject
class wbem_enumerator
{
public:
/// Get next object, return false if none or error.
bool next(wbem_object & obj);
private:
/// Contents is set by wbem_services.
friend class wbem_services;
com_intf_ptr<IEnumWbemClassObject> m_intf;
};
/////////////////////////////////////////////////////////////////////////////
// wbem_services
/// Wrapper class for IWbemServices
class wbem_services
{
public:
/// Connect to service, return false on error.
bool connect();
/// Execute query, get result list.
/// Return false on error.
bool vquery(wbem_enumerator & result, const char * qstr, va_list args) /*const*/;
/// Execute query, get single result object.
/// Return false on error or result size != 1.
bool vquery1(wbem_object & obj, const char * qstr, va_list args) /*const*/;
/// Version of vquery() with printf() formatting.
bool query(wbem_enumerator & result, const char * qstr, ...) /*const*/
__attribute_format_printf(3, 4);
/// Version of vquery1() with printf() formatting.
bool query1(wbem_object & obj, const char * qstr, ...) /*const*/
__attribute_format_printf(3, 4);
private:
com_intf_ptr<IWbemServices> m_intf;
};
#endif // WMIQUERY_H
|