| 1 | /************************************************************************** |
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 3 | * * |
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | #include "StdAfx.h" |
|---|
| 22 | #include ".\wmiquery.h" |
|---|
| 23 | |
|---|
| 24 | #include <objidl.h> |
|---|
| 25 | #include <Wbemidl.h> |
|---|
| 26 | #include <map> |
|---|
| 27 | |
|---|
| 28 | WMIQuery::WMIQuery(void) : bInitialized(false) |
|---|
| 29 | { |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | WMIQuery::~WMIQuery(void) |
|---|
| 33 | { |
|---|
| 34 | if (bInitialized) |
|---|
| 35 | unInitialize(); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | bool WMIQuery::initialize() |
|---|
| 40 | { |
|---|
| 41 | if (CoInitialize(NULL) != S_OK) |
|---|
| 42 | return false; |
|---|
| 43 | bInitialized = true; |
|---|
| 44 | if(CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,0,0) != S_OK) { |
|---|
| 45 | return false; |
|---|
| 46 | } |
|---|
| 47 | return true; |
|---|
| 48 | } |
|---|
| 49 | void WMIQuery::unInitialize() |
|---|
| 50 | { |
|---|
| 51 | CoUninitialize(); |
|---|
| 52 | bInitialized = false; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | std::map<std::string,int> WMIQuery::execute(std::string query) |
|---|
| 57 | { |
|---|
| 58 | std::map<std::string,int> ret; |
|---|
| 59 | IWbemLocator * pIWbemLocator = NULL; |
|---|
| 60 | BSTR bstrNamespace = (L"root\\cimv2"); |
|---|
| 61 | //BSTR bstrNamespace = (L"root\\default"); |
|---|
| 62 | HRESULT hRes = CoCreateInstance(CLSID_WbemAdministrativeLocator,NULL,CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, |
|---|
| 63 | IID_IUnknown,(void**)&pIWbemLocator); |
|---|
| 64 | if (FAILED(hRes)) { |
|---|
| 65 | throw WMIException("CoCreateInstance for CLSID_WbemAdministrativeLocator failed!", hRes); |
|---|
| 66 | } |
|---|
| 67 | IWbemServices * pWbemServices = NULL; |
|---|
| 68 | hRes = pIWbemLocator->ConnectServer(bstrNamespace,NULL,NULL,NULL,0,NULL,NULL,&pWbemServices); |
|---|
| 69 | if (FAILED(hRes)) { |
|---|
| 70 | pIWbemLocator->Release(); |
|---|
| 71 | pIWbemLocator = NULL; |
|---|
| 72 | throw WMIException("ConnectServer failed!", hRes); |
|---|
| 73 | } |
|---|
| 74 | CComBSTR strQuery(query.c_str()); |
|---|
| 75 | BSTR strQL = (L"WQL"); |
|---|
| 76 | IEnumWbemClassObject * pEnumObject = NULL; |
|---|
| 77 | hRes = pWbemServices->ExecQuery(strQL, strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject); |
|---|
| 78 | if (FAILED(hRes)) { |
|---|
| 79 | pWbemServices->Release(); |
|---|
| 80 | pIWbemLocator->Release(); |
|---|
| 81 | pIWbemLocator = NULL; |
|---|
| 82 | throw WMIException("ExecQuery failed:" + query, hRes); |
|---|
| 83 | } |
|---|
| 84 | hRes = pEnumObject->Reset(); |
|---|
| 85 | if (FAILED(hRes)) { |
|---|
| 86 | pWbemServices->Release(); |
|---|
| 87 | pIWbemLocator->Release(); |
|---|
| 88 | pIWbemLocator = NULL; |
|---|
| 89 | throw WMIException("ExecQuery failed:" + query, hRes); |
|---|
| 90 | } |
|---|
| 91 | ULONG uCount = 1, uReturned; |
|---|
| 92 | IWbemClassObject * pClassObject = NULL; |
|---|
| 93 | hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned); |
|---|
| 94 | if (FAILED(hRes)) { |
|---|
| 95 | pWbemServices->Release(); |
|---|
| 96 | pIWbemLocator->Release(); |
|---|
| 97 | pIWbemLocator = NULL; |
|---|
| 98 | throw WMIException("ExecQuery failed!" + query, hRes); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | SAFEARRAY* pstrNames; |
|---|
| 103 | hRes = pClassObject->GetNames(NULL,WBEM_FLAG_ALWAYS|WBEM_FLAG_NONSYSTEM_ONLY,NULL,&pstrNames); |
|---|
| 104 | if (FAILED(hRes)) { |
|---|
| 105 | pClassObject->Release(); |
|---|
| 106 | pWbemServices->Release(); |
|---|
| 107 | pIWbemLocator->Release(); |
|---|
| 108 | throw WMIException("GetNames failed!" + query, hRes); |
|---|
| 109 | } |
|---|
| 110 | CComSafeArray<BSTR> arr = pstrNames; |
|---|
| 111 | long index = 0, begin, end; |
|---|
| 112 | begin = arr.GetLowerBound(); |
|---|
| 113 | end = arr.GetUpperBound(); |
|---|
| 114 | for ( index = begin; index <= end; index++ ) { |
|---|
| 115 | BSTR bStr = arr.GetAt(index); |
|---|
| 116 | CString str = bStr; |
|---|
| 117 | std::string std_str = str; |
|---|
| 118 | CComVariant vValue; |
|---|
| 119 | hRes = pClassObject->Get(bStr, 0, &vValue, 0, 0); |
|---|
| 120 | if (vValue.vt == VT_INT) { |
|---|
| 121 | ret[std_str] = vValue.intVal; |
|---|
| 122 | //std::cout << (LPCTSTR)str << " = (INT) " << vValue.intVal << std::endl; |
|---|
| 123 | } else if (vValue.vt == VT_I4) { |
|---|
| 124 | ret[std_str] = vValue.lVal; |
|---|
| 125 | //std::cout << (LPCTSTR)str << " = (I4) " << vValue.lVal << std::endl; |
|---|
| 126 | } else if (vValue.vt == VT_UINT) { |
|---|
| 127 | ret[std_str] = vValue.uintVal; |
|---|
| 128 | //std::cout << (LPCTSTR)str << " = (UINT) " << vValue.uintVal << std::endl; |
|---|
| 129 | } else if (vValue.vt == VT_BSTR) { |
|---|
| 130 | std::cout << (LPCTSTR)str << " = UNSUPPORTED (BSTR)" << std::endl; |
|---|
| 131 | CString val = vValue; |
|---|
| 132 | //ret[std_str] = std::string(val); |
|---|
| 133 | } else { |
|---|
| 134 | std::cout << (LPCTSTR)str << " = UNSUPPORTED" << vValue.vt << std::endl; |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | pIWbemLocator->Release(); |
|---|
| 138 | pWbemServices->Release(); |
|---|
| 139 | pEnumObject->Release(); |
|---|
| 140 | pClassObject->Release(); |
|---|
| 141 | return ret; |
|---|
| 142 | } |
|---|