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.

279 lines
7.1 KiB

  1. // ConnectionManager.cpp : Implementation of CConnectionManager
  2. #include "stdafx.h"
  3. #include "Connection.h"
  4. #include "ConnMgr.h"
  5. #include "ConnectionManager.h"
  6. #include <process.h>
  7. #include "Ping.h"
  8. /////////////////////////////////////////////////////////////////////////////
  9. // CConnectionManager
  10. /////////////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. CConnectionManager::CConnectionManager()
  13. {
  14. OutputDebugString(_T("CConnectionManager::CConnectionManager()\n"));
  15. }
  16. CConnectionManager::~CConnectionManager()
  17. {
  18. OutputDebugString(_T("CConnectionManager::~CConnectionManager()\n"));
  19. }
  20. /////////////////////////////////////////////////////////////////////////////
  21. // Final Construct and Release
  22. HRESULT CConnectionManager::FinalConstruct()
  23. {
  24. OutputDebugString(_T("CConnectionManager::FinalConstruct()\n"));
  25. HRESULT hRes = CreateLocator();
  26. return hRes;
  27. }
  28. void CConnectionManager::FinalRelease()
  29. {
  30. OutputDebugString(_T("CConnectionManager::FinalRelease()\n"));
  31. Destroy();
  32. }
  33. /////////////////////////////////////////////////////////////////////////////
  34. // Startup/Termination
  35. HRESULT CConnectionManager::CreateLocator()
  36. {
  37. OutputDebugString(_T("CConnectionManager::CreateLocator()\n"));
  38. m_pIWbemLocator = NULL;
  39. HRESULT hr = CoCreateInstance(CLSID_WbemLocator,
  40. NULL,
  41. CLSCTX_INPROC_SERVER,
  42. IID_IWbemLocator,
  43. (LPVOID*)&m_pIWbemLocator);
  44. if (FAILED(hr))
  45. {
  46. OutputDebugString(_T("Failed to create Locator !"));
  47. return hr;
  48. }
  49. ASSERT(m_pIWbemLocator);
  50. return hr;
  51. }
  52. void CConnectionManager::Destroy()
  53. {
  54. OutputDebugString(_T("CConnectionManager::Destroy()\n"));
  55. // destroy all the connections laying around
  56. POSITION pos = m_ConnectionMap.GetStartPosition();
  57. CConnection* pConnection = NULL;
  58. CString sName;
  59. while( pos != NULL )
  60. {
  61. m_ConnectionMap.GetNextAssoc( pos, sName, pConnection);
  62. ASSERT(pConnection);
  63. ASSERT_VALID(pConnection);
  64. if( pConnection )
  65. delete pConnection;
  66. }
  67. m_ConnectionMap.RemoveAll();
  68. if( m_pIWbemLocator )
  69. {
  70. m_pIWbemLocator->Release();
  71. m_pIWbemLocator = NULL;
  72. }
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. // Helper functions
  76. BOOL CConnectionManager::ValidMachine(BSTR& bsMachine)
  77. {
  78. OutputDebugString(_T("CConnection::ValidateMachine()\n"));
  79. CPing Pong;
  80. // Resolve the machine name to see it is valid
  81. unsigned long ulIP = Pong.ResolveName(bsMachine);
  82. if( ulIP == 0L )
  83. {
  84. CString sDebugString;
  85. sDebugString.Format(_T("Could not resolve hostname for <%s>.\n"), CString(bsMachine));
  86. OutputDebugString(sDebugString);
  87. return false;
  88. }
  89. return true;
  90. }
  91. /////////////////////////////////////////////////////////////////////////////
  92. // Interface Methods.
  93. STDMETHODIMP CConnectionManager::GetConnection(BSTR bsMachineName,
  94. IWbemServices __RPC_FAR *__RPC_FAR * ppIWbemServices,
  95. long* lStatus)
  96. {
  97. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  98. CString sDebugString;
  99. sDebugString.Format(_T("CConnectionManager::GetConnection() for %s.\n"), CString(bsMachineName));
  100. OutputDebugString(sDebugString);
  101. *ppIWbemServices = NULL;
  102. *lStatus = 0;
  103. // if connection is already in the map, return it.
  104. CConnection* pConnection = NULL;
  105. if( m_ConnectionMap.Lookup(bsMachineName,pConnection) != 0 )
  106. {
  107. ASSERT(pConnection);
  108. ASSERT_VALID(pConnection);
  109. if( pConnection->m_bAvailable)
  110. {
  111. *ppIWbemServices = pConnection->m_pIWbemServices;
  112. (*ppIWbemServices)->AddRef();
  113. *lStatus = 1;
  114. }
  115. else
  116. {
  117. sDebugString.Format(_T("GetConnection(%s)-Connection is unavailable. Last connect attempt result=<%X>.\n"), CString(bsMachineName), pConnection->m_hrLastConnectResult);
  118. OutputDebugString(sDebugString);
  119. }
  120. return pConnection->m_hrLastConnectResult;
  121. }
  122. // if not valid, do not add it the map
  123. if (!ValidMachine(bsMachineName))
  124. return E_FAIL;
  125. // otherwise create a new connection.
  126. pConnection = new CConnection(bsMachineName, m_pIWbemLocator);
  127. m_ConnectionMap.SetAt(bsMachineName,pConnection);
  128. return S_OK;
  129. }
  130. STDMETHODIMP CConnectionManager::RemoveConnection(BSTR bsMachineName,IWbemObjectSink * pSink)
  131. {
  132. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  133. OutputDebugString(_T("CConnectionManager::RemoveConnection()\n"));
  134. CConnection* pConnection = NULL;
  135. if( m_ConnectionMap.Lookup(bsMachineName,pConnection) != 0 )
  136. {
  137. ASSERT(pConnection);
  138. ASSERT_VALID(pConnection);
  139. if( pConnection == NULL )
  140. {
  141. return E_FAIL;
  142. }
  143. if (pSink)
  144. pConnection->RemoveEventEntry(pSink);
  145. if( pConnection->GetEventConsumerCount() == 0 )
  146. {
  147. if (!m_ConnectionMap.RemoveKey(bsMachineName))
  148. OutputDebugString(_T("CConnectionManager::RemoveConnection() - Failed to Remove Connection!\n"));
  149. delete pConnection;
  150. }
  151. }
  152. return S_OK;
  153. }
  154. STDMETHODIMP CConnectionManager::RegisterEventNotification(BSTR bsMachineName,
  155. BSTR bsQuery,
  156. IWbemObjectSink * pSink)
  157. {
  158. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  159. OutputDebugString(_T("CConnectionManager::RegisterEventNotification()\n"));
  160. ASSERT(pSink);
  161. CConnection* pConnection = NULL;
  162. if( m_ConnectionMap.Lookup(bsMachineName,pConnection) == 0 )
  163. {
  164. OutputDebugString(_T("CConnectionManager::RegisterEventNotification()-Failed to find connection\n"));
  165. return E_FAIL;
  166. }
  167. ASSERT(pConnection);
  168. BOOL bResultRegister = pConnection->AddEventEntry(bsQuery,pSink);
  169. if (!bResultRegister)
  170. {
  171. OutputDebugString(_T("CConnectionManager::RegisterEventNotification()-Failed to add event.\n"));
  172. return E_FAIL;
  173. }
  174. return S_OK;
  175. }
  176. STDMETHODIMP CConnectionManager::ExecQueryAsync(BSTR bsMachineName, BSTR bsQuery, IWbemObjectSink *pSink)
  177. {
  178. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  179. OutputDebugString(_T("CConnectionManager::ExecQueryAsync()\n"));
  180. ASSERT(pSink);
  181. CConnection* pConnection = NULL;
  182. if( m_ConnectionMap.Lookup(bsMachineName,pConnection) == 0 )
  183. {
  184. OutputDebugString(_T("CConnectionManager::ExecQueryAsync()-Failed to find connection\n"));
  185. return E_FAIL;
  186. }
  187. ASSERT(pConnection);
  188. HRESULT hRes = S_OK;
  189. if( pConnection->m_bAvailable )
  190. {
  191. OutputDebugString(_T("ExecQueryAsync - connection available\n"));
  192. ASSERT(pConnection->m_pIWbemServices);
  193. BSTR bsLanguage = SysAllocString(_T("WQL"));
  194. hRes = pConnection->m_pIWbemServices->ExecQueryAsync(bsLanguage, bsQuery, WBEM_FLAG_BIDIRECTIONAL, NULL, pSink);
  195. SysFreeString(bsLanguage);
  196. CString sDebugString;
  197. sDebugString.Format(_T("ExecQueryAsync returns %x\n"),hRes);
  198. OutputDebugString(sDebugString);
  199. }
  200. else
  201. {
  202. OutputDebugString(_T("ExecQueryAsync - no connection available\n"));
  203. BOOL bResultRegister = pConnection->AddEventEntry(bsQuery,pSink);
  204. if (!bResultRegister)
  205. {
  206. OutputDebugString(_T("CConnectionManager::ExecQueryAsync()-Failed to add event.\n"));
  207. return E_FAIL;
  208. }
  209. }
  210. return hRes;
  211. }
  212. STDMETHODIMP CConnectionManager::ConnectToNamespace(BSTR bsNamespace, IWbemServices __RPC_FAR *__RPC_FAR * ppIWbemServices)
  213. {
  214. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  215. HRESULT hr = S_OK;
  216. hr = m_pIWbemLocator->ConnectServer(bsNamespace,0L,0L,0L,0L,0L,0L,ppIWbemServices);
  217. return hr;
  218. }