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.

239 lines
6.5 KiB

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