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.

246 lines
5.2 KiB

  1. #include "Precomp.h"
  2. #include "MemberCollection.h"
  3. #include "Confroom.h"
  4. #include "Particip.h"
  5. #include "EnumVar.h"
  6. //////////////////////////////////////////////////////////
  7. // Construction / destruction / initialization
  8. //////////////////////////////////////////////////////////
  9. CMemberCollection::~CMemberCollection()
  10. {
  11. _FreeMemberCollection();
  12. }
  13. //static
  14. HRESULT CMemberCollection::CreateInstance(CSimpleArray<IMember*>& rMemberObjs, IMemberCollection** ppMemberCollection)
  15. {
  16. HRESULT hr = S_OK;
  17. CComObject<CMemberCollection>* p = NULL;
  18. p = new CComObject<CMemberCollection>(NULL);
  19. if(p)
  20. {
  21. p->SetVoid(NULL);
  22. p->InternalFinalConstructAddRef();
  23. hr = p->FinalConstruct();
  24. p->InternalFinalConstructRelease();
  25. if(hr == S_OK)
  26. {
  27. hr = p->QueryInterface(IID_IMemberCollection, reinterpret_cast<void**>(ppMemberCollection));
  28. if(SUCCEEDED(hr))
  29. {
  30. hr = p->_Init(rMemberObjs);
  31. }
  32. }
  33. if(FAILED(hr))
  34. {
  35. delete p;
  36. *ppMemberCollection = NULL;
  37. }
  38. }
  39. else
  40. {
  41. hr = E_OUTOFMEMORY;
  42. }
  43. return hr;
  44. }
  45. //////////////////////////////////////////////////////////
  46. // IMemberCollection
  47. //////////////////////////////////////////////////////////
  48. STDMETHODIMP CMemberCollection::get_Item(VARIANT Index, IMember** ppMember)
  49. {
  50. DBGENTRY(CMemberCollection::get_Item);
  51. HRESULT hr = S_OK;
  52. if(ppMember)
  53. {
  54. *ppMember = NULL;
  55. switch(Index.vt)
  56. {
  57. case VT_BSTR:
  58. LPTSTR szName;
  59. hr = BSTR_to_LPTSTR (&szName, Index.bstrVal);
  60. if (SUCCEEDED(hr))
  61. {
  62. *ppMember = _GetMemberFromName(szName);
  63. if(*ppMember)
  64. {
  65. (*ppMember)->AddRef();
  66. }
  67. else
  68. {
  69. hr = E_FAIL;
  70. }
  71. // Free resources
  72. //
  73. delete (szName);
  74. }
  75. break;
  76. case VT_I2:
  77. if(m_Members.GetSize() < Index.iVal)
  78. {
  79. *ppMember = m_Members[Index.iVal];
  80. (*ppMember)->AddRef();
  81. }
  82. else
  83. {
  84. hr = E_INVALIDARG;
  85. }
  86. break;
  87. case VT_I4:
  88. if(m_Members.GetSize() < Index.lVal)
  89. {
  90. *ppMember = m_Members[Index.lVal];
  91. (*ppMember)->AddRef();
  92. }
  93. else
  94. {
  95. hr = E_INVALIDARG;
  96. }
  97. break;
  98. default:
  99. hr = E_INVALIDARG;
  100. }
  101. }
  102. else
  103. {
  104. hr = E_POINTER;
  105. }
  106. DBGEXIT_HR(CMemberCollection::get_Item,hr);
  107. return hr;
  108. }
  109. STDMETHODIMP CMemberCollection::_NewEnum(IUnknown** ppunk)
  110. {
  111. DBGENTRY(CMemberCollection::_NewEnum);
  112. HRESULT hr = S_OK;
  113. SAFEARRAY* psa;
  114. SAFEARRAYBOUND rgsabound[1];
  115. rgsabound[0].lLbound = 0;
  116. rgsabound[0].cElements = m_Members.GetSize();
  117. psa = SafeArrayCreate(VT_DISPATCH, m_Members.GetSize(), rgsabound);
  118. if(psa)
  119. {
  120. for(int i = 0; i < m_Members.GetSize(); ++i)
  121. {
  122. CComPtr<IDispatch> spDispatch;
  123. hr = m_Members[i]->QueryInterface(IID_IDispatch, reinterpret_cast<void**>(&spDispatch));
  124. ASSERT(SUCCEEDED(hr));
  125. CComVariant var(spDispatch);
  126. long ix[1] = {i};
  127. SafeArrayPutElement(psa, ix, &var);
  128. }
  129. CEnumVariant* pEnumVar = NULL;
  130. hr = CEnumVariant::Create(psa, m_Members.GetSize(), &pEnumVar);
  131. if(SUCCEEDED(hr))
  132. {
  133. hr = pEnumVar->QueryInterface(IID_IEnumVARIANT, reinterpret_cast<void**>(ppunk));
  134. }
  135. SafeArrayDestroy(psa);
  136. }
  137. else
  138. {
  139. hr = E_OUTOFMEMORY;
  140. }
  141. DBGEXIT_HR(CMemberCollection::_NewEnum,hr);
  142. return hr;
  143. }
  144. STDMETHODIMP CMemberCollection::get_Count(LONG * pnCount)
  145. {
  146. DBGENTRY(CMemberCollection::get_Count);
  147. HRESULT hr = S_OK;
  148. if(pnCount)
  149. {
  150. *pnCount = m_Members.GetSize();
  151. }
  152. else
  153. {
  154. hr = E_OUTOFMEMORY;
  155. }
  156. DBGEXIT_HR(CMemberCollection::get_Count,hr);
  157. return hr;
  158. }
  159. //////////////////////////////////////////////////////////
  160. // Helpers
  161. //////////////////////////////////////////////////////////
  162. HRESULT CMemberCollection::_Init(CSimpleArray<IMember*>& rMemberObjs)
  163. {
  164. HRESULT hr = S_OK;
  165. for(int i = 0; i < rMemberObjs.GetSize(); ++i)
  166. {
  167. IMember* pMember = rMemberObjs[i];
  168. pMember->AddRef();
  169. m_Members.Add(pMember);
  170. }
  171. return hr;
  172. }
  173. void CMemberCollection::_FreeMemberCollection()
  174. {
  175. for(int i = 0; i < m_Members.GetSize(); ++i)
  176. {
  177. m_Members[i]->Release();
  178. }
  179. m_Members.RemoveAll();
  180. }
  181. IMember* CMemberCollection::_GetMemberFromName(LPCTSTR pszName)
  182. {
  183. IMember* pRet = NULL;
  184. for(int i = 0; i < m_Members.GetSize(); ++i)
  185. {
  186. CComBSTR bstrName;
  187. if(SUCCEEDED(m_Members[i]->get_Name(&bstrName)))
  188. {
  189. LPTSTR szName;
  190. hr = BSTR_to_LPTSTR (&szName, bstrName);
  191. if (SUCCEEDED(hr))
  192. {
  193. if(!lstrcmp(szName, pszName))
  194. {
  195. pRet = m_Members[i];
  196. delete (szName);
  197. break;
  198. }
  199. delete (szName);
  200. }
  201. }
  202. }
  203. return pRet;
  204. }