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.

284 lines
7.3 KiB

  1. // ApplicationCommandLine.cpp: implementation of the CApplicationCommandLine class.
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "ApplicationCommandLine.h"
  8. #include "ExtendString.h"
  9. #include "ExtendQuery.h"
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CApplicationCommandLine::CApplicationCommandLine(CRequestObject *pObj, IWbemServices *pNamespace,
  14. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  15. {
  16. }
  17. CApplicationCommandLine::~CApplicationCommandLine()
  18. {
  19. }
  20. HRESULT CApplicationCommandLine::CreateObject(IWbemObjectSink *pHandler, ACTIONTYPE atAction)
  21. {
  22. HRESULT hr = WBEM_S_NO_ERROR;
  23. MSIHANDLE hView = NULL;
  24. MSIHANDLE hRecord = NULL;
  25. MSIHANDLE hSEView = NULL;
  26. MSIHANDLE hSERecord = NULL;
  27. int i = -1;
  28. WCHAR wcBuf[BUFF_SIZE];
  29. WCHAR wcCommand[BUFF_SIZE];
  30. WCHAR wcProductCode[39];
  31. DWORD dwBufSize;
  32. bool bMatch = false;
  33. UINT uiStatus;
  34. WCHAR wcElement[BUFF_SIZE];
  35. bool bElement = false;
  36. CRequestObject *pAntData = NULL;
  37. if(atAction != ACTIONTYPE_ENUM)
  38. {
  39. // we are doing GetObject so we need to be reinitialized
  40. hr = WBEM_E_NOT_FOUND;
  41. int j;
  42. //GetObject optimizations
  43. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  44. for(j = 0; j < m_pRequest->m_iPropCount; j++){
  45. if(_wcsicmp(m_pRequest->m_Property[j], L"Antecedent") == 0){
  46. pAntData = new CRequestObject();
  47. if(!pAntData) throw he;
  48. pAntData->Initialize(m_pNamespace);
  49. pAntData->ParsePath(m_pRequest->m_Value[j]);
  50. break;
  51. }
  52. }
  53. if(pAntData){
  54. for(j = 0; j < pAntData->m_iPropCount; j++){
  55. if(_wcsicmp(pAntData->m_Property[j], L"Name") == 0){
  56. //Get the product code we're looking for
  57. if ( ::SysStringLen ( pAntData->m_Value[j] ) < BUFF_SIZE )
  58. {
  59. wcscpy(wcElement, pAntData->m_Value[j]);
  60. bElement = true;
  61. break;
  62. }
  63. }
  64. }
  65. pAntData->Cleanup();
  66. delete pAntData;
  67. pAntData = NULL;
  68. }
  69. }
  70. //These will change from class to class
  71. bool bAntec, bDepend;
  72. INSTALLSTATE piInstalled;
  73. CStringExt wcProp;
  74. Query wcQuery;
  75. wcQuery.Append ( 1, L"select distinct `Shortcut`, `Component_` from Shortcut" );
  76. //optimize for GetObject
  77. if ( bElement )
  78. {
  79. wcQuery.Append ( 3, L" where `Shortcut`=\'", wcElement, L"\'" );
  80. }
  81. QueryExt wcQuery1 ( L"select distinct `ComponentId` from Component where `Component`=\'" );
  82. LPWSTR dynBuffer = NULL;
  83. LPWSTR Buffer = NULL;
  84. DWORD dwdynBuffer = 0L;
  85. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  86. {
  87. // safe operation:
  88. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  89. wcscpy(wcProductCode, m_pRequest->Package(i));
  90. //Open our database
  91. try{
  92. if ( GetView ( &hView, wcProductCode, wcQuery, L"Shortcut", TRUE, FALSE ) )
  93. {
  94. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  95. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  96. CheckMSI(uiStatus);
  97. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  98. //----------------------------------------------------
  99. dwBufSize = BUFF_SIZE;
  100. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwdynBuffer, dynBuffer, Buffer );
  101. if ( Buffer && Buffer[0] != 0 )
  102. {
  103. wcProp.Copy ( L"Win32_ApplicationService.Name=\"" );
  104. wcProp.Append ( 2, Buffer, L"\"");
  105. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  106. {
  107. dynBuffer [ 0 ] = 0;
  108. }
  109. PutKeyProperty(m_pObj, pAntecedent, wcProp, &bAntec, m_pRequest);
  110. dwBufSize = BUFF_SIZE;
  111. GetBufferToPut ( hRecord, 2, dwBufSize, wcBuf, dwdynBuffer, dynBuffer, Buffer );
  112. // make query on fly
  113. wcQuery1.Append ( 2, Buffer, L"\'" );
  114. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  115. {
  116. dynBuffer [ 0 ] = 0;
  117. }
  118. CheckMSI(g_fpMsiDatabaseOpenViewW(msidata.GetDatabase (), wcQuery1, &hSEView));
  119. CheckMSI(g_fpMsiViewExecute(hSEView, 0));
  120. try{
  121. uiStatus = g_fpMsiViewFetch(hSEView, &hSERecord);
  122. if(uiStatus != ERROR_NO_MORE_ITEMS){
  123. dwBufSize = BUFF_SIZE;
  124. CheckMSI(g_fpMsiRecordGetStringW(hSERecord, 1, wcBuf, &dwBufSize));
  125. if(ValidateComponentID(wcBuf, wcProductCode)){
  126. dwBufSize = BUFF_SIZE;
  127. piInstalled = g_fpMsiGetComponentPathW(wcProductCode, wcBuf,
  128. wcCommand, &dwBufSize);
  129. if((wcscmp(wcCommand, L"") != 0) && (piInstalled != INSTALLSTATE_UNKNOWN)
  130. && (piInstalled != INSTALLSTATE_ABSENT)){
  131. dwBufSize = BUFF_SIZE;
  132. GetBufferToPut ( hRecord, 2, dwBufSize, wcBuf, dwdynBuffer, dynBuffer, Buffer );
  133. if ( ValidateComponentName ( msidata.GetDatabase (),
  134. wcProductCode,
  135. Buffer
  136. )
  137. )
  138. {
  139. wcProp.Copy ( L"Win32_CommandLineAccess.Name=\"" );
  140. wcProp.Append ( 2, EscapeStringW(wcCommand, Buffer), L"\"" );
  141. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  142. {
  143. dynBuffer [ 0 ] = 0;
  144. }
  145. PutKeyProperty(m_pObj, pDependent, wcProp, &bDepend, m_pRequest);
  146. //----------------------------------------------------
  147. if(bAntec && bDepend) bMatch = true;
  148. if((atAction != ACTIONTYPE_GET) || bMatch){
  149. hr = pHandler->Indicate(1, &m_pObj);
  150. }
  151. }
  152. else
  153. {
  154. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  155. {
  156. dynBuffer [ 0 ] = 0;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }catch(...){
  163. g_fpMsiViewClose(hSEView);
  164. g_fpMsiCloseHandle(hSEView);
  165. g_fpMsiCloseHandle(hSERecord);
  166. throw;
  167. }
  168. g_fpMsiViewClose(hSEView);
  169. g_fpMsiCloseHandle(hSEView);
  170. g_fpMsiCloseHandle(hSERecord);
  171. }
  172. m_pObj->Release();
  173. m_pObj = NULL;
  174. g_fpMsiCloseHandle(hRecord);
  175. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  176. }
  177. }
  178. }
  179. catch(...)
  180. {
  181. if ( dynBuffer )
  182. {
  183. delete [] dynBuffer;
  184. dynBuffer = NULL;
  185. }
  186. g_fpMsiCloseHandle(hRecord);
  187. g_fpMsiViewClose(hView);
  188. g_fpMsiCloseHandle(hView);
  189. msidata.CloseDatabase ();
  190. if(m_pObj)
  191. {
  192. m_pObj->Release();
  193. m_pObj = NULL;
  194. }
  195. throw;
  196. }
  197. g_fpMsiCloseHandle(hRecord);
  198. g_fpMsiViewClose(hView);
  199. g_fpMsiCloseHandle(hView);
  200. msidata.CloseDatabase ();
  201. }
  202. if ( dynBuffer )
  203. {
  204. delete [] dynBuffer;
  205. dynBuffer = NULL;
  206. }
  207. return hr;
  208. }