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.

181 lines
4.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: P R O V I D E R . C P P
  7. //
  8. // Contents: Registrar abstraction for a provider.
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 14 Sep 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "uhbase.h"
  18. #include "Provider.h"
  19. #include "uhutil.h"
  20. #include "uhthread.h"
  21. class CProviderAsync : public CWorkItem
  22. {
  23. public:
  24. HRESULT HrInitStart(IUPnPDeviceProviderPtr pProvider, const wchar_t * szInitString);
  25. HRESULT HrInitStop(IUPnPDeviceProviderPtr pProvider);
  26. DWORD DwRun();
  27. private:
  28. IUPnPDeviceProviderPtr m_pProvider;
  29. BOOL m_bStart;
  30. CUString m_strInitString;
  31. };
  32. HRESULT CProviderAsync::HrInitStart(IUPnPDeviceProviderPtr pProvider, const wchar_t * szInitString)
  33. {
  34. HRESULT hr = S_OK;
  35. m_pProvider = pProvider;
  36. m_bStart = TRUE;
  37. hr = m_strInitString.HrAssign(szInitString);
  38. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CProviderAsync::HrInitStart");
  39. return hr;
  40. }
  41. HRESULT CProviderAsync::HrInitStop(IUPnPDeviceProviderPtr pProvider)
  42. {
  43. HRESULT hr = S_OK;
  44. m_pProvider = pProvider;
  45. m_bStart = FALSE;
  46. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CProviderAsync::HrInitStop");
  47. return hr;
  48. }
  49. DWORD CProviderAsync::DwRun()
  50. {
  51. TraceTag(ttidRegistrar, "CProviderAsync::DwRun(%x) - starting worker thread", this);
  52. HRESULT hr = S_OK;
  53. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  54. if(SUCCEEDED(hr))
  55. {
  56. if(m_bStart)
  57. {
  58. BSTR bstrInitString = NULL;
  59. hr = m_strInitString.HrGetBSTR(&bstrInitString);
  60. if(SUCCEEDED(hr))
  61. {
  62. hr = m_pProvider->Start(bstrInitString);
  63. SysFreeString(bstrInitString);
  64. }
  65. }
  66. else
  67. {
  68. hr = m_pProvider->Stop();
  69. }
  70. }
  71. else
  72. {
  73. TraceTag(ttidError, "CProviderAsync::DwRun - CoInitializeEx failed!");
  74. }
  75. m_pProvider.Release();
  76. CoUninitialize();
  77. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CProviderAsync::DwRun(%x)", this);
  78. return hr;
  79. }
  80. CProvider::CProvider()
  81. {
  82. }
  83. CProvider::~CProvider()
  84. {
  85. Clear();
  86. }
  87. HRESULT CProvider::HrInitialize(
  88. const wchar_t * szProgIDProviderClass,
  89. const wchar_t * szInitString,
  90. const wchar_t * szContainerId)
  91. {
  92. CHECK_POINTER(szProgIDProviderClass);
  93. CHECK_POINTER(szInitString);
  94. CHECK_POINTER(szContainerId);
  95. TraceTag(ttidRegistrar, "CProvider::HrInitialize(ProgId=%S)", szProgIDProviderClass);
  96. HRESULT hr = S_OK;
  97. hr = m_strContainerId.HrAssign(szContainerId);
  98. if(SUCCEEDED(hr))
  99. {
  100. hr = HrCreateAndReferenceContainedObjectByProgId(
  101. szContainerId, szProgIDProviderClass, SMART_QI(m_pDeviceProvider));
  102. if(SUCCEEDED(hr))
  103. {
  104. CProviderAsync * pAsync = new CProviderAsync;
  105. if(pAsync)
  106. {
  107. hr = pAsync->HrInitStart(m_pDeviceProvider, szInitString);
  108. if(SUCCEEDED(hr))
  109. {
  110. hr = pAsync->HrStart(TRUE);
  111. }
  112. if(FAILED(hr))
  113. {
  114. delete pAsync;
  115. }
  116. }
  117. else
  118. {
  119. hr = E_OUTOFMEMORY;
  120. }
  121. if (FAILED(hr))
  122. {
  123. HrDereferenceContainer(szContainerId);
  124. }
  125. }
  126. }
  127. TraceHr(ttidRegistrar, FAL, hr, FALSE, "CProvider::HrInitialize");
  128. return hr;
  129. }
  130. void CProvider::Transfer(CProvider & ref)
  131. {
  132. m_strContainerId.Transfer(ref.m_strContainerId);
  133. m_pDeviceProvider.Swap(ref.m_pDeviceProvider);
  134. }
  135. void CProvider::Clear()
  136. {
  137. HRESULT hr = S_OK;
  138. HrDereferenceContainer(m_strContainerId);
  139. m_strContainerId.Clear();
  140. if(m_pDeviceProvider)
  141. {
  142. CProviderAsync * pAsync = new CProviderAsync;
  143. if(pAsync)
  144. {
  145. hr = pAsync->HrInitStop(m_pDeviceProvider);
  146. if(SUCCEEDED(hr))
  147. {
  148. hr = pAsync->HrStart(TRUE);
  149. }
  150. if(FAILED(hr))
  151. {
  152. delete pAsync;
  153. }
  154. }
  155. }
  156. m_pDeviceProvider.Release();
  157. }