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.

232 lines
4.5 KiB

  1. #include "precomp.h"
  2. #include "AppCollection.h"
  3. #include "confroom.h"
  4. #include "EnumVar.h"
  5. //////////////////////////////////////////////////////////
  6. // Construction/destruction/initialization
  7. //////////////////////////////////////////////////////////
  8. CSharableAppCollection::CSharableAppCollection()
  9. : m_pList(NULL)
  10. {
  11. DBGENTRY(CSharableAppCollection::CSharableAppCollection);
  12. DBGEXIT(CSharableAppCollection::CSharableAppCollection);
  13. }
  14. CSharableAppCollection::~CSharableAppCollection()
  15. {
  16. DBGENTRY(CSharableAppCollection::~CSharableAppCollection);
  17. if(m_pList)
  18. {
  19. FreeShareableApps(m_pList);
  20. }
  21. DBGEXIT(CSharableAppCollection::~CSharableAppCollection);
  22. }
  23. //static
  24. HRESULT CSharableAppCollection::CreateInstance(IAS_HWND_ARRAY* pList, ISharableAppCollection **ppSharebleAppCollection)
  25. {
  26. DBGENTRY(HRESULT CSharableAppCollection::CreateInstance);
  27. HRESULT hr = S_OK;
  28. if(pList)
  29. {
  30. CComObject<CSharableAppCollection>* p = NULL;
  31. p = new CComObject<CSharableAppCollection>(NULL);
  32. if(p)
  33. {
  34. p->SetVoid(NULL);
  35. p->InternalFinalConstructAddRef();
  36. hr = p->FinalConstruct();
  37. p->InternalFinalConstructRelease();
  38. if(hr == S_OK)
  39. {
  40. hr = p->QueryInterface(IID_ISharableAppCollection, reinterpret_cast<void**>(ppSharebleAppCollection));
  41. p->m_pList = pList;
  42. }
  43. if(FAILED(hr))
  44. {
  45. delete p;
  46. *ppSharebleAppCollection = NULL;
  47. }
  48. }
  49. else
  50. {
  51. hr = E_OUTOFMEMORY;
  52. }
  53. }
  54. else
  55. {
  56. hr = E_POINTER;
  57. }
  58. DBGEXIT(CSharableAppCollection::CreateInstance);
  59. return hr;
  60. }
  61. //////////////////////////////////////////////////////////
  62. // ISharableAppCollection
  63. //////////////////////////////////////////////////////////
  64. STDMETHODIMP CSharableAppCollection::get_Item(VARIANT Index, DWORD* pSharableAppHWND)
  65. {
  66. DBGENTRY(CSharableAppCollection::get_Item);
  67. HRESULT hr = S_OK;
  68. USES_CONVERSION;
  69. if(m_pList)
  70. {
  71. if(pSharableAppHWND)
  72. {
  73. switch(Index.vt)
  74. {
  75. case VT_BSTR:
  76. *pSharableAppHWND = reinterpret_cast<long>(_GetHWNDFromName(OLE2T(Index.bstrVal)));
  77. break;
  78. case VT_I2:
  79. if(static_cast<UINT>(Index.iVal) < m_pList->cEntries)
  80. {
  81. *pSharableAppHWND = reinterpret_cast<long>(m_pList->aEntries[Index.iVal].hwnd);
  82. }
  83. else
  84. {
  85. hr = E_INVALIDARG;
  86. }
  87. break;
  88. case VT_I4:
  89. if(static_cast<UINT>(Index.lVal) < m_pList->cEntries)
  90. {
  91. *pSharableAppHWND = reinterpret_cast<long>(m_pList->aEntries[Index.lVal].hwnd);
  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. }
  107. else
  108. {
  109. hr = E_UNEXPECTED;
  110. }
  111. DBGEXIT_HR(CSharableAppCollection::get_Item,hr);
  112. return hr;
  113. }
  114. STDMETHODIMP CSharableAppCollection::_NewEnum(IUnknown** ppunk)
  115. {
  116. DBGENTRY(CSharableAppCollection::_NewEnum);
  117. HRESULT hr = S_OK;
  118. if(m_pList)
  119. {
  120. SAFEARRAY* psa;
  121. SAFEARRAYBOUND rgsabound[1];
  122. rgsabound[0].lLbound = 0;
  123. rgsabound[0].cElements = m_pList->cEntries;
  124. psa = SafeArrayCreate(VT_I4, m_pList->cEntries, rgsabound);
  125. if(psa)
  126. {
  127. for(UINT i = 0; i < m_pList->cEntries; ++i)
  128. {
  129. CComVariant var(reinterpret_cast<long>(m_pList->aEntries[i].hwnd));
  130. long ix[1] = {i};
  131. SafeArrayPutElement(psa, ix, &var);
  132. }
  133. CEnumVariant* pEnumVar = NULL;
  134. hr = CEnumVariant::Create(psa, m_pList->cEntries, &pEnumVar);
  135. if(SUCCEEDED(hr))
  136. {
  137. hr = pEnumVar->QueryInterface(IID_IEnumVARIANT, reinterpret_cast<void**>(ppunk));
  138. }
  139. SafeArrayDestroy(psa);
  140. }
  141. else
  142. {
  143. hr = E_OUTOFMEMORY;
  144. }
  145. }
  146. DBGEXIT_HR(CSharableAppCollection::_NewEnum,hr);
  147. return hr;
  148. }
  149. STDMETHODIMP CSharableAppCollection::get_Count(LONG * pnCount)
  150. {
  151. DBGENTRY(CSharableAppCollection::get_Count);
  152. HRESULT hr = S_OK;
  153. if(m_pList)
  154. {
  155. if(pnCount)
  156. {
  157. *pnCount = m_pList->cEntries;
  158. }
  159. else
  160. {
  161. hr = E_POINTER;
  162. }
  163. }
  164. else
  165. {
  166. hr = E_UNEXPECTED;
  167. }
  168. DBGEXIT_HR(CSharableAppCollection::get_Count,hr);
  169. return hr;
  170. }
  171. //////////////////////////////////////////////////////////
  172. // Helper Fns
  173. //////////////////////////////////////////////////////////
  174. HWND CSharableAppCollection::_GetHWNDFromName(LPCTSTR pcsz)
  175. {
  176. HWND hWnd = NULL;
  177. if(m_pList)
  178. {
  179. int cch = lstrlen(pcsz) + 1;
  180. LPTSTR pszTmp = new TCHAR[cch];
  181. if(pszTmp)
  182. {
  183. for(UINT i = 0; i < m_pList->cEntries; ++i)
  184. {
  185. HWND hWndCur = m_pList->aEntries[i].hwnd;
  186. if(::GetWindowText(hWndCur , pszTmp, cch))
  187. {
  188. // If the window text is the same, just return the hwnd
  189. if(!lstrcmp(pcsz, pszTmp))
  190. {
  191. hWnd = hWndCur;
  192. break;
  193. }
  194. }
  195. }
  196. delete [] pszTmp;
  197. }
  198. }
  199. return hWnd;
  200. }