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.

238 lines
5.0 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. if(m_pList)
  69. {
  70. if(pSharableAppHWND)
  71. {
  72. switch(Index.vt)
  73. {
  74. case VT_BSTR:
  75. LPTSTR szName;
  76. hr = BSTR_to_LPTSTR (&szName, Index.bstrVal);
  77. if (SUCCEEDED(hr))
  78. {
  79. *pSharableAppHWND = reinterpret_cast<long>(_GetHWNDFromName(szName));
  80. // Free resources
  81. //
  82. delete (szName);
  83. }
  84. break;
  85. case VT_I2:
  86. if(static_cast<UINT>(Index.iVal) < m_pList->cEntries)
  87. {
  88. *pSharableAppHWND = reinterpret_cast<long>(m_pList->aEntries[Index.iVal].hwnd);
  89. }
  90. else
  91. {
  92. hr = E_INVALIDARG;
  93. }
  94. break;
  95. case VT_I4:
  96. if(static_cast<UINT>(Index.lVal) < m_pList->cEntries)
  97. {
  98. *pSharableAppHWND = reinterpret_cast<long>(m_pList->aEntries[Index.lVal].hwnd);
  99. }
  100. else
  101. {
  102. hr = E_INVALIDARG;
  103. }
  104. break;
  105. default:
  106. hr = E_INVALIDARG;
  107. }
  108. }
  109. else
  110. {
  111. hr = E_POINTER;
  112. }
  113. }
  114. else
  115. {
  116. hr = E_UNEXPECTED;
  117. }
  118. DBGEXIT_HR(CSharableAppCollection::get_Item,hr);
  119. return hr;
  120. }
  121. STDMETHODIMP CSharableAppCollection::_NewEnum(IUnknown** ppunk)
  122. {
  123. DBGENTRY(CSharableAppCollection::_NewEnum);
  124. HRESULT hr = S_OK;
  125. if(m_pList)
  126. {
  127. SAFEARRAY* psa;
  128. SAFEARRAYBOUND rgsabound[1];
  129. rgsabound[0].lLbound = 0;
  130. rgsabound[0].cElements = m_pList->cEntries;
  131. psa = SafeArrayCreate(VT_I4, m_pList->cEntries, rgsabound);
  132. if(psa)
  133. {
  134. for(UINT i = 0; i < m_pList->cEntries; ++i)
  135. {
  136. CComVariant var(reinterpret_cast<long>(m_pList->aEntries[i].hwnd));
  137. long ix[1] = {i};
  138. SafeArrayPutElement(psa, ix, &var);
  139. }
  140. CEnumVariant* pEnumVar = NULL;
  141. hr = CEnumVariant::Create(psa, m_pList->cEntries, &pEnumVar);
  142. if(SUCCEEDED(hr))
  143. {
  144. hr = pEnumVar->QueryInterface(IID_IEnumVARIANT, reinterpret_cast<void**>(ppunk));
  145. }
  146. SafeArrayDestroy(psa);
  147. }
  148. else
  149. {
  150. hr = E_OUTOFMEMORY;
  151. }
  152. }
  153. DBGEXIT_HR(CSharableAppCollection::_NewEnum,hr);
  154. return hr;
  155. }
  156. STDMETHODIMP CSharableAppCollection::get_Count(LONG * pnCount)
  157. {
  158. DBGENTRY(CSharableAppCollection::get_Count);
  159. HRESULT hr = S_OK;
  160. if(m_pList)
  161. {
  162. if(pnCount)
  163. {
  164. *pnCount = m_pList->cEntries;
  165. }
  166. else
  167. {
  168. hr = E_POINTER;
  169. }
  170. }
  171. else
  172. {
  173. hr = E_UNEXPECTED;
  174. }
  175. DBGEXIT_HR(CSharableAppCollection::get_Count,hr);
  176. return hr;
  177. }
  178. //////////////////////////////////////////////////////////
  179. // Helper Fns
  180. //////////////////////////////////////////////////////////
  181. HWND CSharableAppCollection::_GetHWNDFromName(LPCTSTR pcsz)
  182. {
  183. HWND hWnd = NULL;
  184. if(m_pList)
  185. {
  186. int cch = lstrlen(pcsz) + 1;
  187. LPTSTR pszTmp = new TCHAR[cch];
  188. if(pszTmp)
  189. {
  190. for(UINT i = 0; i < m_pList->cEntries; ++i)
  191. {
  192. HWND hWndCur = m_pList->aEntries[i].hwnd;
  193. if(::GetWindowText(hWndCur , pszTmp, cch))
  194. {
  195. // If the window text is the same, just return the hwnd
  196. if(!lstrcmp(pcsz, pszTmp))
  197. {
  198. hWnd = hWndCur;
  199. break;
  200. }
  201. }
  202. }
  203. delete [] pszTmp;
  204. }
  205. }
  206. return hWnd;
  207. }