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.

400 lines
7.7 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 "persistmgr.h"
  8. #include <wininet.h>
  9. #define READ_HANDLE 0
  10. #define WRITE_HANDLE 1
  11. /*
  12. Routine Description:
  13. Name:
  14. CGenericClass::CGenericClass
  15. Functionality:
  16. This is the constructor. Pass along the parameters to the base class
  17. Virtual:
  18. No (you know that, constructor won't be virtual!)
  19. Arguments:
  20. pKeyChain - Pointer to the ISceKeyChain COM interface which is prepared
  21. by the caller who constructs this instance.
  22. pNamespace - Pointer to WMI namespace of our provider (COM interface).
  23. Passed along by the caller. Must not be NULL.
  24. pCtx - Pointer to WMI context object (COM interface). Passed along
  25. by the caller. It's up to WMI whether this interface pointer is NULL or not.
  26. Return Value:
  27. None as any constructor
  28. Notes:
  29. if you add more members, think about initialize them here
  30. */
  31. CGenericClass::CGenericClass (
  32. IN ISceKeyChain * pKeyChain,
  33. IN IWbemServices * pNamespace,
  34. IN IWbemContext * pCtx
  35. )
  36. :
  37. m_srpKeyChain(pKeyChain),
  38. m_srpNamespace(pNamespace),
  39. m_srpCtx(pCtx)
  40. {
  41. }
  42. /*
  43. Routine Description:
  44. Name:
  45. CGenericClass::~CGenericClass
  46. Functionality:
  47. Destructor. Good C++ discipline requires this to be virtual.
  48. Virtual:
  49. Yes.
  50. Arguments:
  51. none as any destructor
  52. Return Value:
  53. None as any destructor
  54. Notes:
  55. if you add more members, think about whether
  56. there is any need for a non-trivial destructor
  57. */
  58. CGenericClass::~CGenericClass()
  59. {
  60. CleanUp();
  61. }
  62. /*
  63. Routine Description:
  64. Name:
  65. CGenericClass::CleanUp
  66. Functionality:
  67. Destructor. Good C++ discipline requires this to be virtual.
  68. Virtual:
  69. Yes.
  70. Arguments:
  71. none.
  72. Return Value:
  73. None.
  74. Notes:
  75. if you add more members, think about whether
  76. there is any need for a non-trivial destructor
  77. */
  78. void CGenericClass::CleanUp()
  79. {
  80. //
  81. // Please note: CComPtr<XXX>.Release is a different function, rather than
  82. // delegating to the wrapped pointer. Basically, this does a Release call
  83. // to the wrapped pointer if it is non NULL and then set the wrapped pointer
  84. // to NULL. So, don't replace these CComPtr<XXX>.Release with
  85. // CComPtr<XXX>->Release. That will be a big mistake.
  86. //
  87. //
  88. // Read ReadMe.doc for information regarding releasing CComPtr<XXX>.
  89. //
  90. m_srpNamespace.Release();
  91. m_srpClassForSpawning.Release();
  92. m_srpCtx.Release();
  93. m_srpKeyChain.Release();
  94. }
  95. /*
  96. Routine Description:
  97. Name:
  98. CGenericClass::SpawnAnInstance
  99. Functionality:
  100. Creating a WMI class instance is a two step process.
  101. (1) First, we must get the class definition. That is done by m_srpNamespace->GetObject.
  102. (2) Secondly, we Spawn an instance.
  103. Object (IWbemClassObject) pointers created in this fashion can be used to fill in
  104. properties. Object pointers returned by m_srpNamespace->GetObject can NOT be used to
  105. fill in properties. That is the major reason why we need this function.
  106. Virtual:
  107. No.
  108. Arguments:
  109. none.
  110. Return Value:
  111. None.
  112. Notes:
  113. */
  114. HRESULT
  115. CGenericClass::SpawnAnInstance (
  116. OUT IWbemClassObject **ppObj
  117. )
  118. {
  119. if (ppObj == NULL)
  120. {
  121. return WBEM_E_INVALID_PARAMETER;
  122. }
  123. else if (m_srpKeyChain == NULL)
  124. {
  125. return WBEM_E_INVALID_OBJECT;
  126. }
  127. *ppObj = NULL;
  128. HRESULT hr = WBEM_NO_ERROR;
  129. //
  130. // Need ask WMI for the class definition so that we can spawn an instance of it.
  131. //
  132. if (!m_srpClassForSpawning)
  133. {
  134. CComBSTR bstrClassName;
  135. //
  136. // must return success code if something is got.
  137. //
  138. hr = m_srpKeyChain->GetClassName(&bstrClassName);
  139. if (SUCCEEDED(hr))
  140. {
  141. hr = m_srpNamespace->GetObject(bstrClassName, 0, m_srpCtx, &m_srpClassForSpawning, NULL);
  142. }
  143. }
  144. //
  145. // Now, let's spawn one that can be used to fill in properties.
  146. // This instance will be blank except those with default values. All properties
  147. // with default values are properly filled up with a spawned instance.
  148. //
  149. if (SUCCEEDED(hr))
  150. {
  151. hr = m_srpClassForSpawning->SpawnInstance(0, ppObj);
  152. }
  153. return hr;
  154. }
  155. /*
  156. Routine Description:
  157. Name:
  158. ProvSceStatusToDosError
  159. Functionality:
  160. converts SCESTATUS error code to dos error defined in winerror.h
  161. Virtual:
  162. No.
  163. Arguments:
  164. none.
  165. Return Value:
  166. None.
  167. Notes:
  168. */
  169. DWORD
  170. ProvSceStatusToDosError (
  171. IN SCESTATUS SceStatus
  172. )
  173. {
  174. switch(SceStatus) {
  175. case SCESTATUS_SUCCESS:
  176. return(NO_ERROR);
  177. case SCESTATUS_OTHER_ERROR:
  178. return(ERROR_EXTENDED_ERROR);
  179. case SCESTATUS_INVALID_PARAMETER:
  180. return(ERROR_INVALID_PARAMETER);
  181. case SCESTATUS_RECORD_NOT_FOUND:
  182. return(ERROR_NO_MORE_ITEMS);
  183. case SCESTATUS_NO_MAPPING:
  184. return(ERROR_NONE_MAPPED);
  185. case SCESTATUS_TRUST_FAIL:
  186. return(ERROR_TRUSTED_DOMAIN_FAILURE);
  187. case SCESTATUS_INVALID_DATA:
  188. return(ERROR_INVALID_DATA);
  189. case SCESTATUS_OBJECT_EXIST:
  190. return(ERROR_FILE_EXISTS);
  191. case SCESTATUS_BUFFER_TOO_SMALL:
  192. return(ERROR_INSUFFICIENT_BUFFER);
  193. case SCESTATUS_PROFILE_NOT_FOUND:
  194. return(ERROR_FILE_NOT_FOUND);
  195. case SCESTATUS_BAD_FORMAT:
  196. return(ERROR_BAD_FORMAT);
  197. case SCESTATUS_NOT_ENOUGH_RESOURCE:
  198. return(ERROR_NOT_ENOUGH_MEMORY);
  199. case SCESTATUS_ACCESS_DENIED:
  200. return(ERROR_ACCESS_DENIED);
  201. case SCESTATUS_CANT_DELETE:
  202. return(ERROR_CURRENT_DIRECTORY);
  203. case SCESTATUS_PREFIX_OVERFLOW:
  204. return(ERROR_BUFFER_OVERFLOW);
  205. case SCESTATUS_ALREADY_RUNNING:
  206. return(ERROR_SERVICE_ALREADY_RUNNING);
  207. case SCESTATUS_SERVICE_NOT_SUPPORT:
  208. return(ERROR_NOT_SUPPORTED);
  209. case SCESTATUS_MOD_NOT_FOUND:
  210. return(ERROR_MOD_NOT_FOUND);
  211. case SCESTATUS_EXCEPTION_IN_SERVER:
  212. return(ERROR_EXCEPTION_IN_SERVICE);
  213. default:
  214. return(ERROR_EXTENDED_ERROR);
  215. }
  216. }
  217. /*
  218. Routine Description:
  219. Name:
  220. ProvDosErrorToWbemError
  221. Functionality:
  222. converts SCESTATUS error code to dos error defined in winerror.h
  223. Virtual:
  224. No.
  225. Arguments:
  226. none.
  227. Return Value:
  228. None.
  229. Notes:
  230. */
  231. HRESULT
  232. ProvDosErrorToWbemError(
  233. IN DWORD rc
  234. )
  235. {
  236. switch(rc) {
  237. case NO_ERROR:
  238. return(WBEM_S_NO_ERROR);
  239. case ERROR_INVALID_PARAMETER:
  240. return(WBEM_E_INVALID_PARAMETER);
  241. case ERROR_NO_MORE_ITEMS:
  242. case ERROR_NONE_MAPPED:
  243. case ERROR_FILE_NOT_FOUND:
  244. case ERROR_MOD_NOT_FOUND:
  245. return(WBEM_E_NOT_FOUND);
  246. case ERROR_INVALID_DATA:
  247. case ERROR_BAD_FORMAT:
  248. return(WBEM_E_INVALID_CONTEXT);
  249. case ERROR_FILE_EXISTS:
  250. case ERROR_SERVICE_ALREADY_RUNNING:
  251. return(WBEM_S_ALREADY_EXISTS);
  252. case ERROR_INSUFFICIENT_BUFFER:
  253. return(WBEM_E_BUFFER_TOO_SMALL);
  254. case ERROR_NOT_ENOUGH_MEMORY:
  255. return(WBEM_E_OUT_OF_MEMORY);
  256. case ERROR_ACCESS_DENIED:
  257. return(WBEM_E_ACCESS_DENIED);
  258. case ERROR_BUFFER_OVERFLOW:
  259. return(WBEM_E_QUEUE_OVERFLOW);
  260. case ERROR_NOT_SUPPORTED:
  261. return(WBEM_E_NOT_SUPPORTED);
  262. default:
  263. return(WBEM_E_FAILED);
  264. }
  265. }