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.

185 lines
3.9 KiB

  1. //
  2. // MODULE: ShortList.cpp
  3. //
  4. // PURPOSE: A list of all of the handles that are currently open.
  5. // There is an instance of a COM interface for every open handle.
  6. //
  7. // PROJECT: Local Troubleshooter Launcher for the Device Manager
  8. //
  9. // COMPANY: Saltmine Creative, Inc. (206)-633-4743 [email protected]
  10. //
  11. // AUTHOR: Richard Meadows
  12. //
  13. // ORIGINAL DATE: 2-26-98
  14. //
  15. //
  16. // Version Date By Comments
  17. //--------------------------------------------------------------------
  18. // V0.1 - RM Original
  19. ///////////////////////
  20. #include <windows.h>
  21. #include <windowsx.h>
  22. #include <winnt.h>
  23. #include <ole2.h>
  24. #include "ShortList.h"
  25. CShortList::CShortList()
  26. {
  27. m_pFirst = NULL;
  28. m_pLast = NULL;
  29. return;
  30. }
  31. CShortList::~CShortList()
  32. {
  33. RemoveAll();
  34. return;
  35. }
  36. // Removes all of the items from the queue. Releases all of the interfaces. Deletes all of the items.
  37. void CShortList::RemoveAll()
  38. {
  39. CShortListItem *pDelItem;
  40. while (NULL != m_pFirst)
  41. {
  42. pDelItem = m_pFirst;
  43. m_pFirst = m_pFirst->m_pNext;
  44. pDelItem->m_pInterface->Release();
  45. delete pDelItem;
  46. }
  47. m_pFirst = NULL;
  48. m_pLast = NULL;
  49. return;
  50. }
  51. // Add: Returns false only if there is no memory left.
  52. bool CShortList::Add(HANDLE hItem, IUnknown *pInterface)
  53. {
  54. bool bHaveMemory = true;
  55. CShortListItem *pItem = new CShortListItem;
  56. if (NULL == pItem)
  57. {
  58. bHaveMemory = false;
  59. }
  60. else
  61. {
  62. pItem->m_hSelf = hItem;
  63. pItem->m_pInterface = pInterface;
  64. if (NULL == m_pFirst)
  65. {
  66. m_pFirst = pItem;
  67. m_pLast = pItem;
  68. }
  69. else
  70. { // Add the item to the end of the list.
  71. m_pLast->m_pNext = pItem;
  72. m_pLast = pItem;
  73. }
  74. }
  75. return bHaveMemory;
  76. }
  77. // Remove: Removes the item from the queue, frees the item's memory and releases the interface.
  78. // Returns false if the hItem is not found.
  79. bool CShortList::Remove(HANDLE hItem)
  80. {
  81. CShortListItem *pPrevious;
  82. CShortListItem *pItem;
  83. bool bItemFound = true;
  84. if (NULL == m_pFirst)
  85. return false;
  86. // Case 1 item.
  87. if (m_pLast == m_pFirst)
  88. {
  89. if (m_pFirst->m_hSelf == hItem)
  90. {
  91. m_pLast->m_pInterface->Release();
  92. delete m_pLast;
  93. m_pFirst = NULL;
  94. m_pLast = NULL;
  95. }
  96. else
  97. {
  98. bItemFound = false;
  99. }
  100. } // Case 2 items.
  101. else if (m_pFirst->m_pNext == m_pLast)
  102. {
  103. if (hItem == m_pFirst->m_hSelf)
  104. {
  105. m_pFirst->m_pInterface->Release();
  106. delete m_pFirst;
  107. m_pFirst = m_pLast;
  108. }
  109. else if (hItem == m_pLast->m_hSelf)
  110. {
  111. m_pLast->m_pInterface->Release();
  112. delete m_pLast;
  113. m_pLast = m_pFirst;
  114. }
  115. else
  116. {
  117. bItemFound = false;
  118. }
  119. } // Case 3 or more items.
  120. else if (NULL != m_pFirst)
  121. {
  122. // Case First item in the list.
  123. if (hItem == m_pFirst->m_hSelf)
  124. {
  125. pItem = m_pFirst;
  126. m_pFirst = m_pFirst->m_pNext;
  127. pItem->m_pInterface->Release();
  128. delete pItem;
  129. }
  130. else
  131. { // Look for the item in the list.
  132. pItem = m_pFirst;
  133. bItemFound = false;
  134. do
  135. {
  136. pPrevious = pItem;
  137. pItem = pItem->m_pNext;
  138. if (hItem == pItem->m_hSelf)
  139. {
  140. bItemFound = true;
  141. // Case last item.
  142. if (pItem == m_pLast)
  143. {
  144. pItem->m_pInterface->Release();
  145. delete pItem;
  146. m_pLast = pPrevious;
  147. m_pLast->m_pNext = NULL;
  148. }
  149. else
  150. { // Some where in the middle.
  151. CShortListItem *pDelItem = pItem;
  152. pPrevious->m_pNext = pItem->m_pNext;
  153. pDelItem->m_pInterface->Release();
  154. delete pDelItem;
  155. }
  156. pItem = NULL;
  157. }
  158. } while (NULL != pItem);
  159. }
  160. }
  161. else
  162. {
  163. bItemFound = false;
  164. }
  165. return bItemFound;
  166. }
  167. // LookUp: Returns a pointer to the interface or NULL if hItem is not in the list.
  168. IUnknown *CShortList::LookUp(HANDLE hItem)
  169. {
  170. IUnknown *pIAny = NULL;
  171. CShortListItem *pItem = m_pFirst;
  172. while(NULL != pItem)
  173. {
  174. if (hItem == pItem->m_hSelf)
  175. {
  176. pIAny = pItem->m_pInterface;
  177. break;
  178. }
  179. pItem = pItem->m_pNext;
  180. }
  181. return pIAny;
  182. }