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.

233 lines
6.4 KiB

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