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.

198 lines
5.3 KiB

  1. // ServiceSpecificationService.cpp: implementation of the CServiceSpecificationService class.
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "ServiceSpecificationService.h"
  8. #include "ExtendString.h"
  9. //////////////////////////////////////////////////////////////////////
  10. // Construction/Destruction
  11. //////////////////////////////////////////////////////////////////////
  12. CServiceSpecificationService::CServiceSpecificationService(CRequestObject *pObj, IWbemServices *pNamespace,
  13. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  14. {
  15. }
  16. CServiceSpecificationService::~CServiceSpecificationService()
  17. {
  18. }
  19. HRESULT CServiceSpecificationService::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 wcQuery[BUFF_SIZE];
  27. WCHAR wcProductCode[39];
  28. DWORD dwBufSize;
  29. bool bMatch = false;
  30. UINT uiStatus;
  31. //These will change from class to class
  32. bool bService, bCheck;
  33. IWbemClassObject *pObj = NULL;
  34. VARIANT v;
  35. VariantInit(&v);
  36. CStringExt wcKey;
  37. // safe operation
  38. // lenght is smaller than BUFF_SIZE ( 512 )
  39. wcscpy(wcQuery, L"select distinct `ServiceInstall`, `Component_`, `Name` from ServiceInstall");
  40. LPWSTR Buffer = NULL;
  41. LPWSTR dynBuffer = NULL;
  42. DWORD dwDynBuffer = 0L;
  43. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  44. {
  45. // safe operation:
  46. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  47. wcscpy(wcProductCode, m_pRequest->Package(i));
  48. //Open our database
  49. try
  50. {
  51. if ( GetView ( &hView, wcProductCode, wcQuery, L"ServiceInstall", TRUE, FALSE ) )
  52. {
  53. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  54. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  55. CheckMSI(uiStatus);
  56. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  57. //----------------------------------------------------
  58. // safe operation
  59. wcKey.Copy ( L"Win32_ServiceSpecification.CheckID=\"" );
  60. dwBufSize = BUFF_SIZE;
  61. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  62. wcKey.Append ( 3, Buffer, wcProductCode, L"\"" );
  63. PutKeyProperty(m_pObj, pCheck, wcKey, &bCheck, m_pRequest);
  64. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  65. {
  66. dynBuffer [ 0 ] = 0;
  67. }
  68. //====================================================
  69. dwBufSize = BUFF_SIZE;
  70. GetBufferToPut ( hRecord, 2, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  71. if ( ValidateComponentName ( msidata.GetDatabase (), wcProductCode, Buffer ) )
  72. {
  73. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  74. {
  75. dynBuffer [ 0 ] = 0;
  76. }
  77. dwBufSize = BUFF_SIZE;
  78. GetBufferToPut ( hRecord, 3, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  79. wcKey.Copy ( L"Win32_Service.Name=\"" );
  80. wcKey.Append ( 2, Buffer, L"\"" );
  81. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  82. {
  83. dynBuffer [ 0 ] = 0;
  84. }
  85. BSTR bstrObj;
  86. if ( ( bstrObj = ::SysAllocString ( wcKey ) ) == NULL )
  87. {
  88. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  89. }
  90. if SUCCEEDED ( hr = m_pNamespace->GetObject ( bstrObj, 0, m_pCtx, &pObj, NULL ) )
  91. {
  92. PutKeyProperty ( m_pObj, pElement, wcKey, &bService, m_pRequest );
  93. pObj->Release();
  94. pObj = NULL;
  95. //----------------------------------------------------
  96. if(bService && bCheck) bMatch = true;
  97. if((atAction != ACTIONTYPE_GET) || bMatch){
  98. hr = pHandler->Indicate(1, &m_pObj);
  99. }
  100. }
  101. ::SysFreeString ( bstrObj );
  102. }
  103. else
  104. {
  105. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  106. {
  107. dynBuffer [ 0 ] = 0;
  108. }
  109. }
  110. m_pObj->Release();
  111. m_pObj = NULL;
  112. g_fpMsiCloseHandle(hRecord);
  113. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  114. }
  115. }
  116. }
  117. catch(...)
  118. {
  119. if ( dynBuffer )
  120. {
  121. delete [] dynBuffer;
  122. dynBuffer = NULL;
  123. }
  124. g_fpMsiCloseHandle(hRecord);
  125. g_fpMsiViewClose(hView);
  126. g_fpMsiCloseHandle(hView);
  127. msidata.CloseDatabase ();
  128. if(m_pObj)
  129. {
  130. m_pObj->Release();
  131. m_pObj = NULL;
  132. }
  133. throw;
  134. }
  135. g_fpMsiCloseHandle(hRecord);
  136. g_fpMsiViewClose(hView);
  137. g_fpMsiCloseHandle(hView);
  138. msidata.CloseDatabase ();
  139. }
  140. if ( dynBuffer )
  141. {
  142. delete [] dynBuffer;
  143. dynBuffer = NULL;
  144. }
  145. return hr;
  146. }