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.

226 lines
5.9 KiB

  1. // MIMEInfoAction.cpp: implementation of the CMIMEInfoAction class.
  2. //
  3. // Copyright (c) 1997-2002 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "MIMEInfoAction.h"
  8. #include "ExtendString.h"
  9. #include "ExtendQuery.h"
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CMIMEInfoAction::CMIMEInfoAction(CRequestObject *pObj, IWbemServices *pNamespace,
  14. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  15. {
  16. }
  17. CMIMEInfoAction::~CMIMEInfoAction()
  18. {
  19. }
  20. HRESULT CMIMEInfoAction::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 bActionID;
  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 `ContentType`, `Extension_`, `CLSID` from MIME" );
  83. //optimize for GetObject
  84. if ( bGotID )
  85. {
  86. wcQuery.Append ( 3, L" where `ContentType`=\'", 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"MIME", 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, pContentType, 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, &bActionID, m_pRequest, 1, wcProductCode );
  114. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  115. {
  116. dynBuffer [ 0 ] = 0;
  117. }
  118. //====================================================
  119. dwBufSize = BUFF_SIZE;
  120. PutPropertySpecial ( hRecord, 2, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, pExtension );
  121. dwBufSize = BUFF_SIZE;
  122. CheckMSI(g_fpMsiRecordGetStringW(hRecord, 3, wcBuf, &dwBufSize));
  123. PutProperty(m_pObj, pCLSID, wcBuf);
  124. //----------------------------------------------------
  125. if(bActionID) bMatch = true;
  126. if((atAction != ACTIONTYPE_GET) || bMatch){
  127. hr = pHandler->Indicate(1, &m_pObj);
  128. }
  129. m_pObj->Release();
  130. m_pObj = NULL;
  131. g_fpMsiCloseHandle(hRecord);
  132. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  133. }
  134. }
  135. }
  136. catch(...)
  137. {
  138. if ( dynBuffer )
  139. {
  140. delete [] dynBuffer;
  141. dynBuffer = NULL;
  142. }
  143. if (hRecord)
  144. g_fpMsiCloseHandle(hRecord);
  145. if (hView)
  146. {
  147. g_fpMsiViewClose(hView);
  148. g_fpMsiCloseHandle(hView);
  149. }
  150. msidata.CloseDatabase ();
  151. if(m_pObj)
  152. {
  153. m_pObj->Release();
  154. m_pObj = NULL;
  155. }
  156. throw;
  157. }
  158. if (hRecord)
  159. g_fpMsiCloseHandle(hRecord);
  160. if (hView)
  161. {
  162. g_fpMsiViewClose(hView);
  163. g_fpMsiCloseHandle(hView);
  164. }
  165. msidata.CloseDatabase ();
  166. }
  167. }
  168. if ( dynBuffer )
  169. {
  170. delete [] dynBuffer;
  171. dynBuffer = NULL;
  172. }
  173. return hr;
  174. }