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.

379 lines
9.9 KiB

  1. /******************************************************************************
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. Connectivity.cpp
  5. Abstract:
  6. This file contains the implementation of the CPCHConnectivity class.
  7. Revision History:
  8. Davide Massarenti (Dmassare) 04/15/2000
  9. created
  10. ******************************************************************************/
  11. #include "stdafx.h"
  12. //
  13. // Required by REGSTR_* macros.
  14. //
  15. #ifdef __TEXT
  16. #undef __TEXT
  17. #define __TEXT(quote) L##quote // r_winnt
  18. #endif
  19. #include <ras.h>
  20. #include <raserror.h>
  21. #include <inetreg.h>
  22. #include <Iphlpapi.h>
  23. static const WCHAR c_szURL_Connection[] = L"hcp://system/errors/Connection.htm";
  24. /////////////////////////////////////////////////////////////////////////////
  25. /////////////////////////////////////////////////////////////////////////////
  26. CPCHConnectivity::CPCHConnectivity()
  27. {
  28. m_parent = NULL; // CPCHHelpCenterExternal* m_parent;
  29. }
  30. HRESULT CPCHConnectivity::ConnectToParent( /*[in]*/ CPCHHelpCenterExternal* parent )
  31. {
  32. m_parent = parent;
  33. return S_OK;
  34. }
  35. STDMETHODIMP CPCHConnectivity::get_IsAModem( /*[out, retval]*/ VARIANT_BOOL *pVal )
  36. {
  37. __HCP_FUNC_ENTRY( "CPCHConnectivity::get_IsAModem" );
  38. HRESULT hr;
  39. DWORD dwMode = 0;
  40. __MPC_PARAMCHECK_BEGIN(hr)
  41. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,VARIANT_FALSE);
  42. __MPC_PARAMCHECK_END();
  43. if(::InternetGetConnectedState( &dwMode, 0 ) == TRUE)
  44. {
  45. if(dwMode & INTERNET_CONNECTION_MODEM) *pVal = VARIANT_TRUE;
  46. }
  47. hr = S_OK;
  48. __HCP_FUNC_CLEANUP;
  49. __HCP_FUNC_EXIT(hr);
  50. }
  51. STDMETHODIMP CPCHConnectivity::get_IsALan( /*[out, retval]*/ VARIANT_BOOL *pVal )
  52. {
  53. __HCP_FUNC_ENTRY( "CPCHConnectivity::get_IsALan" );
  54. HRESULT hr;
  55. DWORD dwMode = 0;
  56. __MPC_PARAMCHECK_BEGIN(hr)
  57. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,VARIANT_FALSE);
  58. __MPC_PARAMCHECK_END();
  59. if(::InternetGetConnectedState( &dwMode, 0 ) == TRUE)
  60. {
  61. if(dwMode & INTERNET_CONNECTION_LAN) *pVal = VARIANT_TRUE;
  62. }
  63. hr = S_OK;
  64. __HCP_FUNC_CLEANUP;
  65. __HCP_FUNC_EXIT(hr);
  66. }
  67. STDMETHODIMP CPCHConnectivity::get_AutoDialEnabled( /*[out, retval]*/ VARIANT_BOOL *pVal )
  68. {
  69. __HCP_FUNC_ENTRY( "CPCHConnectivity::get_AutoDialEnabled" );
  70. HRESULT hr;
  71. DWORD dwValue;
  72. bool fFound;
  73. __MPC_PARAMCHECK_BEGIN(hr)
  74. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,VARIANT_FALSE);
  75. __MPC_PARAMCHECK_END();
  76. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::RegKey_Value_Read( dwValue, fFound, REGSTR_PATH_INTERNET_SETTINGS, REGSTR_VAL_ENABLEAUTODIAL, HKEY_CURRENT_USER ));
  77. if(fFound && dwValue != 0) *pVal = VARIANT_TRUE;
  78. hr = S_OK;
  79. __HCP_FUNC_CLEANUP;
  80. __HCP_FUNC_EXIT(hr);
  81. }
  82. STDMETHODIMP CPCHConnectivity::get_HasConnectoid( /*[out, retval]*/ VARIANT_BOOL *pVal )
  83. {
  84. __HCP_FUNC_ENTRY( "CPCHConnectivity::get_HasConnectoid" );
  85. HRESULT hr;
  86. RASENTRYNAMEW* pEntries = NULL;
  87. DWORD cb = sizeof(*pEntries);
  88. DWORD cEntries = 0;
  89. DWORD dwRet;
  90. __MPC_PARAMCHECK_BEGIN(hr)
  91. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,VARIANT_FALSE);
  92. __MPC_PARAMCHECK_END();
  93. for(int pass=0; pass<2; pass++)
  94. {
  95. __MPC_EXIT_IF_ALLOC_FAILS(hr, pEntries, (RASENTRYNAMEW*)malloc( cb ));
  96. ::ZeroMemory( pEntries, cb ); pEntries[0].dwSize = sizeof(*pEntries);
  97. dwRet = ::RasEnumEntriesW( NULL, NULL, pEntries, &cb, &cEntries );
  98. if(dwRet == ERROR_SUCCESS) break;
  99. if(dwRet == ERROR_BUFFER_TOO_SMALL)
  100. {
  101. free( pEntries ); pEntries = NULL; continue;
  102. }
  103. __MPC_SET_WIN32_ERROR_AND_EXIT(hr, dwRet);
  104. }
  105. *pVal = (cEntries > 0) ? VARIANT_TRUE : VARIANT_FALSE;
  106. hr = S_OK;
  107. __HCP_FUNC_CLEANUP;
  108. if(pEntries) free( pEntries );
  109. __HCP_FUNC_EXIT(hr);
  110. }
  111. STDMETHODIMP CPCHConnectivity::get_IPAddresses( /*[out, retval]*/ BSTR *pVal )
  112. {
  113. __HCP_FUNC_ENTRY( "CPCHConnectivity::get_IPAddresses" );
  114. HRESULT hr;
  115. IP_ADAPTER_INFO* pAdapterInfo = NULL;
  116. ULONG cb = 1024; // Start with a default buffer.
  117. MPC::wstring strList;
  118. DWORD dwRet;
  119. __MPC_PARAMCHECK_BEGIN(hr)
  120. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,NULL);
  121. __MPC_PARAMCHECK_END();
  122. for(int pass=0; pass<2; pass++)
  123. {
  124. __MPC_EXIT_IF_ALLOC_FAILS(hr, pAdapterInfo, (IP_ADAPTER_INFO*)malloc( cb ));
  125. ::ZeroMemory( pAdapterInfo, cb );
  126. dwRet = ::GetAdaptersInfo( pAdapterInfo, &cb );
  127. if(dwRet == ERROR_SUCCESS) break;
  128. if(dwRet == ERROR_BUFFER_TOO_SMALL)
  129. {
  130. free( pAdapterInfo ); pAdapterInfo = NULL; continue;
  131. }
  132. __MPC_SET_WIN32_ERROR_AND_EXIT(hr, dwRet);
  133. }
  134. if(pAdapterInfo)
  135. {
  136. IP_ADAPTER_INFO* ptr = pAdapterInfo;
  137. while(ptr)
  138. {
  139. IP_ADDR_STRING* addr = &ptr->IpAddressList;
  140. while(addr)
  141. {
  142. WCHAR rgAddr[4 * 4 + 1]; ::MultiByteToWideChar( CP_ACP, 0, addr->IpAddress.String, -1, rgAddr, MAXSTRLEN(rgAddr) ); rgAddr[MAXSTRLEN(rgAddr)] = 0;
  143. if(strList.size()) strList.append( L";" );
  144. strList.append( rgAddr );
  145. addr = addr->Next;
  146. }
  147. ptr = ptr->Next;
  148. }
  149. }
  150. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::GetBSTR( strList.c_str(), pVal ));
  151. hr = S_OK;
  152. __HCP_FUNC_CLEANUP;
  153. if(pAdapterInfo) free( pAdapterInfo );
  154. __HCP_FUNC_EXIT(hr);
  155. }
  156. ////////////////////////////////////////
  157. STDMETHODIMP CPCHConnectivity::CreateObject_ConnectionCheck( /*[out, retval]*/ IPCHConnectionCheck* *ppCC )
  158. {
  159. __HCP_FUNC_ENTRY( "CPCHConnectivity::CreateObject_ConnectionCheck" );
  160. HRESULT hr;
  161. CComPtr<CPCHConnectionCheck> cc;
  162. __MPC_PARAMCHECK_BEGIN(hr)
  163. __MPC_PARAMCHECK_POINTER_AND_SET(ppCC,NULL);
  164. __MPC_PARAMCHECK_END();
  165. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &cc ));
  166. __MPC_EXIT_IF_METHOD_FAILS(hr, cc.QueryInterface( ppCC ));
  167. hr = S_OK;
  168. __HCP_FUNC_CLEANUP;
  169. __HCP_FUNC_EXIT(hr);
  170. }
  171. ////////////////////////////////////////
  172. STDMETHODIMP CPCHConnectivity::NetworkAlive( /*[out, retval]*/ VARIANT_BOOL *pVal )
  173. {
  174. __HCP_FUNC_ENTRY( "CPCHConnectivity::NetworkAlive" );
  175. HRESULT hr;
  176. __MPC_PARAMCHECK_BEGIN(hr)
  177. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,VARIANT_FALSE);
  178. __MPC_PARAMCHECK_END();
  179. *pVal = SUCCEEDED(MPC::Connectivity::NetworkAlive( HC_TIMEOUT_NETWORKALIVE )) ? VARIANT_TRUE : VARIANT_FALSE;
  180. hr = S_OK;
  181. __HCP_FUNC_CLEANUP;
  182. __HCP_FUNC_EXIT(hr);
  183. }
  184. STDMETHODIMP CPCHConnectivity::DestinationReachable( /*[in]*/ BSTR bstrURL, /*[out, retval]*/ VARIANT_BOOL *pVal )
  185. {
  186. __HCP_FUNC_ENTRY( "CPCHConnectivity::DestinationReachable" );
  187. HRESULT hr;
  188. __MPC_PARAMCHECK_BEGIN(hr)
  189. __MPC_PARAMCHECK_STRING_NOT_EMPTY(bstrURL);
  190. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,VARIANT_FALSE);
  191. __MPC_PARAMCHECK_END();
  192. if(SUCCEEDED(HyperLinks::IsValid ( bstrURL )) &&
  193. SUCCEEDED(MPC::Connectivity::DestinationReachable( bstrURL, HC_TIMEOUT_DESTINATIONREACHABLE, NULL )) )
  194. {
  195. *pVal = VARIANT_TRUE;
  196. }
  197. hr = S_OK;
  198. __HCP_FUNC_CLEANUP;
  199. __HCP_FUNC_EXIT(hr);
  200. }
  201. ////////////////////////////////////////
  202. STDMETHODIMP CPCHConnectivity::AutoDial( /*[in]*/ VARIANT_BOOL bUnattended )
  203. {
  204. __HCP_FUNC_ENTRY( "CPCHConnectivity::AutoDial" );
  205. HRESULT hr;
  206. //
  207. // Only call the Autodial API if we are really offline.
  208. //
  209. if(FAILED(MPC::Connectivity::NetworkAlive( HC_TIMEOUT_NETWORKALIVE )))
  210. {
  211. if(!::InternetAutodial( bUnattended == VARIANT_TRUE ? INTERNET_AUTODIAL_FORCE_UNATTENDED : INTERNET_AUTODIAL_FORCE_ONLINE, NULL ))
  212. {
  213. __MPC_SET_WIN32_ERROR_AND_EXIT(hr, ::GetLastError());
  214. }
  215. }
  216. hr = S_OK;
  217. __HCP_FUNC_CLEANUP;
  218. __HCP_FUNC_EXIT(hr);
  219. }
  220. STDMETHODIMP CPCHConnectivity::AutoDialHangup()
  221. {
  222. __HCP_FUNC_ENTRY( "CPCHConnectivity::AutoDialHangup" );
  223. HRESULT hr;
  224. if(!::InternetAutodialHangup( 0 ))
  225. {
  226. __MPC_SET_WIN32_ERROR_AND_EXIT(hr, ::GetLastError());
  227. }
  228. hr = S_OK;
  229. __HCP_FUNC_CLEANUP;
  230. __HCP_FUNC_EXIT(hr);
  231. }
  232. STDMETHODIMP CPCHConnectivity::NavigateOnline( /*[in ]*/ BSTR bstrTargetURL ,
  233. /*[in ]*/ BSTR bstrTopicTitle ,
  234. /*[in ]*/ BSTR bstrTopicIntro ,
  235. /*[in,optional]*/ VARIANT vOfflineURL )
  236. {
  237. __HCP_FUNC_ENTRY( "CPCHConnectivity::NavigateOnline" );
  238. HRESULT hr;
  239. MPC::wstring strURL;
  240. __MPC_PARAMCHECK_BEGIN(hr)
  241. __MPC_PARAMCHECK_STRING_NOT_EMPTY(bstrTargetURL);
  242. __MPC_PARAMCHECK_NOTNULL(m_parent);
  243. __MPC_PARAMCHECK_END();
  244. MPC::HTML::vBuildHREF( strURL, c_szURL_Connection, L"online_url" , bstrTargetURL ,
  245. L"topic_title", bstrTopicTitle ,
  246. L"topic_intro", bstrTopicIntro ,
  247. L"offline_url", vOfflineURL.vt == VT_BSTR ? vOfflineURL .bstrVal : NULL,
  248. NULL );
  249. __MPC_EXIT_IF_METHOD_FAILS(hr, m_parent->SetPanelUrl( HSCPANEL_CONTENTS, strURL.c_str() ));
  250. hr = S_OK;
  251. __HCP_FUNC_CLEANUP;
  252. __HCP_FUNC_EXIT(hr);
  253. }