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.8 KiB

  1. // SoftwareElementServiceControl.cpp: implementation of the CSoftwareElementServiceControl class.
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "SoftwareElementServiceControl.h"
  8. #include "ExtendString.h"
  9. #include "ExtendQuery.h"
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CSoftwareElementServiceControl::CSoftwareElementServiceControl(CRequestObject *pObj, IWbemServices *pNamespace,
  14. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  15. {
  16. }
  17. CSoftwareElementServiceControl::~CSoftwareElementServiceControl()
  18. {
  19. }
  20. HRESULT CSoftwareElementServiceControl::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 wcQuery[BUFF_SIZE];
  28. WCHAR wcProductCode[39];
  29. WCHAR wcProp[BUFF_SIZE];
  30. DWORD dwBufSize;
  31. bool bMatch = false;
  32. UINT uiStatus;
  33. //These will change from class to class
  34. bool bEnvironment, bElement;
  35. // safe operation
  36. // lenght is smaller than BUFF_SIZE ( 512 )
  37. wcscpy(wcQuery, L"select distinct `Component_`, `ServiceControl` from ServiceControl");
  38. LPWSTR Buffer = NULL;
  39. LPWSTR dynBuffer = NULL;
  40. DWORD dwDynBuffer = 0L;
  41. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  42. {
  43. // safe operation:
  44. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  45. wcscpy(wcProductCode, m_pRequest->Package(i));
  46. //Open our database
  47. try
  48. {
  49. if ( GetView ( &hView, wcProductCode, wcQuery, L"ServiceControl", TRUE, FALSE ) )
  50. {
  51. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  52. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  53. CheckMSI(uiStatus);
  54. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  55. //----------------------------------------------------
  56. dwBufSize = BUFF_SIZE;
  57. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  58. dwBufSize = BUFF_SIZE;
  59. uiStatus = CreateSoftwareElementString ( msidata.GetDatabase(),
  60. Buffer,
  61. wcProductCode,
  62. wcProp,
  63. &dwBufSize
  64. );
  65. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  66. {
  67. dynBuffer [ 0 ] = 0;
  68. }
  69. if( uiStatus == ERROR_SUCCESS )
  70. {
  71. PutKeyProperty(m_pObj, pElement, wcProp, &bElement, m_pRequest);
  72. dwBufSize = BUFF_SIZE;
  73. GetBufferToPut ( hRecord, 2, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  74. if ( Buffer && Buffer [ 0 ] != 0 )
  75. {
  76. DWORD dwConstant = 0L;
  77. DWORD dwBuf = 0L;
  78. DWORD dwProductCode = 0L;
  79. dwBuf = wcslen ( Buffer );
  80. dwProductCode = wcslen ( wcProductCode );
  81. dwConstant = wcslen ( L"Win32_ServiceControl.ID=\"" ) + wcslen ( L"\",ProductCode=\"" ) + wcslen ( L"\"" );
  82. if ( dwConstant + dwBuf + dwProductCode + 1 < BUFF_SIZE )
  83. {
  84. wcscpy(wcProp, L"Win32_ServiceControl.ID=\"");
  85. wcscat(wcProp, Buffer);
  86. wcscat(wcProp, L"\",ProductCode=\"");
  87. wcscat(wcProp, wcProductCode);
  88. wcscat(wcProp, L"\"");
  89. PutKeyProperty(m_pObj, pSetting, wcProp, &bEnvironment, m_pRequest);
  90. }
  91. else
  92. {
  93. LPWSTR wsz = NULL;
  94. try
  95. {
  96. if ( ( wsz = new WCHAR [ dwConstant + dwBuf + dwProductCode + 1 ] ) != NULL )
  97. {
  98. wcscpy ( wsz, L"Win32_ServiceControl.ID=\"" );
  99. wcscat ( wsz, Buffer );
  100. wcscat ( wsz, L"\",ProductCode=\"" );
  101. wcscat ( wsz, wcProductCode );
  102. wcscat ( wsz, L"\"" );
  103. PutKeyProperty ( m_pObj, pSetting, wsz, &bEnvironment, m_pRequest );
  104. }
  105. else
  106. {
  107. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  108. }
  109. }
  110. catch ( ... )
  111. {
  112. if ( wsz )
  113. {
  114. delete [] wsz;
  115. wsz = NULL;
  116. }
  117. throw;
  118. }
  119. if ( wsz )
  120. {
  121. delete [] wsz;
  122. wsz = NULL;
  123. }
  124. }
  125. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  126. {
  127. dynBuffer [ 0 ] = 0;
  128. }
  129. if(bEnvironment && bElement) bMatch = true;
  130. if((atAction != ACTIONTYPE_GET) || bMatch){
  131. hr = pHandler->Indicate(1, &m_pObj);
  132. }
  133. }
  134. }
  135. m_pObj->Release();
  136. m_pObj = NULL;
  137. g_fpMsiCloseHandle(hRecord);
  138. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  139. }
  140. }
  141. }
  142. catch(...)
  143. {
  144. if ( dynBuffer )
  145. {
  146. delete [] dynBuffer;
  147. dynBuffer = NULL;
  148. }
  149. g_fpMsiCloseHandle(hRecord);
  150. g_fpMsiViewClose(hView);
  151. g_fpMsiCloseHandle(hView);
  152. msidata.CloseDatabase ();
  153. if(m_pObj)
  154. {
  155. m_pObj->Release();
  156. m_pObj = NULL;
  157. }
  158. throw;
  159. }
  160. g_fpMsiCloseHandle(hRecord);
  161. g_fpMsiViewClose(hView);
  162. g_fpMsiCloseHandle(hView);
  163. msidata.CloseDatabase ();
  164. }
  165. if ( dynBuffer )
  166. {
  167. delete [] dynBuffer;
  168. dynBuffer = NULL;
  169. }
  170. return hr;
  171. }