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.

315 lines
7.9 KiB

  1. /*++
  2. Copyright (C) 1995-2001 Microsoft Corporation
  3. Module Name:
  4. IMPDYNP.CPP
  5. Abstract:
  6. Defines the virtual base class for the Property Provider
  7. objects. The base class is overriden for each specific
  8. provider which provides the details of how an actual
  9. property "Put" or "Get" is done.
  10. History:
  11. a-davj 27-Sep-95 Created.
  12. --*/
  13. #include "precomp.h"
  14. //#define _MT
  15. #include <process.h>
  16. #include "impdyn.h"
  17. #include "CVariant.h"
  18. #include <genlex.h>
  19. #include <objpath.h>
  20. #include <genutils.h>
  21. #include <cominit.h>
  22. //***************************************************************************
  23. //
  24. // CImpDynProp::CImpDynProp
  25. //
  26. // DESCRIPTION:
  27. //
  28. // Constructor.
  29. //
  30. //***************************************************************************
  31. CImpDynProp::CImpDynProp()
  32. {
  33. m_pImpDynProv = NULL; // This is set in the derived class constructors.
  34. m_cRef=0;
  35. return;
  36. }
  37. //***************************************************************************
  38. //
  39. // CImpDynProp::~CImpDynProp
  40. //
  41. // DESCRIPTION:
  42. //
  43. // Destructor.
  44. //
  45. //***************************************************************************
  46. CImpDynProp::~CImpDynProp(void)
  47. {
  48. return;
  49. }
  50. //***************************************************************************
  51. // HRESULT CImpDynProp::QueryInterface
  52. // long CImpDynProp::AddRef
  53. // long CImpDynProp::Release
  54. //
  55. // DESCRIPTION:
  56. //
  57. // Standard Com IUNKNOWN functions.
  58. //
  59. //***************************************************************************
  60. STDMETHODIMP CImpDynProp::QueryInterface(
  61. REFIID riid,
  62. PPVOID ppv)
  63. {
  64. *ppv=NULL;
  65. // The only calls for IUnknown are either in a nonaggregated
  66. // case or when created in an aggregation, so in either case
  67. // always return our IUnknown for IID_IUnknown.
  68. if (IID_IUnknown==riid || IID_IWbemPropertyProvider == riid)
  69. *ppv=this;
  70. if (NULL!=*ppv)
  71. {
  72. ((LPUNKNOWN)*ppv)->AddRef();
  73. return NOERROR;
  74. }
  75. return ResultFromScode(E_NOINTERFACE);
  76. }
  77. STDMETHODIMP_(ULONG) CImpDynProp::AddRef(void)
  78. {
  79. return InterlockedIncrement(&m_cRef);
  80. }
  81. STDMETHODIMP_(ULONG) CImpDynProp::Release(void)
  82. {
  83. long lRet = InterlockedDecrement(&m_cRef);
  84. if (0L != lRet)
  85. return lRet;
  86. // Tell the housing that an object is going away so it can
  87. // shut down if appropriate.
  88. delete this; // do before decrementing module obj count.
  89. InterlockedDecrement(&lObj);
  90. return 0;
  91. }
  92. //***************************************************************************
  93. //
  94. // WCHAR * CImpDynProp::BuildString
  95. //
  96. // DESCRIPTION:
  97. //
  98. // Creates a concatenation of the mapping strings.
  99. //
  100. // PARAMETERS:
  101. //
  102. // ClassMapping Class Mapping string passed in by WBEM
  103. // InstMapping Instance Mapping string passed in by WBEM
  104. // PropMapping Property Mapping string passed in by WBEM
  105. //
  106. // RETURN VALUE:
  107. //
  108. // Pointer to the combined string. This must be freed by the caller
  109. // via "delete". NULL is return if low memory.
  110. //
  111. //***************************************************************************
  112. WCHAR * CImpDynProp::BuildString(
  113. IN BSTR ClassMapping,
  114. IN BSTR InstMapping,
  115. IN BSTR PropMapping)
  116. {
  117. int iLen = 3;
  118. if(ClassMapping)
  119. iLen += wcslen(ClassMapping);
  120. if(InstMapping)
  121. iLen += wcslen(InstMapping);
  122. if(PropMapping)
  123. iLen += wcslen(PropMapping);
  124. WCHAR * pNew = new WCHAR[iLen];
  125. if(pNew == NULL)
  126. return NULL;
  127. *pNew = NULL;
  128. if(ClassMapping)
  129. StringCchCatW(pNew, iLen, ClassMapping);
  130. if(InstMapping)
  131. StringCchCatW(pNew, iLen, InstMapping);
  132. if(PropMapping)
  133. StringCchCatW(pNew, iLen, PropMapping);
  134. return pNew;
  135. }
  136. //***************************************************************************
  137. //
  138. // STDMETHODIMP CImpDynProp::PutProperty
  139. //
  140. // DESCRIPTION:
  141. //
  142. // Writes data out to something like the registry.
  143. //
  144. // PARAMETERS:
  145. //
  146. // ClassMapping Class Mapping string passed in by WBEM
  147. // InstMapping Instance Mapping string passed in by WBEM
  148. // PropMapping Property Mapping string passed in by WBEM
  149. // pvValue Value to be put
  150. //
  151. // RETURN VALUE:
  152. //
  153. // S_OK all is well
  154. // WBEM_E_OUT_OF_MEMORY low memory
  155. // WBEM_E_INVALID_PARAMETER missing tokens
  156. // otherwise error code from OMSVariantChangeType, or UpdateProperty
  157. //
  158. //***************************************************************************
  159. STDMETHODIMP CImpDynProp::PutProperty(
  160. long lFlags,
  161. const BSTR Locale,
  162. IN const BSTR ClassMapping,
  163. IN const BSTR InstMapping,
  164. IN const BSTR PropMapping,
  165. IN const VARIANT *pvValue)
  166. {
  167. SCODE sc;
  168. if(IsNT())
  169. {
  170. sc = WbemCoImpersonateClient();
  171. if(FAILED(sc))
  172. return sc;
  173. }
  174. WCHAR * pNew = BuildString(ClassMapping, InstMapping, PropMapping);
  175. if(pNew == NULL)
  176. return WBEM_E_OUT_OF_MEMORY;
  177. if(wcslen(pNew) == 3)
  178. {
  179. delete pNew;
  180. return WBEM_E_INVALID_PARAMETER;
  181. }
  182. CObject * pPackageObj = NULL;
  183. sc = m_pImpDynProv->StartBatch(0,NULL,&pPackageObj,FALSE);
  184. if(sc != S_OK)
  185. {
  186. delete pNew;
  187. return WBEM_E_OUT_OF_MEMORY;
  188. }
  189. CVariant cVar;
  190. sc = OMSVariantChangeType(cVar.GetVarPtr(), (VARIANT *)pvValue, 0, pvValue->vt);
  191. if(sc == S_OK)
  192. {
  193. CProvObj ProvObj(pNew,MAIN_DELIM,NeedsEscapes());
  194. sc = m_pImpDynProv->UpdateProperty(0,NULL, NULL, ProvObj, pPackageObj, &cVar);
  195. }
  196. delete pNew;
  197. m_pImpDynProv->EndBatch(0, NULL,pPackageObj, FALSE);
  198. return sc;
  199. }
  200. //***************************************************************************
  201. //
  202. // STDMETHODIMP CImpDynProp::GetProperty
  203. //
  204. // DESCRIPTION:
  205. //
  206. // Gets data from something like the registry.
  207. //
  208. // PARAMETERS:
  209. //
  210. // ClassMapping Class Mapping string passed in by WBEM
  211. // InstMapping Instance Mapping string passed in by WBEM
  212. // PropMapping Property Mapping string passed in by WBEM
  213. // pvValue Value to be put
  214. //
  215. // RETURN VALUE:
  216. //
  217. // S_OK all is well
  218. // WBEM_E_OUT_OF_MEMORY low memory
  219. // WBEM_E_INVALID_PARAMETER missing tokens
  220. // otherwise error code from RefreshProperty
  221. //
  222. //***************************************************************************
  223. STDMETHODIMP CImpDynProp::GetProperty(
  224. long lFlags,
  225. const BSTR Locale,
  226. IN const BSTR ClassMapping,
  227. IN const BSTR InstMapping,
  228. IN const BSTR PropMapping,
  229. OUT IN VARIANT *pvValue)
  230. {
  231. SCODE sc;
  232. if(IsNT())
  233. {
  234. sc = WbemCoImpersonateClient();
  235. if(FAILED(sc))
  236. return sc;
  237. }
  238. WCHAR * pNew = BuildString(ClassMapping, InstMapping, PropMapping);
  239. memset((void *)&(pvValue->bstrVal),0,8);
  240. if(pNew == NULL)
  241. return WBEM_E_OUT_OF_MEMORY;
  242. if(wcslen(pNew) == 3)
  243. {
  244. delete pNew;
  245. return WBEM_E_INVALID_PARAMETER;
  246. }
  247. CObject * pPackageObj = NULL;
  248. sc = m_pImpDynProv->StartBatch(0,NULL,&pPackageObj,TRUE);
  249. if(sc != S_OK)
  250. {
  251. delete pNew;
  252. return WBEM_E_OUT_OF_MEMORY;
  253. }
  254. CVariant cVar;
  255. CProvObj ProvObj(pNew,MAIN_DELIM,NeedsEscapes());
  256. sc = m_pImpDynProv->RefreshProperty(0, NULL, NULL, ProvObj, pPackageObj, &cVar, FALSE);
  257. if(sc == S_OK)
  258. sc = VariantCopy(pvValue, cVar.GetVarPtr());
  259. delete pNew;
  260. m_pImpDynProv->EndBatch(0,NULL,pPackageObj, TRUE);
  261. return sc;
  262. }