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.

417 lines
11 KiB

  1. // podbase.cpp, implementation of CPodBase class
  2. // Copyright (c)1997-1999 Microsoft Corporation
  3. //
  4. //////////////////////////////////////////////////////////////////////
  5. #include "precomp.h"
  6. #include "podbase.h"
  7. #include <io.h>
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CPodBase::CPodBase(CRequestObject *pObj, IWbemServices *pNamespace,
  12. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  13. {
  14. }
  15. CPodBase::~CPodBase()
  16. {
  17. }
  18. //////////////////////////////////////////////////////////////////////
  19. // CPodBase::CreateObject
  20. //
  21. // Create one instance for the requested Sample_BaseClass
  22. //////////////////////////////////////////////////////////////////////
  23. HRESULT CPodBase::CreateObject(IWbemObjectSink *pHandler, ACTIONTYPE atAction)
  24. {
  25. HRESULT hr = WBEM_S_NO_ERROR;
  26. if ( ACTIONTYPE_DELETE == atAction ) {
  27. //
  28. // do not support enumeration because we don't know the scope of the request
  29. //
  30. hr = WBEM_E_NOT_SUPPORTED;
  31. } else if ( ACTIONTYPE_GET == atAction ||
  32. ACTIONTYPE_ENUM == atAction ) {
  33. // Create the instance
  34. //============================
  35. try{
  36. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  37. //----------------------------------------------------
  38. hr = PutProperty(m_pObj, pPodID, (PWSTR)szPodGUID);
  39. if ( SUCCEEDED(hr) ) {
  40. hr = pHandler->Indicate(1, &m_pObj);
  41. }
  42. m_pObj->Release();
  43. m_pObj = NULL;
  44. }catch(...){
  45. if(m_pObj){
  46. m_pObj->Release();
  47. m_pObj = NULL;
  48. }
  49. // Clean up
  50. // ========
  51. throw;
  52. }
  53. } else {
  54. //
  55. // not supported for now
  56. //
  57. hr = WBEM_E_NOT_SUPPORTED;
  58. }
  59. return hr;
  60. }
  61. //////////////////////////////////////////////////////////////////////
  62. // CPodBase::ExecMethod
  63. // execute static and non static methods defined in the class
  64. //////////////////////////////////////////////////////////////////////
  65. HRESULT CPodBase::ExecMethod(IN BSTR bstrMethod,
  66. IN bool bIsInstance,
  67. IN IWbemClassObject *pInParams,
  68. IN IWbemObjectSink *pHandler,
  69. IN IWbemContext *pCtx
  70. )
  71. {
  72. if ( pInParams == NULL || pHandler == NULL ) {
  73. return WBEM_E_INVALID_PARAMETER;
  74. }
  75. HRESULT hr=WBEM_S_NO_ERROR;
  76. if ( !bIsInstance ) {
  77. //Static Methods
  78. if(0 != _wcsicmp(bstrMethod, L"Configure"))
  79. hr = WBEM_E_NOT_SUPPORTED;
  80. } else {
  81. //Non-Static Methods
  82. hr = WBEM_E_NOT_SUPPORTED;
  83. }
  84. if ( FAILED(hr) ) return hr;
  85. //
  86. // parse the input parameters
  87. //
  88. BSTR bstrDatabase = NULL;
  89. BSTR bstrLog = NULL;
  90. LONG ulStatus = 0;
  91. BSTR bstrReturnValue = SysAllocString(L"ReturnValue");
  92. if(!bstrReturnValue) throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  93. IWbemClassObject *pClass = NULL;
  94. IWbemClassObject *pOutClass = NULL;
  95. IWbemClassObject *pOutParams = NULL;
  96. VARIANT v;
  97. if(SUCCEEDED(hr = m_pRequest->m_pNamespace->GetObject(m_pRequest->m_bstrClass,
  98. 0, pCtx, &pClass, NULL))){
  99. if(SUCCEEDED(hr = pClass->GetMethod(bstrMethod, 0, NULL, &pOutClass))){
  100. if(SUCCEEDED(hr = pOutClass->SpawnInstance(0, &pOutParams))){
  101. //Get DatabaseName
  102. hr = GetProperty(pInParams, pSceStorePath, &bstrDatabase);
  103. if ( hr == WBEM_S_RESET_TO_DEFAULT ) hr = WBEM_E_INVALID_METHOD_PARAMETERS;
  104. if(SUCCEEDED(hr)){
  105. if( SysStringLen(bstrDatabase) == 0 ) hr = WBEM_E_INVALID_METHOD_PARAMETERS;
  106. }
  107. if(SUCCEEDED(hr)){
  108. // get LogName, optional
  109. GetProperty(pInParams, pLogFilePath, &bstrLog);
  110. // now query data then configure this component
  111. hr = PodConfigure(pCtx, bstrDatabase, bstrLog, &ulStatus);
  112. if ( SUCCEEDED(hr) ) {
  113. //Set up ReturnValue
  114. VariantInit(&v);
  115. V_VT(&v) = VT_I4;
  116. V_I4(&v) = ulStatus;
  117. if(SUCCEEDED(hr = pOutParams->Put(bstrReturnValue, 0,
  118. &v, NULL)))
  119. pHandler->Indicate(1, &pOutParams);
  120. }
  121. }
  122. pOutParams->Release();
  123. }
  124. pOutClass->Release();
  125. }
  126. pClass->Release();
  127. }
  128. if ( bstrDatabase ) SysFreeString(bstrDatabase);
  129. if ( bstrLog ) SysFreeString(bstrLog);
  130. SysFreeString(bstrReturnValue);
  131. return hr;
  132. }
  133. //////////////////////////////////////////////////////////////////////
  134. // CPodBase::PodConfigure
  135. //
  136. // Configure the Pod using data defined for the Pod
  137. // for this sample app, it just creates/sets the data to registry
  138. //////////////////////////////////////////////////////////////////////
  139. HRESULT CPodBase::PodConfigure(IWbemContext *pCtx, BSTR bstrDb, BSTR bstrLog, LONG *pStatus)
  140. {
  141. if ( !bstrDb || !pStatus ) return WBEM_E_INVALID_PARAMETER;
  142. *pStatus = 0;
  143. HRESULT hr=WBEM_S_NO_ERROR;
  144. //
  145. // query data from the store
  146. //
  147. DWORD Len = SysStringLen(bstrDb);
  148. WCHAR *pQuery =TEXT("SELECT * FROM Sce_PodData WHERE SceStorePath=\"");
  149. WCHAR *pQuery2 =TEXT("\" AND PodID=\"");
  150. WCHAR *pQuery3 = TEXT("\" AND PodSection=\"1\"");
  151. PWSTR tmp=(PWSTR)LocalAlloc(LPTR, (Len+wcslen(pQuery)+wcslen(pQuery2)+wcslen(szPodGUID)+wcslen(pQuery3)+2)*sizeof(WCHAR));
  152. if ( tmp == NULL ) return WBEM_E_OUT_OF_MEMORY;
  153. wcscpy(tmp, pQuery);
  154. wcscat(tmp, bstrDb);
  155. wcscat(tmp, pQuery2);
  156. wcscat(tmp, szPodGUID);
  157. wcscat(tmp, pQuery3);
  158. BSTR strQueryCategories = SysAllocString(tmp);
  159. LocalFree(tmp);
  160. IEnumWbemClassObject * pEnum = NULL;
  161. IWbemClassObject * pObj = NULL;
  162. ULONG n = 0;
  163. HKEY hKey1=NULL;
  164. BOOL bFindOne=FALSE;
  165. hr = m_pNamespace->ExecQuery(TEXT("WQL"),
  166. strQueryCategories,
  167. WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
  168. NULL,
  169. &pEnum);
  170. if (SUCCEEDED(hr))
  171. {
  172. //
  173. // get data
  174. //
  175. do {
  176. hr = pEnum->Next(WBEM_INFINITE, 1, &pObj, &n);
  177. if ( hr == WBEM_S_FALSE ) {
  178. if ( bFindOne ) {
  179. hr = WBEM_S_NO_ERROR;
  180. break;
  181. } else hr = WBEM_E_NOT_FOUND; // not find any
  182. }
  183. if ( SUCCEEDED(hr) && n > 0) {
  184. bFindOne = TRUE;
  185. //
  186. // find the instance
  187. //
  188. BSTR bstrKey=NULL;
  189. BSTR bstrValue=NULL;
  190. hr = GetProperty(pObj, pKey, &bstrKey);
  191. if ( SUCCEEDED(hr) )
  192. hr = GetProperty(pObj, pValue, &bstrValue);
  193. // log the operation
  194. LogOneRecord(pCtx, bstrLog, hr, (PWSTR)bstrKey, (PWSTR)bstrValue);
  195. if ( bstrKey && bstrValue ) {
  196. //
  197. // set the registry value
  198. //
  199. DWORD rc = RegCreateKey(HKEY_LOCAL_MACHINE, L"software\\microsoft\\windows nt\\currentversion\\secedit", &hKey1);
  200. if ( NO_ERROR == rc ) {
  201. rc = RegSetValueEx(hKey1, (PWSTR)bstrKey, 0, REG_SZ, (BYTE *)bstrValue, (wcslen(bstrValue)+1)*sizeof(WCHAR));
  202. RegCloseKey(hKey1);
  203. hKey1 = NULL;
  204. }
  205. if ( rc != NO_ERROR )
  206. *pStatus = rc;
  207. }
  208. if ( bstrKey ) {
  209. SysFreeString(bstrKey);
  210. bstrKey = NULL;
  211. }
  212. if ( bstrValue ) {
  213. SysFreeString(bstrValue);
  214. bstrValue = NULL;
  215. }
  216. }
  217. if (pObj)
  218. {
  219. pObj->Release();
  220. pObj = NULL;
  221. }
  222. } while ( SUCCEEDED(hr) );
  223. if ( WBEM_E_NOT_FOUND == hr )
  224. LogOneRecord(pCtx, bstrLog, hr, L"No data to configure", NULL);
  225. else if ( FAILED(hr) )
  226. LogOneRecord(pCtx, bstrLog, hr, L"Query pod data failed", NULL);
  227. }
  228. SysFreeString(strQueryCategories);
  229. if (pEnum)
  230. {
  231. pEnum->Release();
  232. }
  233. return hr;
  234. }
  235. //////////////////////////////////////////////////////////////////////
  236. // CPodBase::LogOneRecord
  237. //
  238. // Log a record for the Pod
  239. //////////////////////////////////////////////////////////////////////
  240. HRESULT CPodBase::LogOneRecord(IWbemContext *pCtx, BSTR bstrLog, HRESULT hrLog, PWSTR bufKey, PWSTR bufValue)
  241. {
  242. if ( !bstrLog ) return WBEM_E_INVALID_PARAMETER;
  243. //
  244. // build the log record string
  245. //
  246. DWORD Len=0;
  247. if ( bufKey ) Len += wcslen(bufKey) + 1;
  248. if ( bufValue ) Len += wcslen(bufValue) + 1;
  249. PWSTR tmp=(PWSTR)LocalAlloc(LPTR, (Len+2)*sizeof(WCHAR));
  250. if ( !tmp ) return WBEM_E_OUT_OF_MEMORY;
  251. if ( bufKey ) {
  252. wcscat(tmp, bufKey);
  253. wcscat(tmp, L"\t");
  254. }
  255. if ( bufValue ) {
  256. wcscat(tmp, bufValue);
  257. wcscat(tmp, L"\t");
  258. }
  259. BSTR bstrRecord=SysAllocString(tmp);
  260. LocalFree(tmp);
  261. if ( !bstrRecord ) return WBEM_E_OUT_OF_MEMORY;
  262. HRESULT hr = WBEM_S_NO_ERROR;
  263. //
  264. // get the log class
  265. //
  266. BSTR bstrClass=SysAllocString(L"Sce_ConfigurationLogRecord");
  267. if ( !bstrClass ) hr = WBEM_E_OUT_OF_MEMORY;
  268. IWbemClassObject *pClass=NULL;
  269. IWbemClassObject *pObj=NULL;
  270. if ( SUCCEEDED(hr) ) {
  271. //
  272. // create an instance of the log class
  273. //
  274. hr = m_pNamespace->GetObject(bstrClass, 0, pCtx, &pClass, NULL);
  275. if ( SUCCEEDED(hr) ) {
  276. hr = pClass->SpawnInstance(0, &pObj);
  277. }
  278. if ( SUCCEEDED(hr) ) {
  279. bool bName=FALSE;
  280. // fill in the properties of this class
  281. hr = PutKeyProperty(pObj, pLogFilePath, (PWSTR)bstrLog, &bName, m_pRequest);
  282. if (SUCCEEDED(hr) )
  283. hr = PutKeyProperty(pObj, pLogArea, (PWSTR)szPodGUID, &bName, m_pRequest);
  284. if (SUCCEEDED(hr) )
  285. hr = PutKeyProperty(pObj, pLogFileRecord, (PWSTR)bstrRecord, &bName, m_pRequest);
  286. if (SUCCEEDED(hr) )
  287. hr = PutProperty(pObj, pLogErrorCode, (int)hrLog);
  288. if ( SUCCEEDED(hr) ) {
  289. // save this instance
  290. hr = m_pNamespace->PutInstance( pObj, 0, pCtx, NULL );
  291. }
  292. }
  293. }
  294. if ( bstrClass ) SysFreeString(bstrClass);
  295. if ( bstrRecord ) SysFreeString(bstrRecord);
  296. if ( pClass ) {
  297. pClass->Release();
  298. }
  299. if ( pObj ) {
  300. pObj->Release();
  301. }
  302. return hr;
  303. }