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.

221 lines
5.6 KiB

  1. // SelfRegModule.cpp: implementation of the CSelfRegModule class.
  2. //
  3. // Copyright (c) 1997-2002 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "SelfRegModule.h"
  8. #include "ExtendString.h"
  9. #include "ExtendQuery.h"
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CSelfRegModule::CSelfRegModule(CRequestObject *pObj, IWbemServices *pNamespace,
  14. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  15. {
  16. }
  17. CSelfRegModule::~CSelfRegModule()
  18. {
  19. }
  20. HRESULT CSelfRegModule::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 wcAction[BUFF_SIZE];
  32. WCHAR wcTestCode[39];
  33. //These will change from class to class
  34. bool bCheck;
  35. SetSinglePropertyPath(L"ActionID");
  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"ActionID" );
  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], wcAction);
  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 `File_`, `Cost` from SelfReg" );
  83. //optimize for GetObject
  84. if ( bGotID )
  85. {
  86. wcQuery.Append ( 3, L" where `File_`=\'", wcAction, 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"SelfReg", 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, pFile, Buffer);
  110. PutProperty(m_pObj, pCaption, Buffer);
  111. PutProperty(m_pObj, pDescription, Buffer);
  112. PutProperty(m_pObj, pName, Buffer);
  113. PutKeyProperty ( m_pObj, pActionID, Buffer, &bCheck, m_pRequest, 1, wcProductCode );
  114. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  115. {
  116. dynBuffer [ 0 ] = 0;
  117. }
  118. //====================================================
  119. PutProperty(m_pObj, pCost, g_fpMsiRecordGetInteger(hRecord, 2));
  120. //----------------------------------------------------
  121. if(bCheck) bMatch = true;
  122. if((atAction != ACTIONTYPE_GET) || bMatch){
  123. hr = pHandler->Indicate(1, &m_pObj);
  124. }
  125. m_pObj->Release();
  126. m_pObj = NULL;
  127. g_fpMsiCloseHandle(hRecord);
  128. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  129. }
  130. }
  131. }
  132. catch(...)
  133. {
  134. if ( dynBuffer )
  135. {
  136. delete [] dynBuffer;
  137. dynBuffer = NULL;
  138. }
  139. if (hRecord)
  140. g_fpMsiCloseHandle(hRecord);
  141. if (hView)
  142. {
  143. g_fpMsiViewClose(hView);
  144. g_fpMsiCloseHandle(hView);
  145. }
  146. msidata.CloseDatabase ();
  147. if(m_pObj)
  148. {
  149. m_pObj->Release();
  150. m_pObj = NULL;
  151. }
  152. throw;
  153. }
  154. if (hRecord)
  155. g_fpMsiCloseHandle(hRecord);
  156. if (hView)
  157. {
  158. g_fpMsiViewClose(hView);
  159. g_fpMsiCloseHandle(hView);
  160. }
  161. msidata.CloseDatabase ();
  162. }
  163. }
  164. if ( dynBuffer )
  165. {
  166. delete [] dynBuffer;
  167. dynBuffer = NULL;
  168. }
  169. return hr;
  170. }