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.

220 lines
5.6 KiB

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