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.

251 lines
7.6 KiB

  1. // ApplicationService.cpp: implementation of the CApplicationService class.
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include <tchar.h>
  8. #include "ApplicationService.h"
  9. #include "ExtendString.h"
  10. #include "ExtendQuery.h"
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. CApplicationService::CApplicationService(CRequestObject *pObj, IWbemServices *pNamespace,
  15. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  16. {
  17. }
  18. CApplicationService::~CApplicationService()
  19. {
  20. }
  21. HRESULT CApplicationService::CreateObject(IWbemObjectSink *pHandler, ACTIONTYPE atAction)
  22. {
  23. HRESULT hr = WBEM_S_NO_ERROR;
  24. MSIHANDLE hView = NULL;
  25. MSIHANDLE hRecord = NULL;
  26. int i = -1;
  27. WCHAR wcBuf[BUFF_SIZE];
  28. WCHAR wcProductCode[39];
  29. #if !defined(_UNICODE)
  30. WCHAR wcTmp[BUFF_SIZE];
  31. #endif
  32. DWORD dwBufSize;
  33. bool bMatch = false;
  34. UINT uiStatus;
  35. //These will change from class to class
  36. bool bName;
  37. INSTALLSTATE piInstalled, piAction;
  38. if( m_pRequest->m_iPropCount >= MSI_KEY_LIST_SIZE) {
  39. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  40. }
  41. if(m_pRequest->m_iValCount > m_pRequest->m_iPropCount){
  42. m_pRequest->m_Property[m_pRequest->m_iPropCount] = SysAllocString(L"Name");
  43. if(!m_pRequest->m_Property[(m_pRequest->m_iPropCount)++])
  44. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  45. }
  46. bool bGotID = false;
  47. Query wcQuery;
  48. wcQuery.Append ( 1, L"select distinct `Shortcut`, `Component_` from Shortcut" );
  49. //improve getobject performance by optimizing the query
  50. if(atAction != ACTIONTYPE_ENUM)
  51. {
  52. // we are doing GetObject so we need to be reinitialized
  53. hr = WBEM_E_NOT_FOUND;
  54. BSTR bstrCompare;
  55. int iPos = -1;
  56. bstrCompare = SysAllocString ( L"Name" );
  57. if ( bstrCompare )
  58. {
  59. if(FindIn(m_pRequest->m_Property, bstrCompare, &iPos))
  60. {
  61. bGotID = true;
  62. wcQuery.Append ( 3, L" where `Shortcut`=\'", m_pRequest->m_Value[iPos], L"\'" );
  63. }
  64. SysFreeString ( bstrCompare );
  65. }
  66. else
  67. {
  68. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  69. }
  70. }
  71. LPWSTR Buffer = NULL;
  72. LPWSTR dynBuffer = NULL;
  73. DWORD dwDynBuffer = 0L;
  74. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  75. {
  76. // safe operation:
  77. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  78. wcscpy(wcProductCode, m_pRequest->Package(i));
  79. //Open our database
  80. try
  81. {
  82. if ( GetView ( &hView, wcProductCode, wcQuery, L"Shortcut", FALSE, FALSE ) )
  83. {
  84. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  85. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  86. CheckMSI(uiStatus);
  87. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  88. //----------------------------------------------------
  89. dwBufSize = BUFF_SIZE;
  90. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  91. PutKeyProperty(m_pObj, pName, Buffer, &bName, m_pRequest);
  92. PutProperty(m_pObj, pCaption, Buffer);
  93. PutProperty(m_pObj, pDescription, Buffer);
  94. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  95. {
  96. dynBuffer [ 0 ] = 0;
  97. }
  98. dwBufSize = BUFF_SIZE;
  99. GetBufferToPut ( hRecord, 2, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  100. if ( ValidateComponentName ( msidata.GetDatabase(), wcProductCode, Buffer ) )
  101. {
  102. uiStatus = g_fpMsiGetComponentStateW( msidata.GetProduct (), Buffer, &piInstalled, &piAction );
  103. if(uiStatus == ERROR_SUCCESS){
  104. if((piInstalled == INSTALLSTATE_ADVERTISED) ||
  105. (piInstalled == INSTALLSTATE_LOCAL) ||
  106. (piInstalled == INSTALLSTATE_SOURCE) ||
  107. (piInstalled == INSTALLSTATE_DEFAULT))
  108. wcscpy(wcBuf, L"OK");
  109. else if((piInstalled == INSTALLSTATE_BADCONFIG) ||
  110. (piInstalled == INSTALLSTATE_MOREDATA) ||
  111. (piInstalled == INSTALLSTATE_INVALIDARG))
  112. wcscpy(wcBuf, L"Error");
  113. else if((piInstalled == INSTALLSTATE_INCOMPLETE) ||
  114. (piInstalled == INSTALLSTATE_SOURCEABSENT) ||
  115. (piInstalled == INSTALLSTATE_BROKEN) ||
  116. (piInstalled == INSTALLSTATE_NOTUSED) ||
  117. (piInstalled == INSTALLSTATE_REMOVED) ||
  118. (piInstalled == INSTALLSTATE_ABSENT))
  119. wcscpy(wcBuf, L"Degraded");
  120. else
  121. wcscpy(wcBuf, L"Unknown");
  122. }else
  123. wcscpy(wcBuf, L"Unknown");
  124. PutProperty(m_pObj, pStatus, wcBuf);
  125. PutProperty(m_pObj, pCreationClassName, L"Win32_ApplicationService");
  126. PutProperty(m_pObj, pSystemCreationClassName, L"Win32_ComputerSystem");
  127. TCHAR cBuf[MAX_COMPUTERNAME_LENGTH + 1] = { _T('\0') };
  128. dwBufSize = ( MAX_COMPUTERNAME_LENGTH+1 ) * sizeof ( TCHAR );
  129. if(!GetComputerName(cBuf, &dwBufSize)) throw WBEM_E_FAILED;
  130. #ifndef UNICODE
  131. WCHAR wcComputer[MAX_COMPUTERNAME_LENGTH + 1];
  132. mbstowcs(wcComputer, cBuf, MAX_COMPUTERNAME_LENGTH + 1);
  133. PutProperty(m_pObj, pSystemName, wcComputer);
  134. #else UNICODE
  135. PutProperty(m_pObj, pSystemName, cBuf);
  136. #endif UNICODE
  137. //====================================================
  138. PutProperty(m_pObj, pStartMode, L"Manual");
  139. //----------------------------------------------------
  140. if(bName) bMatch = true;
  141. if((atAction != ACTIONTYPE_GET) || bMatch){
  142. hr = pHandler->Indicate(1, &m_pObj);
  143. }
  144. }
  145. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  146. {
  147. dynBuffer [ 0 ] = 0;
  148. }
  149. m_pObj->Release();
  150. m_pObj = NULL;
  151. g_fpMsiCloseHandle(hRecord);
  152. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  153. }
  154. }
  155. }
  156. catch(...)
  157. {
  158. if ( dynBuffer )
  159. {
  160. delete [] dynBuffer;
  161. dynBuffer = NULL;
  162. }
  163. g_fpMsiCloseHandle(hRecord);
  164. g_fpMsiViewClose(hView);
  165. g_fpMsiCloseHandle(hView);
  166. msidata.CloseDatabase ();
  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.CloseDatabase ();
  179. msidata.CloseProduct ();
  180. }
  181. if ( dynBuffer )
  182. {
  183. delete [] dynBuffer;
  184. dynBuffer = NULL;
  185. }
  186. return hr;
  187. }