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.

348 lines
8.9 KiB

  1. // SoftwareFeatureSofwareElements.cpp: implementation of the CSoftwareFeatureSofwareElements class.
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "SoftwareFeatureSoftwareElements.h"
  8. #include "ExtendString.h"
  9. #include "ExtendQuery.h"
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CSoftwareFeatureSofwareElements::CSoftwareFeatureSofwareElements(CRequestObject *pObj, IWbemServices *pNamespace,
  14. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  15. {
  16. }
  17. CSoftwareFeatureSofwareElements::~CSoftwareFeatureSofwareElements()
  18. {
  19. }
  20. HRESULT CSoftwareFeatureSofwareElements::CreateObject(IWbemObjectSink *pHandler, ACTIONTYPE atAction)
  21. {
  22. HRESULT hr = WBEM_S_NO_ERROR;
  23. MSIHANDLE hView = NULL;
  24. MSIHANDLE hRecord = NULL;
  25. int i = -1;
  26. WCHAR wcBuf[BUFF_SIZE];
  27. WCHAR wcProductCode[39];
  28. WCHAR wcID[39];
  29. WCHAR wcProp[BUFF_SIZE];
  30. WCHAR wcFeature[BUFF_SIZE];
  31. WCHAR wcElement[BUFF_SIZE];
  32. DWORD dwBufSize;
  33. bool bMatch = false;
  34. UINT uiStatus;
  35. //These will change from class to class
  36. bool bFeature, bElement;
  37. bool bFeatureRestrict = false;
  38. bool bElementRestrict = false;
  39. if(atAction != ACTIONTYPE_ENUM)
  40. {
  41. // we are doing GetObject so we need to be reinitialized
  42. hr = WBEM_E_NOT_FOUND;
  43. int iPos = -1;
  44. BSTR bstrName = SysAllocString(L"GroupComponent");
  45. if ( bstrName )
  46. {
  47. if(FindIn(m_pRequest->m_Property, bstrName, &iPos))
  48. {
  49. CRequestObject *pFeature = new CRequestObject();
  50. if(!pFeature) throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  51. pFeature->Initialize(m_pNamespace);
  52. if(pFeature->ParsePath(m_pRequest->m_Value[iPos]))
  53. {
  54. iPos = -1;
  55. SysFreeString(bstrName);
  56. bstrName = SysAllocString(L"IdentifyingNumber");
  57. if ( bstrName )
  58. {
  59. if(FindIn(pFeature->m_Property, bstrName, &iPos))
  60. {
  61. if ( ::SysStringLen ( pFeature->m_Value[iPos] ) == 38 )
  62. {
  63. //Get the product code we're looking for
  64. wcscpy(wcID, pFeature->m_Value[iPos]);
  65. }
  66. else
  67. {
  68. // we are not good to go, they have sent us longer string
  69. SysFreeString ( bstrName );
  70. throw hr;
  71. }
  72. iPos = -1;
  73. SysFreeString(bstrName);
  74. bstrName = SysAllocString(L"Name");
  75. if ( bstrName )
  76. {
  77. if(FindIn(pFeature->m_Property, bstrName, &iPos))
  78. {
  79. if ( ::SysStringLen ( pFeature->m_Value[iPos] ) <= BUFF_SIZE )
  80. {
  81. //Get the product code we're looking for
  82. wcscpy(wcFeature, pFeature->m_Value[iPos]);
  83. bFeatureRestrict = true;
  84. }
  85. else
  86. {
  87. // we are not good to go, they have sent us longer string
  88. SysFreeString ( bstrName );
  89. throw hr;
  90. }
  91. }
  92. }
  93. else
  94. {
  95. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  96. }
  97. }
  98. }
  99. else
  100. {
  101. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  102. }
  103. }
  104. pFeature->Cleanup();
  105. delete pFeature;
  106. pFeature = NULL;
  107. }
  108. SysFreeString(bstrName);
  109. }
  110. else
  111. {
  112. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  113. }
  114. iPos = -1;
  115. bstrName = SysAllocString(L"PartComponent");
  116. if ( bstrName )
  117. {
  118. if(FindIn(m_pRequest->m_Property, bstrName, &iPos))
  119. {
  120. CRequestObject *pElement = new CRequestObject();
  121. if(!pElement) throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  122. pElement->Initialize(m_pNamespace);
  123. if(pElement->ParsePath(m_pRequest->m_Value[iPos]))
  124. {
  125. iPos = -1;
  126. SysFreeString(bstrName);
  127. bstrName = SysAllocString(L"Name");
  128. if ( bstrName )
  129. {
  130. if(FindIn(pElement->m_Property, bstrName, &iPos))
  131. {
  132. if ( ::SysStringLen ( pElement->m_Value[iPos] ) <= BUFF_SIZE )
  133. {
  134. //Get the product code we're looking for
  135. wcscpy(wcElement, pElement->m_Value[iPos]);
  136. bElementRestrict = true;
  137. }
  138. else
  139. {
  140. // we are not good to go, they have sent us longer string
  141. SysFreeString ( bstrName );
  142. throw hr;
  143. }
  144. }
  145. }
  146. else
  147. {
  148. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  149. }
  150. }
  151. pElement->Cleanup();
  152. delete pElement;
  153. pElement = NULL;
  154. }
  155. SysFreeString(bstrName);
  156. }
  157. else
  158. {
  159. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  160. }
  161. }
  162. Query wcQuery;
  163. wcQuery.Append ( 1, L"select distinct `Component_`, `Feature_` from FeatureComponents" );
  164. //optimize for GetObject
  165. if ( bElementRestrict || bFeatureRestrict )
  166. {
  167. if ( bFeatureRestrict )
  168. {
  169. wcQuery.Append ( 3, L" where `Feature_`=\'", wcFeature, L"\'" );
  170. }
  171. if ( bElementRestrict )
  172. {
  173. if ( bFeatureRestrict )
  174. {
  175. wcQuery.Append ( 3, L" or `Component_`=\'", wcElement, L"\'" );
  176. }
  177. else
  178. {
  179. wcQuery.Append ( 3, L" where `Component_`=\'", wcElement, L"\'" );
  180. }
  181. }
  182. }
  183. LPWSTR Buffer = NULL;
  184. LPWSTR dynBuffer = NULL;
  185. DWORD dwDynBuffer = 0L;
  186. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  187. {
  188. // safe operation:
  189. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  190. wcscpy(wcProductCode, m_pRequest->Package(i));
  191. //This trims the number of times we itterate on getobject calls
  192. if((atAction == ACTIONTYPE_ENUM) || !bFeatureRestrict ||
  193. (0 == _wcsicmp(m_pRequest->Package(i), wcID))){
  194. //Open our database
  195. try
  196. {
  197. if ( GetView ( &hView, wcProductCode, wcQuery, L"FeatureComponents", TRUE, FALSE ) )
  198. {
  199. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  200. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  201. CheckMSI(uiStatus);
  202. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  203. //----------------------------------------------------
  204. dwBufSize = BUFF_SIZE;
  205. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  206. dwBufSize = BUFF_SIZE;
  207. uiStatus = CreateSoftwareElementString ( msidata.GetDatabase(),
  208. Buffer,
  209. wcProductCode,
  210. wcProp,
  211. &dwBufSize
  212. );
  213. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  214. {
  215. dynBuffer [ 0 ] = 0;
  216. }
  217. if( uiStatus == ERROR_SUCCESS )
  218. {
  219. PutKeyProperty(m_pObj, pPartComponent, wcProp, &bElement, m_pRequest);
  220. dwBufSize = BUFF_SIZE;
  221. GetBufferToPut ( hRecord, 2, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  222. if ( CreateSoftwareFeatureString ( Buffer, wcProductCode, wcProp, true ) )
  223. {
  224. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  225. {
  226. dynBuffer [ 0 ] = 0;
  227. }
  228. PutKeyProperty(m_pObj, pGroupComponent, wcProp, &bFeature, m_pRequest);
  229. //----------------------------------------------------
  230. if(bFeature && bElement) bMatch = true;
  231. if((atAction != ACTIONTYPE_GET) || bMatch){
  232. hr = pHandler->Indicate(1, &m_pObj);
  233. }
  234. }
  235. else
  236. {
  237. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  238. {
  239. dynBuffer [ 0 ] = 0;
  240. }
  241. }
  242. }
  243. m_pObj->Release();
  244. m_pObj = NULL;
  245. g_fpMsiCloseHandle(hRecord);
  246. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  247. }//while
  248. }//if
  249. }
  250. catch(...)
  251. {
  252. if ( dynBuffer )
  253. {
  254. delete [] dynBuffer;
  255. dynBuffer = NULL;
  256. }
  257. g_fpMsiCloseHandle(hRecord);
  258. g_fpMsiViewClose(hView);
  259. g_fpMsiCloseHandle(hView);
  260. msidata.CloseDatabase ();
  261. if(m_pObj)
  262. {
  263. m_pObj->Release();
  264. m_pObj = NULL;
  265. }
  266. throw;
  267. }
  268. g_fpMsiCloseHandle(hRecord);
  269. g_fpMsiViewClose(hView);
  270. g_fpMsiCloseHandle(hView);
  271. msidata.CloseDatabase ();
  272. }//if
  273. }//while
  274. if ( dynBuffer )
  275. {
  276. delete [] dynBuffer;
  277. dynBuffer = NULL;
  278. }
  279. return hr;
  280. }