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.

218 lines
6.5 KiB

  1. // P3DomainEnum.cpp : Implementation of CP3DomainEnum
  2. #include "stdafx.h"
  3. #include "P3Admin.h"
  4. #include "P3DomainEnum.h"
  5. #include "P3Domain.h"
  6. #include <Iads.h>
  7. #include <Adshlp.h>
  8. /////////////////////////////////////////////////////////////////////////////
  9. // CP3DomainEnum
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CP3DomainEnum::CP3DomainEnum() :
  14. m_pIUnk(NULL), m_pAdminX(NULL), m_pIEnumVARIANT(NULL)
  15. {
  16. }
  17. CP3DomainEnum::~CP3DomainEnum()
  18. {
  19. if ( NULL != m_pIUnk )
  20. m_pIUnk->Release();
  21. if ( NULL != m_pIEnumVARIANT )
  22. m_pIEnumVARIANT->Release();
  23. }
  24. /////////////////////////////////////////////////////////////////////////////
  25. // IEnumVARIANT
  26. STDMETHODIMP CP3DomainEnum::Next( /* [in] */ ULONG celt, /* [length_is][size_is][out] */ VARIANT __RPC_FAR *rgVar, /* [out] */ ULONG __RPC_FAR *pCeltFetched)
  27. {
  28. if ( NULL == rgVar || ( 1 != celt && NULL == pCeltFetched ))
  29. return E_POINTER;
  30. if ( NULL == m_pAdminX ) return E_POINTER;
  31. if ( NULL == m_pIEnumVARIANT ) return E_POINTER;
  32. HRESULT hr;
  33. ULONG nActual = 0;
  34. bool bValidDomain = false;
  35. VARIANT __RPC_FAR *pVar = rgVar;
  36. VARIANT v;
  37. BSTR bstr = NULL;
  38. LPWSTR ps = NULL;
  39. IUnknown *pIUnk;
  40. CComObject<CP3Domain> *p;
  41. CComPtr<IADs> spIADs = NULL;
  42. VariantInit( &v );
  43. hr = m_pIEnumVARIANT->Next( 1, &v, pCeltFetched );
  44. while ( S_OK == hr && !bValidDomain )
  45. {
  46. if ( S_OK == hr )
  47. {
  48. if ( VT_DISPATCH == V_VT( &v ))
  49. {
  50. if ( NULL != spIADs.p )
  51. spIADs.Release();
  52. hr = V_DISPATCH( &v )->QueryInterface( IID_IADs, reinterpret_cast<LPVOID*>( &spIADs ));
  53. }
  54. else
  55. hr = E_UNEXPECTED;
  56. VariantClear( &v );
  57. }
  58. if ( S_OK == hr )
  59. hr = spIADs->get_Name( &bstr );
  60. if ( S_OK == hr )
  61. {
  62. hr = m_pAdminX->ValidateDomain( bstr );
  63. if ( S_OK == hr )
  64. bValidDomain = true;
  65. else
  66. {
  67. hr = m_pIEnumVARIANT->Next( 1, &v, pCeltFetched );
  68. SysFreeString( bstr );
  69. bstr = NULL;
  70. }
  71. }
  72. }
  73. if ( S_OK == hr )
  74. {
  75. hr = CComObject<CP3Domain>::CreateInstance( &p ); // Reference count still 0
  76. if ( S_OK == hr )
  77. {
  78. // Increment the reference count on the source object and pass it to the new object
  79. hr = m_pIUnk->QueryInterface(IID_IUnknown, reinterpret_cast<LPVOID*>( &pIUnk ));
  80. if ( S_OK == hr )
  81. {
  82. hr = p->Init( pIUnk, m_pAdminX, bstr );
  83. if ( S_OK == hr )
  84. {
  85. V_VT( &v ) = VT_DISPATCH;
  86. hr = p->QueryInterface(IID_IDispatch, reinterpret_cast<void**>( &V_DISPATCH( &v )));
  87. if ( S_OK == hr )
  88. hr = VariantCopy( pVar, &v );
  89. VariantClear( &v );
  90. nActual++;
  91. }
  92. }
  93. if FAILED( hr )
  94. delete p;
  95. }
  96. }
  97. if ( NULL != bstr )
  98. SysFreeString( bstr );
  99. if (pCeltFetched)
  100. *pCeltFetched = nActual;
  101. if ((S_OK == hr) && (nActual < celt))
  102. hr = S_FALSE;
  103. return hr;
  104. }
  105. STDMETHODIMP CP3DomainEnum::Skip(ULONG celt)
  106. {
  107. if ( NULL == m_pAdminX ) return E_POINTER;
  108. if ( NULL == m_pIEnumVARIANT ) return E_POINTER;
  109. HRESULT hr = S_OK;
  110. BSTR bstrDomainName;
  111. VARIANT v;
  112. CComPtr<IADs> spIADs = NULL;
  113. VariantInit( &v );
  114. while (( S_OK == hr ) && ( 0 < celt ))
  115. {
  116. if ( S_OK == hr )
  117. {
  118. hr = m_pIEnumVARIANT->Next( 1, &v, NULL );
  119. if ( S_OK == hr )
  120. {
  121. if ( VT_DISPATCH == V_VT( &v ))
  122. {
  123. if ( NULL != spIADs.p )
  124. spIADs.Release();
  125. hr = V_DISPATCH( &v )->QueryInterface( IID_IADs, reinterpret_cast<LPVOID*>( &spIADs ));
  126. }
  127. else
  128. hr = E_UNEXPECTED;
  129. VariantClear( &v );
  130. }
  131. }
  132. if ( S_OK == hr )
  133. {
  134. hr = spIADs->get_Name( &bstrDomainName );
  135. if ( S_OK == hr )
  136. {
  137. hr = m_pAdminX->ValidateDomain( bstrDomainName );
  138. if ( S_OK == hr )
  139. celt--;
  140. else
  141. hr = S_OK;
  142. SysFreeString( bstrDomainName );
  143. }
  144. }
  145. }
  146. return hr;
  147. }
  148. STDMETHODIMP CP3DomainEnum::Reset(void)
  149. {
  150. if ( NULL == m_pIEnumVARIANT ) return E_POINTER;
  151. return m_pIEnumVARIANT->Reset();
  152. }
  153. STDMETHODIMP CP3DomainEnum::Clone( /* [out] */ IEnumVARIANT __RPC_FAR *__RPC_FAR *ppEnum)
  154. {
  155. if ( NULL == ppEnum ) return E_INVALIDARG;
  156. HRESULT hr;
  157. LPUNKNOWN pIUnk;
  158. CComObject<CP3DomainEnum> *p;
  159. IEnumVARIANT *pIEnumVARIANT;
  160. *ppEnum = NULL;
  161. hr = m_pIEnumVARIANT->Clone( &pIEnumVARIANT );
  162. if SUCCEEDED( hr )
  163. {
  164. hr = CComObject<CP3DomainEnum>::CreateInstance(&p); // Reference count still 0
  165. if SUCCEEDED( hr )
  166. { // Increment the reference count on the source object and pass it to the new enumerator
  167. hr = m_pIUnk->QueryInterface(IID_IUnknown, reinterpret_cast<LPVOID*>( &pIUnk ));
  168. if SUCCEEDED( hr )
  169. {
  170. hr = p->Init( pIUnk, m_pAdminX, pIEnumVARIANT ); // p must call release on pIUnk when done.
  171. if SUCCEEDED( hr )
  172. hr = p->QueryInterface( IID_IUnknown, reinterpret_cast<LPVOID*>( ppEnum ));
  173. }
  174. if FAILED( hr )
  175. delete p; // Release
  176. }
  177. pIEnumVARIANT->Release();
  178. }
  179. return hr;
  180. }
  181. //////////////////////////////////////////////////////////////////////
  182. // Implementation: public
  183. HRESULT CP3DomainEnum::Init(IUnknown *pIUnk, CP3AdminWorker *p, IEnumVARIANT *pIEnumVARIANT )
  184. {
  185. if ( NULL == pIUnk ) return E_INVALIDARG;
  186. if ( NULL == p ) return E_INVALIDARG;
  187. if ( NULL == pIEnumVARIANT ) return E_INVALIDARG;
  188. m_pIUnk = pIUnk;
  189. m_pAdminX = p;
  190. pIEnumVARIANT->AddRef();
  191. m_pIEnumVARIANT = pIEnumVARIANT;
  192. return S_OK;
  193. }