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.

221 lines
6.4 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: D E V I C E M A N A G E R . C P P
  7. //
  8. // Contents: Registrar collection of physical devices and device information
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 12 Sep 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "uhbase.h"
  18. #include "DeviceManager.h"
  19. CDeviceManager::CDeviceManager()
  20. {
  21. }
  22. CDeviceManager::~CDeviceManager()
  23. {
  24. }
  25. HRESULT CDeviceManager::HrShutdown()
  26. {
  27. TraceTag(ttidRegistrar, "CDeviceManager::HrShutdown");
  28. HRESULT hr = S_OK;
  29. m_physicalDeviceTable.Clear();
  30. m_udnMappingTable.Clear();
  31. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CDeviceManager::HrShutdown");
  32. return hr;
  33. }
  34. HRESULT CDeviceManager::HrAddDevice(
  35. const PhysicalDeviceIdentifier & pdi,
  36. const wchar_t * szProgIdDeviceControlClass,
  37. const wchar_t * szInitString,
  38. const wchar_t * szContainerId,
  39. long nUDNs,
  40. wchar_t ** arszUDNs)
  41. {
  42. CHECK_POINTER(szProgIdDeviceControlClass);
  43. CHECK_POINTER(szInitString);
  44. CHECK_POINTER(szContainerId);
  45. TraceTag(ttidRegistrar, "CDeviceManager::HrAddDevice");
  46. HRESULT hr = S_OK;
  47. // Ensure we have a valid ProgID
  48. if (SUCCEEDED(hr))
  49. {
  50. CLSID clsid;
  51. if (FAILED(CLSIDFromProgID(szProgIdDeviceControlClass, &clsid)))
  52. {
  53. hr = E_INVALIDARG;
  54. }
  55. }
  56. if (SUCCEEDED(hr))
  57. {
  58. // Create and initialize the device first
  59. CPhysicalDeviceInfo physicalDeviceInfo;
  60. hr = physicalDeviceInfo.HrInitialize(szProgIdDeviceControlClass,
  61. szInitString, szContainerId,
  62. nUDNs, arszUDNs);
  63. if(SUCCEEDED(hr))
  64. {
  65. // Now add to collection
  66. hr = m_physicalDeviceTable.HrInsertTransfer(
  67. const_cast<PhysicalDeviceIdentifier&>(pdi),
  68. physicalDeviceInfo);
  69. if(SUCCEEDED(hr))
  70. {
  71. // For each UDN, add a mapping back to the physical device identifier
  72. for(long n = 0; n < nUDNs && SUCCEEDED(hr); ++n)
  73. {
  74. CUString strUDN;
  75. hr = strUDN.HrAssign(arszUDNs[n]);
  76. if(SUCCEEDED(hr))
  77. {
  78. hr = m_udnMappingTable.HrInsertTransfer(strUDN,
  79. const_cast<PhysicalDeviceIdentifier&>(pdi));
  80. }
  81. }
  82. }
  83. }
  84. }
  85. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CDeviceManager::HrAddDevice");
  86. return hr;
  87. }
  88. HRESULT CDeviceManager::HrAddRunningDevice(
  89. const PhysicalDeviceIdentifier & pdi,
  90. IUnknown * pUnkDeviceControl,
  91. const wchar_t * szInitString,
  92. long nUDNs,
  93. wchar_t ** arszUDNs)
  94. {
  95. CHECK_POINTER(pUnkDeviceControl);
  96. CHECK_POINTER(szInitString);
  97. TraceTag(ttidRegistrar, "CDeviceManager::HrAddRunningDevice");
  98. HRESULT hr = S_OK;
  99. // Create and initialize the device first
  100. CPhysicalDeviceInfo physicalDeviceInfo;
  101. hr = physicalDeviceInfo.HrInitializeRunning(pdi, pUnkDeviceControl, szInitString, nUDNs, arszUDNs);
  102. if(SUCCEEDED(hr))
  103. {
  104. // Now add to collection
  105. hr = m_physicalDeviceTable.HrInsertTransfer(const_cast<PhysicalDeviceIdentifier&>(pdi),
  106. physicalDeviceInfo);
  107. if(SUCCEEDED(hr))
  108. {
  109. // For each UDN, add a mapping back to the physical device identifier
  110. for(long n = 0; n < nUDNs && SUCCEEDED(hr); ++n)
  111. {
  112. CUString strUDN;
  113. hr = strUDN.HrAssign(arszUDNs[n]);
  114. if(SUCCEEDED(hr))
  115. {
  116. hr = m_udnMappingTable.HrInsertTransfer(strUDN, const_cast<PhysicalDeviceIdentifier&>(pdi));
  117. }
  118. }
  119. }
  120. }
  121. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CDeviceManager::HrAddRunningDevice");
  122. return hr;
  123. }
  124. HRESULT CDeviceManager::HrRemoveDevice(const PhysicalDeviceIdentifier & pdi)
  125. {
  126. TraceTag(ttidRegistrar, "CDeviceManager::HrRemoveDevice");
  127. HRESULT hr = S_OK;
  128. hr = m_physicalDeviceTable.HrErase(pdi);
  129. if(SUCCEEDED(hr))
  130. {
  131. while(SUCCEEDED(hr))
  132. {
  133. long nCount = m_udnMappingTable.Values().GetCount();
  134. for(long n = 0; n < nCount; ++n)
  135. {
  136. // Remove if we find a mapping for this pdi
  137. if(pdi == m_udnMappingTable.Values()[n])
  138. {
  139. hr = m_udnMappingTable.HrErase(m_udnMappingTable.Keys()[n]);
  140. break;
  141. }
  142. }
  143. if(n == nCount)
  144. {
  145. // We processed the whole array so there is nothing left to remove
  146. break;
  147. }
  148. }
  149. }
  150. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CDeviceManager::HrRemoveDevice");
  151. return hr;
  152. }
  153. HRESULT CDeviceManager::HrGetService(
  154. const wchar_t * szUDN,
  155. const wchar_t * szServiceId,
  156. CServiceInfo ** ppServiceInfo)
  157. {
  158. CHECK_POINTER(szUDN);
  159. CHECK_POINTER(szServiceId);
  160. CHECK_POINTER(ppServiceInfo);
  161. TraceTag(ttidRegistrar, "CDeviceManager::HrGetService(UDN=%S, ServiceId=%S)", szUDN, szServiceId);
  162. HRESULT hr = S_OK;
  163. // See which PDI this refers to
  164. CUString strUDN;
  165. hr = strUDN.HrAssign(szUDN);
  166. if(SUCCEEDED(hr))
  167. {
  168. PhysicalDeviceIdentifier * pPdi = m_udnMappingTable.Lookup(strUDN);
  169. if(pPdi)
  170. {
  171. // Now find the physical device and forward to that
  172. CPhysicalDeviceInfo * pPhysicalDeviceInfo = m_physicalDeviceTable.Lookup(*pPdi);
  173. if(pPhysicalDeviceInfo)
  174. {
  175. hr = pPhysicalDeviceInfo->HrGetService(*pPdi, szUDN, szServiceId, ppServiceInfo);
  176. }
  177. else
  178. {
  179. hr = E_INVALIDARG;
  180. }
  181. }
  182. else
  183. {
  184. hr = E_INVALIDARG;
  185. }
  186. }
  187. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CDeviceManager::HrGetService");
  188. return hr;
  189. }
  190. BOOL CDeviceManager::FHasDevice(const PhysicalDeviceIdentifier & pdi)
  191. {
  192. TraceTag(ttidRegistrar, "CDeviceManager::FHasDevice");
  193. HRESULT hr = S_OK;
  194. CPhysicalDeviceInfo* pphysicalDeviceInfo = NULL;
  195. hr = m_physicalDeviceTable.HrLookup(pdi, &pphysicalDeviceInfo);
  196. return (hr == S_OK);
  197. }