Source code of Windows XP (NT5)
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.

198 lines
4.2 KiB

  1. #include "pch.h"
  2. #include "query.h"
  3. CDSSearch::CDSSearch()
  4. {
  5. m_bInitialized = FALSE;
  6. m_pwszFilter = NULL;
  7. m_ppszAttr = NULL;
  8. m_CountAttr = NULL;
  9. m_pObj = NULL;
  10. m_SearchHandle = NULL;
  11. }
  12. void CDSSearch::_Reset()
  13. {
  14. if (m_pObj != NULL)
  15. {
  16. if (m_SearchHandle)
  17. {
  18. m_pObj->CloseSearchHandle (m_SearchHandle);
  19. m_SearchHandle = NULL;
  20. }
  21. m_pObj->Release();
  22. m_pObj = NULL;
  23. }
  24. if (m_pwszFilter)
  25. {
  26. delete[] m_pwszFilter;
  27. m_pwszFilter = NULL;
  28. }
  29. }
  30. CDSSearch::~CDSSearch()
  31. {
  32. _Reset();
  33. }
  34. HRESULT CDSSearch::Init(IDirectorySearch * pObj)
  35. {
  36. HRESULT hr = S_OK;
  37. _Reset();
  38. m_pObj = pObj;
  39. pObj->AddRef();
  40. m_bInitialized = TRUE;
  41. m_scope = ADS_SCOPE_ONELEVEL;
  42. return hr;
  43. }
  44. HRESULT CDSSearch::Init(PCWSTR pszPath,
  45. const CDSCmdCredentialObject& refCredObject)
  46. {
  47. ENTER_FUNCTION_HR(FULL_LOGGING, CDSSearch::Init, hr);
  48. do // false loop
  49. {
  50. //
  51. // Verify parameters
  52. //
  53. if (!pszPath)
  54. {
  55. ASSERT(pszPath);
  56. hr = E_INVALIDARG;
  57. break;
  58. }
  59. CComPtr<IDirectorySearch> spDirSearch;
  60. hr = DSCmdOpenObject(refCredObject,
  61. pszPath,
  62. IID_IDirectorySearch,
  63. (void**)&spDirSearch,
  64. true);
  65. if (FAILED(hr))
  66. {
  67. break;
  68. }
  69. hr = Init(spDirSearch);
  70. } while (false);
  71. return hr;
  72. }
  73. HRESULT CDSSearch::SetAttributeList (LPTSTR *ppszAttribs, INT cAttrs)
  74. {
  75. m_ppszAttr = ppszAttribs;
  76. m_CountAttr = cAttrs;
  77. return S_OK;
  78. }
  79. HRESULT CDSSearch::SetSearchScope (ADS_SCOPEENUM scope)
  80. {
  81. if (m_bInitialized) {
  82. m_scope = scope;
  83. }
  84. return S_OK;
  85. }
  86. const int NUM_PREFS = 5;
  87. HRESULT _SetSearchPreference(IDirectorySearch* piSearch, ADS_SCOPEENUM scope, BOOL bAttrOnly)
  88. {
  89. if (NULL == piSearch)
  90. {
  91. ASSERT(FALSE);
  92. return E_INVALIDARG;
  93. }
  94. int cPref = 4;
  95. ADS_SEARCHPREF_INFO aSearchPref[5];
  96. aSearchPref[0].dwSearchPref = ADS_SEARCHPREF_CHASE_REFERRALS;
  97. aSearchPref[0].vValue.dwType = ADSTYPE_INTEGER;
  98. aSearchPref[0].vValue.Integer = ADS_CHASE_REFERRALS_EXTERNAL;
  99. aSearchPref[1].dwSearchPref = ADS_SEARCHPREF_PAGESIZE;
  100. aSearchPref[1].vValue.dwType = ADSTYPE_INTEGER;
  101. aSearchPref[1].vValue.Integer = QUERY_PAGESIZE;
  102. aSearchPref[2].dwSearchPref = ADS_SEARCHPREF_CACHE_RESULTS;
  103. aSearchPref[2].vValue.dwType = ADSTYPE_BOOLEAN;
  104. aSearchPref[2].vValue.Integer = FALSE;
  105. aSearchPref[3].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  106. aSearchPref[3].vValue.dwType = ADSTYPE_INTEGER;
  107. aSearchPref[3].vValue.Integer = scope;
  108. if(bAttrOnly)
  109. {
  110. aSearchPref[4].dwSearchPref = ADS_SEARCHPREF_ATTRIBTYPES_ONLY;
  111. aSearchPref[4].vValue.dwType = ADSTYPE_BOOLEAN;
  112. aSearchPref[4].vValue.Integer = TRUE;
  113. ++cPref;
  114. }
  115. return piSearch->SetSearchPreference (aSearchPref, cPref);
  116. }
  117. HRESULT CDSSearch::DoQuery(BOOL bAttrOnly)
  118. {
  119. if (!m_bInitialized)
  120. return E_ADS_BAD_PATHNAME;
  121. HRESULT hr = _SetSearchPreference(m_pObj, m_scope, bAttrOnly);
  122. if (SUCCEEDED(hr)) {
  123. hr = m_pObj->ExecuteSearch (m_pwszFilter,
  124. m_ppszAttr,
  125. m_CountAttr,
  126. &m_SearchHandle);
  127. }
  128. return hr;
  129. }
  130. HRESULT
  131. CDSSearch::GetNextRow()
  132. {
  133. DWORD status = ERROR_MORE_DATA;
  134. HRESULT hr = S_OK;
  135. HRESULT hr2 = S_OK;
  136. WCHAR Buffer1[512], Buffer2[512];
  137. if (!m_bInitialized) {
  138. return E_ADS_BAD_PATHNAME;
  139. }
  140. while (status == ERROR_MORE_DATA ) {
  141. hr = m_pObj->GetNextRow (m_SearchHandle);
  142. if (hr == S_ADS_NOMORE_ROWS) {
  143. hr2 = ADsGetLastError(&status, Buffer1, 512,
  144. Buffer2, 512);
  145. ASSERT(SUCCEEDED(hr2));
  146. } else {
  147. status = 0;
  148. }
  149. }
  150. return hr;
  151. }
  152. HRESULT
  153. CDSSearch::GetColumn(LPWSTR Attribute,
  154. PADS_SEARCH_COLUMN pColumnData)
  155. {
  156. if (m_bInitialized) {
  157. return m_pObj->GetColumn (m_SearchHandle,
  158. Attribute,
  159. pColumnData);
  160. }
  161. return E_ADS_BAD_PATHNAME;
  162. }
  163. HRESULT
  164. CDSSearch::GetNextColumnName(LPWSTR *ppszColumnName)
  165. {
  166. if (m_bInitialized) {
  167. return m_pObj->GetNextColumnName(m_SearchHandle,
  168. ppszColumnName);
  169. }
  170. return E_ADS_BAD_PATHNAME;
  171. }