Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

549 lines
13 KiB

  1. // GenericClass.cpp: implementation of the CGenericClass class.
  2. // Copyright (c)1997-1999 Microsoft Corporation
  3. //
  4. //////////////////////////////////////////////////////////////////////
  5. #include "precomp.h"
  6. #include "GenericClass.h"
  7. #include <wininet.h>
  8. #define READ_HANDLE 0
  9. #define WRITE_HANDLE 1
  10. /////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CGenericClass::CGenericClass(CRequestObject *pObj, IWbemServices *pNamespace, IWbemContext *pCtx)
  14. {
  15. m_pRequest = pObj;
  16. m_pNamespace = pNamespace;
  17. m_pCtx = pCtx;
  18. m_iRecurs = 0;
  19. m_pObj = NULL;
  20. m_pClassForSpawning = NULL;
  21. }
  22. CGenericClass::~CGenericClass()
  23. {
  24. }
  25. void CGenericClass::CleanUp()
  26. {
  27. if(m_pClassForSpawning){
  28. m_pClassForSpawning->Release();
  29. m_pClassForSpawning = NULL;
  30. }
  31. }
  32. HRESULT CGenericClass::SetSinglePropertyPath(WCHAR wcProperty[])
  33. {
  34. if(m_pRequest->m_iValCount > m_pRequest->m_iPropCount){
  35. m_pRequest->m_Property[m_pRequest->m_iPropCount] = SysAllocString(wcProperty);
  36. if(!m_pRequest->m_Property[(m_pRequest->m_iPropCount)++])
  37. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  38. }
  39. return S_OK;
  40. }
  41. WCHAR * CGenericClass::GetFirstGUID(WCHAR wcIn[], WCHAR wcOut[])
  42. {
  43. wcscpy(wcOut, wcIn);
  44. wcOut[38] = NULL;
  45. return wcOut;
  46. }
  47. WCHAR * CGenericClass::RemoveFinalGUID(WCHAR wcIn[], WCHAR wcOut[])
  48. {
  49. wcscpy(wcOut, wcIn);
  50. wcOut[wcslen(wcOut) - 38] = NULL;
  51. return wcOut;
  52. }
  53. HRESULT CGenericClass::SpawnAnInstance(IWbemServices *pNamespace, IWbemContext *pCtx,
  54. IWbemClassObject **pObj, BSTR bstrName)
  55. {
  56. HRESULT hr = WBEM_S_NO_ERROR;
  57. if(!m_pClassForSpawning){
  58. //Get ourselves an instance
  59. if(FAILED(hr = m_pNamespace->GetObject(bstrName, 0, m_pCtx, &m_pClassForSpawning, NULL))){
  60. *pObj = NULL;
  61. return hr;
  62. }
  63. }
  64. hr = m_pClassForSpawning->SpawnInstance(0, pObj);
  65. return hr;
  66. }
  67. HRESULT CGenericClass::SpawnAnInstance(IWbemClassObject **pObj)
  68. {
  69. HRESULT hr = WBEM_S_NO_ERROR;
  70. if(!m_pClassForSpawning){
  71. //Get ourselves an instance
  72. if(FAILED(hr = m_pNamespace->GetObject(m_pRequest->m_bstrClass, 0, m_pCtx,
  73. &m_pClassForSpawning, NULL))){
  74. *pObj = NULL;
  75. return hr;
  76. }
  77. }
  78. hr = m_pClassForSpawning->SpawnInstance(0, pObj);
  79. return hr;
  80. }
  81. HRESULT CGenericClass::PutProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, WCHAR *wcValue)
  82. {
  83. HRESULT hr = WBEM_S_NO_ERROR;
  84. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  85. BSTR bstrName = SysAllocString(wcProperty);
  86. if(!bstrName)
  87. throw he;
  88. VARIANT *vp = new VARIANT;
  89. VariantInit(vp);
  90. V_VT(vp) = VT_BSTR;
  91. V_BSTR(vp) = SysAllocString(wcValue);
  92. if(!V_BSTR(vp)){
  93. SysFreeString(bstrName);
  94. throw he;
  95. }
  96. if((wcValue == NULL) || (0 != _wcsicmp(wcValue, L""))){
  97. hr = pObj->Put(bstrName, 0, vp, NULL);
  98. if(FAILED(hr)){
  99. SysFreeString(bstrName);
  100. VariantClear(vp);
  101. delete vp;
  102. throw hr;
  103. }
  104. }else hr = WBEM_E_FAILED;
  105. SysFreeString(bstrName);
  106. VariantClear(vp);
  107. delete vp;
  108. return hr;
  109. }
  110. HRESULT CGenericClass::PutProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, int iValue)
  111. {
  112. HRESULT hr = WBEM_S_NO_ERROR;
  113. BSTR bstrName = SysAllocString(wcProperty);
  114. if(!bstrName)
  115. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);;
  116. VARIANT *pv = new VARIANT;
  117. if(iValue != POD_NULL_INTEGER){
  118. VariantInit(pv);
  119. V_VT(pv) = VT_I4;
  120. V_I4(pv) = iValue;
  121. hr = pObj->Put(bstrName, 0, pv, NULL);
  122. VariantClear(pv);
  123. if(FAILED(hr)){
  124. SysFreeString(bstrName);
  125. delete pv;
  126. throw hr;
  127. }
  128. }else hr = WBEM_E_FAILED;
  129. SysFreeString(bstrName);
  130. delete pv;
  131. return hr;
  132. }
  133. HRESULT CGenericClass::PutProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, float dValue)
  134. {
  135. HRESULT hr = WBEM_S_NO_ERROR;
  136. BSTR bstrName = SysAllocString(wcProperty);
  137. if(!bstrName)
  138. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);;
  139. VARIANT *pv = new VARIANT;
  140. VariantInit(pv);
  141. V_VT(pv) = VT_R4;
  142. V_R4(pv) = dValue;
  143. hr = pObj->Put(bstrName, 0, pv, NULL);
  144. SysFreeString(bstrName);
  145. VariantClear(pv);
  146. delete pv;
  147. if(FAILED(hr))
  148. throw hr;
  149. return hr;
  150. }
  151. HRESULT CGenericClass::PutProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, bool bValue)
  152. {
  153. HRESULT hr = WBEM_S_NO_ERROR;
  154. BSTR bstrName = SysAllocString(wcProperty);
  155. if(!bstrName)
  156. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);;
  157. VARIANT *pv = new VARIANT;
  158. VariantInit(pv);
  159. V_VT(pv) = VT_BOOL;
  160. if(bValue) V_BOOL(pv) = VARIANT_TRUE;
  161. else V_BOOL(pv) = VARIANT_FALSE;
  162. hr = pObj->Put(bstrName, 0, pv, NULL);
  163. SysFreeString(bstrName);
  164. VariantClear(pv);
  165. delete pv;
  166. if(FAILED(hr))
  167. throw hr;
  168. return hr;
  169. }
  170. HRESULT CGenericClass::PutProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, WCHAR *mszValue, CIMTYPE cimtype)
  171. {
  172. HRESULT hr = WBEM_S_NO_ERROR;
  173. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  174. long lCount=0;
  175. PWSTR pTemp;
  176. for ( pTemp = mszValue; pTemp != NULL && pTemp[0] != L'\0'; pTemp = pTemp + wcslen(pTemp)+1,lCount++);
  177. if ( lCount == 0 ) return hr; // nothing to save
  178. BSTR bstrName = SysAllocString(wcProperty);
  179. if(!bstrName)
  180. throw he;
  181. VARIANT v;
  182. SAFEARRAYBOUND sbArrayBounds ;
  183. sbArrayBounds.cElements = lCount;
  184. sbArrayBounds.lLbound = 0;
  185. if(V_ARRAY(&v) = SafeArrayCreate(VT_BSTR, 1, &sbArrayBounds)){
  186. V_VT(&v) = VT_BSTR | VT_ARRAY ;
  187. BSTR bstrVal;
  188. long j;
  189. //get each string in the MULTI-SZ string
  190. for(j = 0, pTemp = mszValue;
  191. j < lCount && pTemp != NULL && pTemp[0] != L'\0';
  192. j++, pTemp=pTemp+wcslen(pTemp)+1){
  193. bstrVal = SysAllocString(pTemp);
  194. SafeArrayPutElement(V_ARRAY(&v), &j, bstrVal);
  195. SysFreeString(bstrVal);
  196. }
  197. hr = pObj->Put(bstrName, 0, &v, NULL);
  198. if ( FAILED(hr) ) {
  199. SysFreeString(bstrName);
  200. VariantClear(&v);
  201. throw hr;
  202. }
  203. }else hr = WBEM_E_FAILED;
  204. SysFreeString(bstrName);
  205. VariantClear(&v);
  206. return hr;
  207. }
  208. HRESULT CGenericClass::PutKeyProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, WCHAR *wcValue,
  209. bool *bKey, CRequestObject *pRequest)
  210. {
  211. HRESULT hr = WBEM_S_NO_ERROR;
  212. BSTR bstrName = SysAllocString(wcProperty);
  213. if(!bstrName)
  214. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);;
  215. VARIANT *pv = new VARIANT;
  216. VariantInit(pv);
  217. V_VT(pv) = VT_BSTR;
  218. #ifdef _STRIP_ESCAPED_CHARS
  219. V_BSTR(pv) = SysAllocString(ConvertToASCII(wcValue));
  220. #else
  221. V_BSTR(pv) = SysAllocString(wcValue);
  222. #endif //_STRIP_ESCAPED_CHARS
  223. if(!V_BSTR(pv))
  224. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);;
  225. if((wcValue == NULL) || (0 != wcscmp(wcValue, L""))){
  226. hr = pObj->Put(bstrName, 0, pv, NULL);
  227. if(FAILED(hr)){
  228. SysFreeString(bstrName);
  229. VariantClear(pv);
  230. delete pv;
  231. throw hr;
  232. }
  233. // Find the keys
  234. *bKey = false;
  235. int iPos = -1;
  236. if(FindIn(pRequest->m_Property, bstrName, &iPos) &&
  237. FindIn(pRequest->m_Value, V_BSTR(pv), &iPos)) *bKey = true;
  238. }else hr = WBEM_E_FAILED;
  239. SysFreeString(bstrName);
  240. VariantClear(pv);
  241. delete pv;
  242. return hr;
  243. }
  244. HRESULT CGenericClass::PutKeyProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, int iValue,
  245. bool *bKey, CRequestObject *pRequest)
  246. {
  247. HRESULT hr = WBEM_S_NO_ERROR;
  248. BSTR bstrName = SysAllocString(wcProperty);
  249. if(!bstrName)
  250. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);;
  251. VARIANT *pv = new VARIANT;
  252. WCHAR wcBuf[BUFF_SIZE];
  253. if(iValue != POD_NULL_INTEGER){
  254. VariantInit(pv);
  255. V_VT(pv) = VT_I4;
  256. V_I4(pv) = iValue;
  257. hr = pObj->Put(bstrName, 0, pv, NULL);
  258. VariantClear(pv);
  259. delete pv;
  260. if(FAILED(hr)){
  261. SysFreeString(bstrName);
  262. throw hr;
  263. }
  264. // Find the keys
  265. _itow(iValue, wcBuf, 10);
  266. BSTR bstrValue = SysAllocString(wcBuf);
  267. if(!bstrValue)
  268. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);;
  269. *bKey = false;
  270. int iPos = -1;
  271. if(FindIn(pRequest->m_Property, bstrName, &iPos) &&
  272. FindIn(pRequest->m_Value, bstrValue, &iPos)) *bKey = true;
  273. SysFreeString(bstrValue);
  274. }else hr = WBEM_E_FAILED;
  275. SysFreeString(bstrName);
  276. return hr;
  277. }
  278. bool CGenericClass::FindIn(BSTR bstrProp[], BSTR bstrSearch, int *iPos)
  279. {
  280. int i = 0;
  281. if(*iPos == (-1)){
  282. while(bstrProp[i] != NULL){
  283. if(0 == _wcsicmp(bstrProp[i], bstrSearch)){
  284. *iPos = i;
  285. return true;
  286. }
  287. i++;
  288. }
  289. }else{
  290. if(0 == _wcsicmp(bstrProp[*iPos], bstrSearch)) return true;
  291. }
  292. return false;
  293. }
  294. HRESULT CGenericClass::GetProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, WCHAR *wcValue)
  295. {
  296. HRESULT hr = WBEM_S_NO_ERROR;
  297. VARIANT v;
  298. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  299. BSTR bstrProp = SysAllocString(wcProperty);
  300. if(!bstrProp)
  301. throw he;
  302. VariantInit(&v);
  303. if(SUCCEEDED(hr = pObj->Get(bstrProp, 0, &v, NULL, NULL))){
  304. if(V_VT(&v) == VT_BSTR) wcscpy(wcValue, V_BSTR(&v));
  305. else if(V_VT(&v) == VT_EMPTY || V_VT(&v) == VT_NULL ) hr = WBEM_S_RESET_TO_DEFAULT;
  306. else wcscpy(wcValue, L"");
  307. }
  308. SysFreeString(bstrProp);
  309. VariantClear(&v);
  310. return hr;
  311. }
  312. HRESULT CGenericClass::GetProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, BSTR *wcValue)
  313. {
  314. HRESULT hr = WBEM_S_NO_ERROR;
  315. VARIANT v;
  316. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  317. BSTR bstrProp = SysAllocString(wcProperty);
  318. if(!bstrProp)
  319. throw he;
  320. VariantInit(&v);
  321. if(SUCCEEDED(hr = pObj->Get(bstrProp, 0, &v, NULL, NULL))){
  322. if(wcslen(V_BSTR(&v)) > INTERNET_MAX_PATH_LENGTH) return WBEM_E_INVALID_METHOD_PARAMETERS;
  323. if(V_VT(&v) == VT_BSTR) *wcValue = SysAllocString(V_BSTR(&v));
  324. else if(V_VT(&v) == VT_EMPTY || V_VT(&v) == VT_NULL ) hr = WBEM_S_RESET_TO_DEFAULT;
  325. else *wcValue = SysAllocString(L"");
  326. if(hr != WBEM_S_RESET_TO_DEFAULT && !wcValue)
  327. throw he;
  328. }
  329. SysFreeString(bstrProp);
  330. VariantClear(&v);
  331. return hr;
  332. }
  333. HRESULT CGenericClass::GetProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, int *piValue)
  334. {
  335. HRESULT hr = WBEM_S_NO_ERROR;
  336. VARIANT v;
  337. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  338. BSTR bstrProp = SysAllocString(wcProperty);
  339. if(!bstrProp)
  340. throw he;
  341. VariantInit(&v);
  342. if(SUCCEEDED(hr = pObj->Get(bstrProp, 0, &v, NULL, NULL))){
  343. if(V_VT(&v) == VT_I4) *piValue = V_I4(&v);
  344. else if(V_VT(&v) == VT_EMPTY || V_VT(&v) == VT_NULL ) hr = WBEM_S_RESET_TO_DEFAULT;
  345. else *piValue = 0;
  346. }
  347. SysFreeString(bstrProp);
  348. VariantClear(&v);
  349. return hr;
  350. }
  351. HRESULT CGenericClass::GetProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, bool *pbValue)
  352. {
  353. HRESULT hr = WBEM_S_NO_ERROR;
  354. VARIANT v;
  355. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  356. BSTR bstrProp = SysAllocString(wcProperty);
  357. if(!bstrProp)
  358. throw he;
  359. VariantInit(&v);
  360. if(SUCCEEDED(hr = pObj->Get(bstrProp, 0, &v, NULL, NULL))){
  361. if((V_VT(&v) == VT_BOOL) & V_BOOL(&v)) *pbValue = true;
  362. else if(V_VT(&v) == VT_EMPTY || V_VT(&v) == VT_NULL ) hr = WBEM_S_RESET_TO_DEFAULT;
  363. else *pbValue = false;
  364. }
  365. SysFreeString(bstrProp);
  366. VariantClear(&v);
  367. return hr;
  368. }
  369. WCHAR * CGenericClass::GetNextVar(WCHAR *pwcStart)
  370. {
  371. WCHAR *pwc = pwcStart;
  372. //get to end of variable
  373. while(*pwc){ pwc++; }
  374. return ++pwc;
  375. }
  376. long CGenericClass::GetVarCount(void * pEnv)
  377. {
  378. long lRetVal = 0;
  379. WCHAR *pwc = (WCHAR *)pEnv;
  380. //count the variables
  381. while(*pwc){
  382. //get to end of variable
  383. while(*pwc){ pwc++; }
  384. pwc++;
  385. lRetVal++;
  386. }
  387. return lRetVal;
  388. }