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

  1. // Binary.cpp: implementation of the CBinary class.
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "Binary.h"
  8. #include "ExtendString.h"
  9. #include "ExtendQuery.h"
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CBinary::CBinary(CRequestObject *pObj, IWbemServices *pNamespace,
  14. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  15. {
  16. }
  17. CBinary::~CBinary()
  18. {
  19. }
  20. HRESULT CBinary::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 wcProductCode[39];
  28. DWORD dwBufSize;
  29. bool bMatch = false;
  30. UINT uiStatus;
  31. bool bGotID = false;
  32. WCHAR wcTestCode[39];
  33. bool bGotName = false;
  34. WCHAR wcName[BUFF_SIZE];
  35. //These will change from class to class
  36. bool bName, bCode;
  37. //improve getobject performance by optimizing the query
  38. if(atAction != ACTIONTYPE_ENUM)
  39. {
  40. // we are doing GetObject so we need to be reinitialized
  41. hr = WBEM_E_NOT_FOUND;
  42. BSTR bstrCompare;
  43. int iPos = -1;
  44. bstrCompare = SysAllocString ( L"ProductCode" );
  45. if ( bstrCompare )
  46. {
  47. if(FindIn(m_pRequest->m_Property, bstrCompare, &iPos))
  48. {
  49. if ( ::SysStringLen ( m_pRequest->m_Value[iPos] ) == 38 )
  50. {
  51. //Get the product code we're looking for
  52. wcscpy(wcTestCode, m_pRequest->m_Value[iPos]);
  53. bGotID = true;
  54. }
  55. else
  56. {
  57. // we are not good to go, they have sent us longer string
  58. SysFreeString ( bstrCompare );
  59. throw hr;
  60. }
  61. }
  62. SysFreeString ( bstrCompare );
  63. }
  64. else
  65. {
  66. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  67. }
  68. iPos = -1;
  69. bstrCompare = SysAllocString ( L"Name" );
  70. if ( bstrCompare )
  71. {
  72. if(FindIn(m_pRequest->m_Property, bstrCompare, &iPos))
  73. {
  74. if ( ::SysStringLen ( m_pRequest->m_Value[iPos] ) < BUFF_SIZE )
  75. {
  76. //Get the name we're looking for
  77. wcscpy(wcName, m_pRequest->m_Value[iPos]);
  78. bGotName = true;
  79. }
  80. else
  81. {
  82. // we are not good to go, they have sent us longer string
  83. SysFreeString ( bstrCompare );
  84. throw hr;
  85. }
  86. }
  87. SysFreeString ( bstrCompare );
  88. }
  89. else
  90. {
  91. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  92. }
  93. }
  94. Query wcQuery;
  95. wcQuery.Append ( 1, L"select distinct `Name` from Binary" );
  96. if( bGotName )
  97. {
  98. wcQuery.Append ( 3, L" where `Name`=\'", wcName, L"\'" );
  99. }
  100. LPWSTR Buffer = NULL;
  101. LPWSTR dynBuffer = NULL;
  102. DWORD dwDynBuffer = 0;
  103. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  104. {
  105. // safe operation:
  106. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  107. wcscpy(wcProductCode, m_pRequest->Package(i));
  108. if((atAction == ACTIONTYPE_ENUM) || (bGotID && (_wcsicmp(wcTestCode, wcProductCode) == 0))){
  109. //Open our database
  110. try
  111. {
  112. if ( GetView ( &hView, wcProductCode, wcQuery, L"Binary", TRUE, FALSE ) )
  113. {
  114. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  115. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  116. CheckMSI(uiStatus);
  117. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  118. //----------------------------------------------------
  119. dwBufSize = BUFF_SIZE;
  120. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  121. PutKeyProperty(m_pObj, pName, Buffer, &bName, m_pRequest);
  122. PutProperty(m_pObj, pCaption, Buffer);
  123. PutProperty(m_pObj, pDescription, Buffer);
  124. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  125. {
  126. dynBuffer [ 0 ] = 0;
  127. }
  128. PutKeyProperty(m_pObj, pProductCode, wcProductCode, &bCode, m_pRequest);
  129. //----------------------------------------------------
  130. if(bName && bCode) bMatch = true;
  131. if((atAction != ACTIONTYPE_GET) || bMatch){
  132. hr = pHandler->Indicate(1, &m_pObj);
  133. }
  134. m_pObj->Release();
  135. m_pObj = NULL;
  136. g_fpMsiCloseHandle(hRecord);
  137. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  138. }
  139. }
  140. }
  141. catch(...)
  142. {
  143. if ( dynBuffer )
  144. {
  145. delete [] dynBuffer;
  146. dynBuffer = NULL;
  147. }
  148. g_fpMsiCloseHandle(hRecord);
  149. g_fpMsiViewClose(hView);
  150. g_fpMsiCloseHandle(hView);
  151. msidata.CloseDatabase ();
  152. if(m_pObj)
  153. {
  154. m_pObj->Release();
  155. m_pObj = NULL;
  156. }
  157. throw;
  158. }
  159. g_fpMsiCloseHandle(hRecord);
  160. g_fpMsiViewClose(hView);
  161. g_fpMsiCloseHandle(hView);
  162. msidata.CloseDatabase ();
  163. }
  164. }
  165. if ( dynBuffer )
  166. {
  167. delete [] dynBuffer;
  168. dynBuffer = NULL;
  169. }
  170. return hr;
  171. }