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.

247 lines
6.9 KiB

  1. // SoftwareElementCondition.cpp: implementation of the CSoftwareElementCondition class.
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "SoftwareElementCondition.h"
  8. #include "ExtendString.h"
  9. //////////////////////////////////////////////////////////////////////
  10. // Construction/Destruction
  11. //////////////////////////////////////////////////////////////////////
  12. CSoftwareElementCondition::CSoftwareElementCondition(CRequestObject *pObj, IWbemServices *pNamespace,
  13. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  14. {
  15. }
  16. CSoftwareElementCondition::~CSoftwareElementCondition()
  17. {
  18. }
  19. HRESULT CSoftwareElementCondition::CreateObject(IWbemObjectSink *pHandler, ACTIONTYPE atAction)
  20. {
  21. HRESULT hr = WBEM_S_NO_ERROR;
  22. MSIHANDLE hView = NULL;
  23. MSIHANDLE hRecord = NULL;
  24. int i = -1;
  25. WCHAR wcBuf[BUFF_SIZE];
  26. WCHAR wcTmp[BUFF_SIZE];
  27. WCHAR wcQuery[BUFF_SIZE];
  28. WCHAR wcProductCode[39];
  29. DWORD dwBufSize;
  30. bool bMatch = false;
  31. UINT uiStatus;
  32. int iState;
  33. CStringExt wcProp;
  34. //These will change from class to class
  35. bool bCheck, bNull;
  36. INSTALLSTATE piInstalled;
  37. SetSinglePropertyPath(L"CheckID");
  38. WCHAR wcTestCode[39];
  39. //improve getobject performance by optimizing the query
  40. if(atAction != ACTIONTYPE_ENUM)
  41. {
  42. // we are doing GetObject so we need to be reinitialized
  43. hr = WBEM_E_NOT_FOUND;
  44. BSTR bstrCompare;
  45. int iPos = -1;
  46. bstrCompare = SysAllocString ( L"CheckID" );
  47. if ( bstrCompare )
  48. {
  49. if(FindIn(m_pRequest->m_Property, bstrCompare, &iPos))
  50. {
  51. if ( ::SysStringLen ( m_pRequest->m_Value[iPos] ) < BUFF_SIZE )
  52. {
  53. //Get the action we're looking for
  54. wcscpy(wcBuf, m_pRequest->m_Value[iPos]);
  55. // safe operation if wcslen ( wcBuf ) > 38
  56. if ( wcslen ( wcBuf ) > 38 )
  57. {
  58. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  59. }
  60. else
  61. {
  62. // we are not good to go, they have sent us longer string
  63. SysFreeString ( bstrCompare );
  64. throw hr;
  65. }
  66. }
  67. else
  68. {
  69. // we are not good to go, they have sent us longer string
  70. SysFreeString ( bstrCompare );
  71. throw hr;
  72. }
  73. }
  74. SysFreeString ( bstrCompare );
  75. }
  76. else
  77. {
  78. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  79. }
  80. }
  81. // safe operation
  82. // lenght is smaller than BUFF_SIZE ( 512 )
  83. wcscpy(wcQuery, L"select distinct `Component`, `ComponentId`, `Condition` from Component");
  84. LPWSTR Buffer = NULL;
  85. LPWSTR dynBuffer = NULL;
  86. DWORD dwDynBuffer = 0L;
  87. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  88. {
  89. // safe operation:
  90. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  91. wcscpy(wcProductCode, m_pRequest->Package(i));
  92. if((atAction == ACTIONTYPE_ENUM) || (_wcsicmp(wcTestCode, wcProductCode) == 0))
  93. {
  94. //Open our database
  95. try
  96. {
  97. if ( GetView ( &hView, wcProductCode, wcQuery, L"Component", FALSE, TRUE ) )
  98. {
  99. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  100. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  101. CheckMSI(uiStatus);
  102. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  103. //----------------------------------------------------
  104. dwBufSize = BUFF_SIZE;
  105. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  106. PutProperty(m_pObj, pName, Buffer);
  107. PutProperty(m_pObj, pCaption, Buffer);
  108. PutProperty(m_pObj, pDescription, Buffer);
  109. wcProp.Copy ( Buffer );
  110. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  111. {
  112. dynBuffer [ 0 ] = 0;
  113. }
  114. dwBufSize = BUFF_SIZE;
  115. CheckMSI(g_fpMsiRecordGetStringW(hRecord, 2, wcBuf, &dwBufSize));
  116. dwBufSize = BUFF_SIZE;
  117. piInstalled = g_fpMsiGetComponentPathW(wcProductCode, wcBuf, wcTmp, &dwBufSize);
  118. SoftwareElementState(piInstalled, &iState);
  119. PutProperty(m_pObj, pSoftwareElementState, iState);
  120. if(ValidateComponentID(wcBuf, wcProductCode)){
  121. PutProperty(m_pObj, pSoftwareElementID, wcBuf);
  122. PutProperty(m_pObj, pTargetOperatingSystem, GetOS());
  123. dwBufSize = BUFF_SIZE;
  124. CheckMSI(g_fpMsiGetProductPropertyW(msidata.GetProduct(), L"ProductVersion", wcBuf, &dwBufSize));
  125. PutProperty(m_pObj, pVersion, wcBuf);
  126. //====================================================
  127. dwBufSize = BUFF_SIZE;
  128. GetBufferToPut ( hRecord, 3, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  129. if( !Buffer || ( Buffer && 0 == Buffer [ 0 ] ) )
  130. {
  131. bNull = true;
  132. }
  133. else
  134. {
  135. bNull = false;
  136. }
  137. PutProperty(m_pObj, pCondition, Buffer);
  138. wcProp.Append ( 2, Buffer, wcProductCode );
  139. PutKeyProperty(m_pObj, pCheckID, wcProp, &bCheck, m_pRequest);
  140. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  141. {
  142. dynBuffer = NULL;
  143. }
  144. //----------------------------------------------------
  145. if(bCheck) bMatch = true;
  146. if(((atAction != ACTIONTYPE_GET) || bMatch) && !bNull){
  147. hr = pHandler->Indicate(1, &m_pObj);
  148. }
  149. }
  150. m_pObj->Release();
  151. m_pObj = NULL;
  152. g_fpMsiCloseHandle(hRecord);
  153. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  154. }
  155. }
  156. }
  157. catch(...)
  158. {
  159. if ( dynBuffer )
  160. {
  161. delete [] dynBuffer;
  162. dynBuffer = NULL;
  163. }
  164. g_fpMsiCloseHandle(hRecord);
  165. g_fpMsiViewClose(hView);
  166. g_fpMsiCloseHandle(hView);
  167. msidata.CloseProduct ();
  168. if(m_pObj)
  169. {
  170. m_pObj->Release();
  171. m_pObj = NULL;
  172. }
  173. throw;
  174. }
  175. g_fpMsiCloseHandle(hRecord);
  176. g_fpMsiViewClose(hView);
  177. g_fpMsiCloseHandle(hView);
  178. msidata.CloseProduct ();
  179. }
  180. }
  181. if ( dynBuffer )
  182. {
  183. delete [] dynBuffer;
  184. dynBuffer = NULL;
  185. }
  186. return hr;
  187. }