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.

1864 lines
47 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: cnetapi.cpp
  7. //
  8. // Contents: Network/SENS API wrappers
  9. //
  10. // Classes:
  11. //
  12. // Notes:
  13. //
  14. // History: 08-Dec-97 rogerg Created.
  15. //
  16. //--------------------------------------------------------------------------
  17. #include "precomp.h"
  18. // SENS DLL and function strings
  19. STRING_FILENAME(szSensApiDll, "SensApi.dll");
  20. STRING_INTERFACE(szIsNetworkAlive,"IsNetworkAlive");
  21. // RAS Dll and Function Strings
  22. STRING_FILENAME(szRasDll, "RASAPI32.DLL");
  23. // RAS function strings
  24. STRING_INTERFACE(szRasEnumConnectionsW,"RasEnumConnectionsW");
  25. STRING_INTERFACE(szRasEnumConnectionsA,"RasEnumConnectionsA");
  26. STRING_INTERFACE(szRasEnumEntriesA,"RasEnumEntriesA");
  27. STRING_INTERFACE(szRasEnumEntriesW,"RasEnumEntriesW");
  28. STRING_INTERFACE(szRasGetEntryPropertiesW,"RasGetEntryPropertiesW");
  29. STRING_INTERFACE(szRasGetErrorStringW,"RasGetErrorStringW");
  30. STRING_INTERFACE(szRasGetErrorStringA,"RasGetErrorStringA");
  31. STRING_INTERFACE(szRasGetAutodialParam, "RasGetAutodialParamA");
  32. STRING_INTERFACE(szRasSetAutodialParam, "RasSetAutodialParamA");
  33. #if 0
  34. RSTRING_INTERFACE(szRasDial,"RasDialW");
  35. STRING_INTERFACE(szRasHangup,"RasHangUpW");
  36. STRING_INTERFACE(szRasGetConnectStatus,"RasGetConnectStatusW");
  37. STRING_INTERFACE(szRasGetEntryDialParams,"RasGetEntryDialParamsW");
  38. STRING_INTERFACE(szRasGetAutodialParam, "RasGetAutodialParamW");
  39. STRING_INTERFACE(szRasSetAutodialParam, "RasSetAutodialParamW");
  40. STRING_INTERFACE(szRasDial,"RasDialA");
  41. STRING_INTERFACE(szRasHangup,"RasHangUpA");
  42. STRING_INTERFACE(szRasGetConnectStatus,"RasGetConnectStatusA");
  43. STRING_INTERFACE(szRasGetEntryDialParams,"RasGetEntryDialParamsA");
  44. #endif
  45. // wininet declarations
  46. // warning - IE 4.0 only exported InternetDial which was ANSI. IE5 has InternetDailA and
  47. // internetDialW. we always use InternetDial for Ansi. So we prefere InternetDialW but
  48. // must fall back to ANSI for IE 4.0
  49. STRING_FILENAME(szWinInetDll, "WININET.DLL");
  50. STRING_INTERFACE(szInternetDial,"InternetDial");
  51. STRING_INTERFACE(szInternetDialW,"InternetDialW");
  52. STRING_INTERFACE(szInternetHangup,"InternetHangUp");
  53. STRING_INTERFACE(szInternetAutodial,"InternetAutodial");
  54. STRING_INTERFACE(szInternetAutodialHangup,"InternetAutodialHangup");
  55. STRING_INTERFACE(szInternetQueryOption,"InternetQueryOptionA"); // always use the A Version
  56. STRING_INTERFACE(szInternetSetOption,"InternetSetOptionA"); // always use A Version
  57. // SENS install key under HKLM
  58. const WCHAR wszSensInstallKey[] = TEXT("Software\\Microsoft\\Mobile\\Sens");
  59. extern OSVERSIONINFOA g_OSVersionInfo; // osVersionInfo.
  60. //+---------------------------------------------------------------------------
  61. //
  62. // Member: CNetApi::CNetApi, public
  63. //
  64. // Synopsis: Constructor
  65. //
  66. // Arguments:
  67. //
  68. // Returns:
  69. //
  70. // Modifies:
  71. //
  72. // History: 08-Dec-97 rogerg Created.
  73. //
  74. //----------------------------------------------------------------------------
  75. CNetApi::CNetApi()
  76. {
  77. m_fTriedToLoadSens = FALSE;
  78. m_hInstSensApiDll = NULL;
  79. m_pIsNetworkAlive = NULL;
  80. m_fTriedToLoadRas = FALSE;
  81. m_hInstRasApiDll = NULL;
  82. m_pRasEnumConnectionsW = NULL;
  83. m_pRasEnumConnectionsA = NULL;
  84. m_pRasEnumEntriesA = NULL;
  85. m_pRasEnumEntriesW = NULL;
  86. m_pRasGetEntryPropertiesW = NULL;
  87. m_pRasGetErrorStringW = NULL;
  88. m_pRasGetErrorStringA = NULL;
  89. m_pRasGetAutodialParam = NULL;
  90. m_pRasSetAutodialParam = NULL;
  91. #if 0
  92. m_pRasDial = NULL;
  93. m_pRasHangup = NULL;
  94. m_pRasConnectStatus = NULL;
  95. m_pRasEntryGetDialParams = NULL;
  96. #endif // 0
  97. m_fTriedToLoadWinInet = FALSE;
  98. m_hInstWinInetDll = NULL;
  99. m_pInternetDial = NULL;
  100. m_pInternetDialW = NULL;
  101. m_pInternetHangUp = NULL;
  102. m_pInternetAutodial = NULL;
  103. m_pInternetAutodialHangup = NULL;
  104. m_pInternetQueryOption = NULL;
  105. m_pInternetSetOption = NULL;
  106. m_fIsUnicode = WideWrapIsUnicode();
  107. m_cRefs = 1;
  108. }
  109. //+---------------------------------------------------------------------------
  110. //
  111. // Member: CNetApi::~CNetApi, public
  112. //
  113. // Synopsis: Destructor
  114. //
  115. // Arguments:
  116. //
  117. // Returns:
  118. //
  119. // Modifies:
  120. //
  121. // History: 08-Dec-97 rogerg Created.
  122. //
  123. //----------------------------------------------------------------------------
  124. CNetApi::~CNetApi()
  125. {
  126. Assert(0 == m_cRefs);
  127. if (NULL != m_hInstSensApiDll)
  128. {
  129. FreeLibrary(m_hInstSensApiDll);
  130. }
  131. if (NULL != m_hInstWinInetDll)
  132. {
  133. FreeLibrary(m_hInstWinInetDll);
  134. }
  135. if (NULL != m_hInstRasApiDll)
  136. {
  137. FreeLibrary(m_hInstWinInetDll);
  138. }
  139. }
  140. //+-------------------------------------------------------------------------
  141. //
  142. // Method: CNetApi::QueryInterface
  143. //
  144. // Synopsis: Increments refcount
  145. //
  146. // History: 31-Jul-1998 SitaramR Created
  147. //
  148. //--------------------------------------------------------------------------
  149. STDMETHODIMP CNetApi::QueryInterface( REFIID, LPVOID* )
  150. {
  151. AssertSz(0,"E_NOTIMPL");
  152. return E_NOINTERFACE;
  153. }
  154. //+-------------------------------------------------------------------------
  155. //
  156. // Method: CNetApiXf
  157. //
  158. // Synopsis: Increments refcount
  159. //
  160. // History: 31-Jul-1998 SitaramR Created
  161. //
  162. //--------------------------------------------------------------------------
  163. STDMETHODIMP_(ULONG) CNetApi::AddRef()
  164. {
  165. DWORD dwTmp = InterlockedIncrement( (long *) &m_cRefs );
  166. return dwTmp;
  167. }
  168. //+-------------------------------------------------------------------------
  169. //
  170. // Method: CNetApi::Release
  171. //
  172. // Synopsis: Decrement refcount. Delete if necessary.
  173. //
  174. // History: 31-Jul-1998 SitaramR Created
  175. //
  176. //--------------------------------------------------------------------------
  177. STDMETHODIMP_(ULONG) CNetApi::Release()
  178. {
  179. Assert( m_cRefs > 0 );
  180. DWORD dwTmp = InterlockedDecrement( (long *) &m_cRefs );
  181. if ( 0 == dwTmp )
  182. delete this;
  183. return dwTmp;
  184. }
  185. //+---------------------------------------------------------------------------
  186. //
  187. // Member: CNetApi::LoadSensDll
  188. //
  189. // Synopsis: Trys to Load Sens Library.
  190. //
  191. // Arguments:
  192. //
  193. // Returns: NOERROR if successfull.
  194. //
  195. // Modifies:
  196. //
  197. // History: 08-Dec-97 rogerg Created.
  198. //
  199. //----------------------------------------------------------------------------
  200. STDMETHODIMP CNetApi::LoadSensDll()
  201. {
  202. HRESULT hr = S_FALSE;
  203. if (m_fTriedToLoadSens)
  204. {
  205. return m_hInstSensApiDll ? NOERROR : S_FALSE;
  206. }
  207. CLock lock(this);
  208. lock.Enter();
  209. if (!m_fTriedToLoadSens)
  210. {
  211. Assert(NULL == m_hInstSensApiDll);
  212. m_hInstSensApiDll = LoadLibrary(szSensApiDll);
  213. if (m_hInstSensApiDll)
  214. {
  215. // for now, don't return an error is GetProc Fails but check in each function.
  216. m_pIsNetworkAlive = (ISNETWORKALIVE)
  217. GetProcAddress(m_hInstSensApiDll, szIsNetworkAlive);
  218. }
  219. if (NULL == m_hInstSensApiDll
  220. || NULL == m_pIsNetworkAlive)
  221. {
  222. hr = S_FALSE;
  223. if (m_hInstSensApiDll)
  224. {
  225. FREE(m_hInstSensApiDll);
  226. m_hInstSensApiDll = NULL;
  227. }
  228. }
  229. else
  230. {
  231. hr = NOERROR;
  232. }
  233. m_fTriedToLoadSens = TRUE; // set after all initialization is done.
  234. }
  235. else
  236. {
  237. hr = m_hInstSensApiDll ? NOERROR : S_FALSE;
  238. }
  239. lock.Leave();
  240. return hr;
  241. }
  242. //+---------------------------------------------------------------------------
  243. //
  244. // Member: CNetApi::IsNetworkAlive, public
  245. //
  246. // Synopsis: Calls the Sens IsNetworkAlive API.
  247. //
  248. // Arguments:
  249. //
  250. // Returns: IsNetworkAlive results or FALSE is failed to load
  251. // sens or import.
  252. //
  253. // Modifies:
  254. //
  255. // History: 08-Dec-97 rogerg Created.
  256. //
  257. //----------------------------------------------------------------------------
  258. STDMETHODIMP_(BOOL) CNetApi::IsNetworkAlive(LPDWORD lpdwFlags)
  259. {
  260. //
  261. // Sens load fail is not an error
  262. //
  263. LoadSensDll();
  264. BOOL fResult = FALSE;
  265. if (NULL == m_pIsNetworkAlive)
  266. {
  267. DWORD cbNumEntries;
  268. RASCONN *pWanConnections;
  269. // if couldn't load export see if there are any WAN Connections.
  270. if (NOERROR == GetWanConnections(&cbNumEntries,&pWanConnections))
  271. {
  272. if (cbNumEntries)
  273. {
  274. fResult = TRUE;
  275. *lpdwFlags = NETWORK_ALIVE_WAN;
  276. }
  277. if (pWanConnections)
  278. {
  279. FreeWanConnections(pWanConnections);
  280. }
  281. }
  282. // for testing without sens
  283. // fResult = TRUE;
  284. // *lpdwFlags |= NETWORK_ALIVE_LAN;
  285. // end of testing without sens
  286. }
  287. else
  288. {
  289. fResult = m_pIsNetworkAlive(lpdwFlags);
  290. }
  291. return fResult;
  292. }
  293. //+---------------------------------------------------------------------------
  294. //
  295. // Member: CNetApi::IsSensInstalled, public
  296. //
  297. // Synopsis: Determines if SENS is installed on the System.
  298. //
  299. // Arguments:
  300. //
  301. // Returns: TRUE if sens is installed
  302. //
  303. // Modifies:
  304. //
  305. // History: 12-Aug-98 Kyle Created.
  306. //
  307. //----------------------------------------------------------------------------
  308. STDMETHODIMP_(BOOL) CNetApi::IsSensInstalled(void)
  309. {
  310. HKEY hkResult;
  311. BOOL fResult = FALSE;
  312. if (ERROR_SUCCESS == RegOpenKeyExXp(HKEY_LOCAL_MACHINE,wszSensInstallKey,0,
  313. KEY_READ,&hkResult,FALSE /*fSetSecurity*/))
  314. {
  315. fResult = TRUE;
  316. RegCloseKey(hkResult);
  317. }
  318. return fResult;
  319. }
  320. //+---------------------------------------------------------------------------
  321. //
  322. // Member: CNetApi::GetWanConnections, public
  323. //
  324. // Synopsis: returns an array of Active Wan connections.
  325. // up to the caller to free RasEntries structure when done.
  326. //
  327. // Arguments: [out] [cbNumEntries] - Number of Connections found
  328. // [out] [pWanConnections] - Array of Connections found.
  329. //
  330. // Returns: IsNetworkAlive results or FALSE is failed to load
  331. // sens or import.
  332. //
  333. // Modifies:
  334. //
  335. // History: 08-Dec-97 rogerg Created.
  336. //
  337. //----------------------------------------------------------------------------
  338. STDMETHODIMP CNetApi::GetWanConnections(DWORD *cbNumEntries,RASCONN **pWanConnections)
  339. {
  340. DWORD dwError = -1;
  341. DWORD dwSize;
  342. DWORD cConnections;
  343. *pWanConnections = NULL;
  344. *pWanConnections = 0;
  345. LPRASCONN lpRasConn;
  346. dwSize = sizeof(RASCONN);
  347. lpRasConn = (LPRASCONN) ALLOC(dwSize);
  348. if(lpRasConn)
  349. {
  350. lpRasConn->dwSize = sizeof(RASCONN);
  351. cConnections = 0;
  352. dwError = RasEnumConnections(lpRasConn, &dwSize, &cConnections);
  353. if (dwError == ERROR_BUFFER_TOO_SMALL)
  354. {
  355. dwSize = lpRasConn->dwSize; // get size needed
  356. FREE(lpRasConn);
  357. lpRasConn = (LPRASCONN) ALLOC(dwSize);
  358. if(lpRasConn)
  359. {
  360. lpRasConn->dwSize = sizeof(RASCONN);
  361. cConnections = 0;
  362. dwError = RasEnumConnections(lpRasConn, &dwSize, &cConnections);
  363. }
  364. }
  365. }
  366. if (!dwError && lpRasConn)
  367. {
  368. *cbNumEntries = cConnections;
  369. *pWanConnections = lpRasConn;
  370. return NOERROR;
  371. }
  372. else
  373. {
  374. if (lpRasConn)
  375. {
  376. FREE(lpRasConn);
  377. }
  378. }
  379. return S_FALSE;
  380. }
  381. //+---------------------------------------------------------------------------
  382. //
  383. // Member: CNetApi::FreeWanConnections, public
  384. //
  385. // Synopsis: Called by caller to free up memory
  386. // allocated by GetWanConnections.
  387. //
  388. // Arguments: [in] [pWanConnections] - WanConnection Array to free
  389. //
  390. // Returns:
  391. //
  392. // Modifies:
  393. //
  394. // History: 08-Dec-98 rogerg Created.
  395. //
  396. //----------------------------------------------------------------------------
  397. STDMETHODIMP CNetApi::FreeWanConnections(RASCONN *pWanConnections)
  398. {
  399. Assert(pWanConnections);
  400. if (pWanConnections)
  401. {
  402. FREE(pWanConnections);
  403. }
  404. return NOERROR;
  405. }
  406. //+---------------------------------------------------------------------------
  407. //
  408. // Member: CNetApi::RasEnumConnections, public
  409. //
  410. // Synopsis: Wraps RasEnumConnections.
  411. //
  412. // Arguments:
  413. //
  414. // Returns:
  415. //
  416. // Modifies:
  417. //
  418. // History: 02-Aug-98 rogerg Created.
  419. //
  420. //----------------------------------------------------------------------------
  421. STDMETHODIMP_(DWORD) CNetApi::RasEnumConnections(LPRASCONNW lprasconn,LPDWORD lpcb,LPDWORD lpcConnections)
  422. {
  423. DWORD dwReturn = -1;
  424. if (NOERROR != LoadRasApiDll())
  425. return -1;
  426. if(m_fIsUnicode && m_pRasEnumConnectionsW)
  427. {
  428. dwReturn = (*m_pRasEnumConnectionsW)(lprasconn,lpcb,lpcConnections);
  429. }
  430. else if (m_pRasEnumConnectionsA)
  431. {
  432. DWORD cbNumRasConn;
  433. LPRASCONNA pRasConnA = NULL;
  434. DWORD cbBufSizeA;
  435. // allocate number of RASCONNA names that can
  436. // be thunked back to RASCONNW.
  437. cbNumRasConn = (*lpcb)/sizeof(RASCONNW);
  438. Assert(cbNumRasConn > 0);
  439. cbBufSizeA = cbNumRasConn*sizeof(RASCONNA);
  440. if (cbBufSizeA)
  441. {
  442. pRasConnA = (LPRASCONNA) ALLOC(cbBufSizeA);
  443. if (pRasConnA)
  444. {
  445. pRasConnA->dwSize = sizeof(RASCONNA);
  446. }
  447. }
  448. dwReturn = (*m_pRasEnumConnectionsA)(pRasConnA,&cbBufSizeA,lpcConnections);
  449. // fudge and say the cbBufSize necessary is the returned size *2 so
  450. // wide enough for WCHAR
  451. *lpcb = cbBufSizeA*2;
  452. // if no error thunk then entries back to WCHAR.
  453. if (0 == dwReturn && pRasConnA)
  454. {
  455. DWORD dwEntries = *lpcConnections;
  456. LPRASCONNA pCurRasEntryNameA = pRasConnA;
  457. LPRASCONNW pCurRasEntryNameW = lprasconn;
  458. int iFailCount = 0;
  459. while (dwEntries--)
  460. {
  461. //!!! we only conver the entry name if need other fields
  462. // will have to convert these as well.
  463. if (!ConvertString(pCurRasEntryNameW->szEntryName,pCurRasEntryNameA->szEntryName
  464. ,sizeof(pCurRasEntryNameW->szEntryName)))
  465. {
  466. ++iFailCount;
  467. }
  468. ++pCurRasEntryNameW;
  469. ++pCurRasEntryNameA;
  470. }
  471. if (iFailCount)
  472. {
  473. Assert(0 == iFailCount);
  474. dwReturn = -1;
  475. }
  476. }
  477. if (pRasConnA)
  478. {
  479. FREE(pRasConnA);
  480. }
  481. }
  482. return dwReturn;
  483. }
  484. //+---------------------------------------------------------------------------
  485. //
  486. // Member: CNetApi::GetConnectionStatus, private
  487. //
  488. // Synopsis: Given a Connection Name determines if the connection
  489. // has already been established.
  490. // Also set ths WanActive flag to indicate if there are any
  491. // existing RAS connections.
  492. //
  493. // Arguments: [pszConnectionName] - Name of the Connection
  494. // [out] [fConnected] - Indicates if specified connection is currently connected.
  495. // [out] [fCanEstablishConnection] - Flag indicates if the connection is not found can establish it.
  496. //
  497. // Returns: NOERROR if the dll was sucessfully loaded
  498. //
  499. // Modifies:
  500. //
  501. // History: 08-Dec-97 rogerg Created.
  502. //
  503. //----------------------------------------------------------------------------
  504. STDMETHODIMP CNetApi::GetConnectionStatus(LPCTSTR pszConnectionName,DWORD dwConnectionType,BOOL *fConnected,BOOL *fCanEstablishConnection)
  505. {
  506. *fConnected = FALSE;
  507. *fCanEstablishConnection = FALSE;
  508. // if this is a lan connection then see if network is alive,
  509. // else go through the Ras apis.
  510. if (CNETAPI_CONNECTIONTYPELAN == dwConnectionType)
  511. {
  512. DWORD dwFlags;
  513. if (IsNetworkAlive(&dwFlags)
  514. && (dwFlags & NETWORK_ALIVE_LAN) )
  515. {
  516. *fConnected = TRUE;
  517. }
  518. }
  519. else
  520. { // check for Ras Connections.
  521. RASCONN *pWanConnections;
  522. DWORD cbNumConnections;
  523. if (NOERROR == GetWanConnections(&cbNumConnections,&pWanConnections))
  524. {
  525. *fCanEstablishConnection = TRUE;
  526. if (cbNumConnections > 0)
  527. {
  528. *fCanEstablishConnection = FALSE;
  529. // loop through the entries to see if this connection is already
  530. // connected.
  531. while (cbNumConnections)
  532. {
  533. cbNumConnections--;
  534. if (0 == lstrcmp(pWanConnections[cbNumConnections].szEntryName,pszConnectionName))
  535. {
  536. *fConnected = TRUE;
  537. break;
  538. }
  539. }
  540. }
  541. if (pWanConnections)
  542. {
  543. FreeWanConnections(pWanConnections);
  544. }
  545. }
  546. }
  547. return NOERROR;
  548. }
  549. #if 0
  550. //+---------------------------------------------------------------------------
  551. //
  552. // Member: CNetApi::RasDial, private
  553. //
  554. // Synopsis: Given a Connection tries to dial it.
  555. //
  556. // Arguments: [pszConnectionName]
  557. // Returns: NOERROR if the dial was successfull.
  558. //
  559. // Modifies:
  560. //
  561. // History: 08-Dec-97 rogerg Created.
  562. //
  563. //----------------------------------------------------------------------------
  564. DWORD CNetApi::RasDial(TCHAR *pszConnectionName,HRASCONN *phRasConn)
  565. {
  566. DWORD dwErr = -1;
  567. RASDIALPARAMS rasDialParams;
  568. *phRasConn = 0;
  569. if (NOERROR != LoadRasApiDll())
  570. return -1;
  571. memset(&rasDialParams,0,sizeof(RASDIALPARAMS));
  572. rasDialParams.dwSize = sizeof(RASDIALPARAMS);
  573. lstrcpy(rasDialParams.szEntryName,pszConnectionName);
  574. dwErr = (*m_pRasDial)( NULL, NULL, &rasDialParams, 0,
  575. NULL, phRasConn);
  576. if(dwErr)
  577. {
  578. if(*phRasConn)
  579. {
  580. m_pRasHangup(*phRasConn);
  581. *phRasConn = 0;
  582. }
  583. }
  584. return dwErr;
  585. }
  586. //+---------------------------------------------------------------------------
  587. //
  588. // Member: CNetApi::RasDialDlg, public
  589. //
  590. // Synopsis: Given a Connection tries to dial it.
  591. //
  592. // Arguments: [pszConnectionName]
  593. // [phRasConn] - return HRASCONN if dial was successful.
  594. //
  595. // Returns: NOERROR if the dial was successfull.
  596. //
  597. // Modifies:
  598. //
  599. // History: 08-Dec-97 rogerg Created.
  600. //
  601. //----------------------------------------------------------------------------
  602. DWORD CNetApi::RasDialDlg(TCHAR *pszConnectionName,HRASCONN *phRasConn)
  603. {
  604. CRasDialDlg *pRasDialDlg = NULL;
  605. DWORD dwErr = -1;
  606. *phRasConn = 0;
  607. if (NOERROR == LoadRasApiDll())
  608. {
  609. pRasDialDlg = new CRasDialDlg(this);
  610. if (NULL != pRasDialDlg)
  611. {
  612. dwErr = pRasDialDlg->Dial(pszConnectionName,phRasConn);
  613. delete pRasDialDlg;
  614. }
  615. }
  616. return dwErr;
  617. }
  618. //+---------------------------------------------------------------------------
  619. //
  620. // Member: CNetApi::RasDialProc, public
  621. //
  622. // Synopsis: Directly calls RasDial()
  623. //
  624. // Arguments:
  625. //
  626. // Returns: Appropriate Error codes
  627. //
  628. // Modifies:
  629. //
  630. // History: 08-Dec-97 rogerg Created.
  631. //
  632. //----------------------------------------------------------------------------
  633. DWORD CNetApi::RasDialProc(LPRASDIALEXTENSIONS lpRasDialExtensions,
  634. LPTSTR lpszPhonebook, LPRASDIALPARAMS lpRasDialParams,
  635. DWORD dwNotifierType, LPVOID lpvNotifier,
  636. LPHRASCONN phRasConn)
  637. {
  638. DWORD dwErr = -1;
  639. CRasDialDlg *pRasDialDlg = NULL;
  640. *phRasConn = 0;
  641. if (NOERROR != LoadRasApiDll())
  642. return -1;
  643. dwErr = (*m_pRasDial)( lpRasDialExtensions, lpszPhonebook, lpRasDialParams,
  644. dwNotifierType,lpvNotifier,phRasConn);
  645. if(dwErr)
  646. {
  647. if(*phRasConn)
  648. {
  649. m_pRasHangup(*phRasConn);
  650. *phRasConn = 0;
  651. }
  652. }
  653. return dwErr;
  654. }
  655. //+---------------------------------------------------------------------------
  656. //
  657. // Member: CNetApi::RasHangUpProc, public
  658. //
  659. // Synopsis: Directly calls RasDial()
  660. //
  661. // Arguments:
  662. //
  663. // Returns: Appropriate Error codes
  664. //
  665. // Modifies:
  666. //
  667. // History: 08-Dec-97 rogerg Created.
  668. //
  669. //----------------------------------------------------------------------------
  670. DWORD CNetApi::RasHangUpProc( HRASCONN hrasconn)
  671. {
  672. DWORD dwErr = -1;
  673. if (NOERROR != LoadRasApiDll())
  674. return -1;
  675. dwErr = m_pRasHangup(hrasconn);
  676. return dwErr;
  677. }
  678. //+---------------------------------------------------------------------------
  679. //
  680. // Member: CNetApi::RasGetConnectStatusProc, public
  681. //
  682. // Synopsis: Directly calls RasGetConnectStatus()
  683. //
  684. // Arguments:
  685. //
  686. // Returns: Appropriate Error codes
  687. //
  688. // Modifies:
  689. //
  690. // History: 08-Dec-97 rogerg Created.
  691. //
  692. //----------------------------------------------------------------------------
  693. DWORD CNetApi::RasGetConnectStatusProc(HRASCONN hrasconn,LPRASCONNSTATUS lprasconnstatus)
  694. {
  695. DWORD dwErr = -1;
  696. if (NOERROR != LoadRasApiDll())
  697. return -1;
  698. dwErr = m_pRasConnectStatus(hrasconn,lprasconnstatus);
  699. return dwErr;
  700. }
  701. #endif // if 0
  702. //+---------------------------------------------------------------------------
  703. //
  704. // Member: CNetApi::RasGetErrorStringProc, public
  705. //
  706. // Synopsis: Directly calls RasGetErrorString()
  707. //
  708. // Arguments:
  709. //
  710. // Returns: Appropriate Error codes
  711. //
  712. // Modifies:
  713. //
  714. // History: 08-Dec-97 rogerg Created.
  715. //
  716. //----------------------------------------------------------------------------
  717. STDMETHODIMP_(DWORD) CNetApi::RasGetErrorStringProc( UINT uErrorValue, LPTSTR lpszErrorString,DWORD cBufSize)
  718. {
  719. DWORD dwErr = -1;
  720. if (NOERROR != LoadRasApiDll())
  721. return -1;
  722. if ( m_fIsUnicode && m_pRasGetErrorStringW)
  723. {
  724. dwErr = m_pRasGetErrorStringW(uErrorValue,lpszErrorString,cBufSize);
  725. }
  726. else
  727. {
  728. XArray<CHAR> xszErrString;
  729. BOOL fOk = xszErrString.Init( cBufSize );
  730. if ( !fOk )
  731. {
  732. SetLastError( ERROR_OUTOFMEMORY );
  733. return dwErr;
  734. }
  735. dwErr = m_pRasGetErrorStringA( uErrorValue, xszErrString.Get(), cBufSize );
  736. if ( dwErr != ERROR_SUCCESS )
  737. return dwErr;
  738. XArray<WCHAR> xwszOutErr;
  739. fOk = ConvertMultiByteToWideChar( xszErrString.Get(), xwszOutErr );
  740. if ( !fOk )
  741. {
  742. SetLastError( ERROR_OUTOFMEMORY );
  743. return -1;
  744. }
  745. ULONG ulLen = lstrlenX( xwszOutErr.Get() );
  746. if ( ulLen > cBufSize-1 )
  747. {
  748. //
  749. // Truncate error message to fit
  750. //
  751. lstrcpynX( lpszErrorString, xwszOutErr.Get(), cBufSize-1 );
  752. lpszErrorString[cBufSize-1] = 0;
  753. }
  754. else
  755. lstrcpyX( lpszErrorString, xwszOutErr.Get() );
  756. }
  757. return ERROR_SUCCESS;
  758. }
  759. #if 0
  760. //+---------------------------------------------------------------------------
  761. //
  762. // Member: CNetApi::RasGetEntryDialParamsProc, public
  763. //
  764. // Synopsis: Directly calls RasGetEntryDialParams()
  765. //
  766. // Arguments:
  767. //
  768. // Returns: Appropriate Error codes
  769. //
  770. // Modifies:
  771. //
  772. // History: 08-Dec-97 rogerg Created.
  773. //
  774. //----------------------------------------------------------------------------
  775. STDMETHODIMP_(DWORD) CNetApi::RasGetEntryDialParamsProc(LPCTSTR lpszPhonebook,LPRASDIALPARAMS lprasdialparams,LPBOOL lpfPassword)
  776. {
  777. DWORD dwErr = -1;
  778. if (NOERROR != LoadRasApiDll())
  779. return -1;
  780. dwErr = m_pRasEntryGetDialParams(lpszPhonebook,lprasdialparams,lpfPassword);
  781. return dwErr;
  782. }
  783. //+---------------------------------------------------------------------------
  784. //
  785. // Member: CNetApi::RasHangup, public
  786. //
  787. // Synopsis: Hangs up a Ras Connection.
  788. //
  789. // Arguments: [hRasConn] - Ras Connection to Terminate
  790. // Returns: NOERROR if the hangup was successfull.
  791. //
  792. // Modifies:
  793. //
  794. // History: 08-Dec-97 rogerg Created.
  795. //
  796. //----------------------------------------------------------------------------
  797. STDMETHODIMP CNetApi::RasHangup(HRASCONN hRasConn)
  798. {
  799. DWORD dwErr = 0;
  800. if (NOERROR != LoadRasApiDll())
  801. return S_FALSE;
  802. dwErr = m_pRasHangup(hRasConn);
  803. return dwErr;
  804. }
  805. #endif // if 0
  806. //+---------------------------------------------------------------------------
  807. //
  808. // Member: CNetApi::RasEnumEntries, public
  809. //
  810. // Synopsis: wraps RasEnumEntries
  811. //
  812. // Arguments:
  813. //
  814. // Returns:
  815. //
  816. // Modifies:
  817. //
  818. // History: 08-Aug-98 rogerg Created.
  819. //
  820. //----------------------------------------------------------------------------
  821. DWORD CNetApi::RasEnumEntries(LPWSTR reserved,LPWSTR lpszPhoneBook,
  822. LPRASENTRYNAME lprasEntryName,LPDWORD lpcb,LPDWORD lpcEntries)
  823. {
  824. DWORD dwReturn = -1;
  825. if (NOERROR != LoadRasApiDll())
  826. return -1;
  827. if(m_fIsUnicode && m_pRasEnumEntriesW)
  828. {
  829. BOOL fIsNT = (g_OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
  830. // if NT 5.0 or greater need to call enum with NT 5 size
  831. // or entries will be missing.
  832. if (fIsNT && g_OSVersionInfo.dwMajorVersion >= 5)
  833. {
  834. dwReturn = RasEnumEntriesNT50(reserved,lpszPhoneBook,
  835. lprasEntryName,lpcb,lpcEntries);
  836. }
  837. else
  838. {
  839. dwReturn = (*m_pRasEnumEntriesW)(reserved,lpszPhoneBook,
  840. lprasEntryName,lpcb,lpcEntries);
  841. }
  842. }
  843. else if (m_pRasEnumEntriesA)
  844. {
  845. DWORD cbNumRasEntries;
  846. LPRASENTRYNAMEA pRasEntryNameA = NULL;
  847. DWORD cbBufSizeA;
  848. Assert(NULL == reserved);
  849. Assert(NULL == lpszPhoneBook);
  850. // allocate number of RASENTRYA names that can
  851. // be thunked back to RASENTRYW.
  852. cbNumRasEntries = (*lpcb)/sizeof(RASENTRYNAMEW);
  853. Assert(cbNumRasEntries > 0);
  854. cbBufSizeA = cbNumRasEntries*sizeof(RASENTRYNAMEA);
  855. if (cbBufSizeA)
  856. {
  857. pRasEntryNameA = (LPRASENTRYNAMEA) ALLOC(cbBufSizeA);
  858. if (pRasEntryNameA)
  859. {
  860. pRasEntryNameA->dwSize = sizeof(RASENTRYNAMEA);
  861. }
  862. }
  863. dwReturn = (*m_pRasEnumEntriesA)(NULL,NULL,
  864. pRasEntryNameA,&cbBufSizeA,lpcEntries);
  865. // fudge and say the cbBufSize necessary is the returned size *2 so
  866. // wide enough for WCHAR
  867. *lpcb = cbBufSizeA*2;
  868. // if no error thunk then entries back to WCHAR.
  869. if (0 == dwReturn && pRasEntryNameA)
  870. {
  871. DWORD dwEntries = *lpcEntries;
  872. LPRASENTRYNAMEA pCurRasEntryNameA = pRasEntryNameA;
  873. LPRASENTRYNAMEW pCurRasEntryNameW = lprasEntryName;
  874. int iFailCount = 0;
  875. while (dwEntries--)
  876. {
  877. if (!ConvertString(pCurRasEntryNameW->szEntryName,pCurRasEntryNameA->szEntryName
  878. ,sizeof(pCurRasEntryNameW->szEntryName)))
  879. {
  880. ++iFailCount;
  881. }
  882. ++pCurRasEntryNameW;
  883. ++pCurRasEntryNameA;
  884. }
  885. if (iFailCount)
  886. {
  887. Assert(0 == iFailCount);
  888. dwReturn = -1;
  889. }
  890. }
  891. if (pRasEntryNameA)
  892. {
  893. FREE(pRasEntryNameA);
  894. }
  895. }
  896. return dwReturn;
  897. }
  898. //+---------------------------------------------------------------------------
  899. //
  900. // Member: CNetApi::RasGetAutodial
  901. //
  902. // Synopsis: Gets the autodial state
  903. //
  904. // Arguments: [fEnabled] - Whether Ras autodial is enabled or disabled returned here
  905. //
  906. // History: 28-Jul-98 SitaramR Created
  907. //
  908. //----------------------------------------------------------------------------
  909. STDMETHODIMP CNetApi::RasGetAutodial( BOOL& fEnabled )
  910. {
  911. //
  912. // In case of failures the default is to assume that Ras autodial is enabled
  913. //
  914. fEnabled = TRUE;
  915. if (NOERROR != LoadRasApiDll())
  916. return NOERROR;
  917. if ( m_pRasGetAutodialParam == NULL )
  918. return NOERROR;
  919. DWORD dwValue;
  920. DWORD dwSize = sizeof(dwValue);
  921. DWORD dwRet = m_pRasGetAutodialParam( RASADP_LoginSessionDisable,
  922. &dwValue,
  923. &dwSize );
  924. if ( dwRet == ERROR_SUCCESS )
  925. {
  926. Assert( dwSize == sizeof(dwValue) );
  927. fEnabled = (dwValue == 0);
  928. }
  929. return NOERROR;
  930. }
  931. //+---------------------------------------------------------------------------
  932. //
  933. // Member: CNetApi::RasSetAutodial
  934. //
  935. // Synopsis: Sets the autodial state
  936. //
  937. // Arguments: [fEnabled] - Whether Ras is to be enabled or disabled
  938. //
  939. // History: 28-Jul-98 SitaramR Created
  940. //
  941. //----------------------------------------------------------------------------
  942. STDMETHODIMP CNetApi::RasSetAutodial( BOOL fEnabled )
  943. {
  944. //
  945. // Ignore failures
  946. //
  947. if (NOERROR != LoadRasApiDll())
  948. return NOERROR;
  949. if ( m_pRasGetAutodialParam == NULL )
  950. return NOERROR;
  951. DWORD dwValue = !fEnabled;
  952. DWORD dwRet = m_pRasSetAutodialParam( RASADP_LoginSessionDisable,
  953. &dwValue,
  954. sizeof(dwValue) );
  955. return NOERROR;
  956. }
  957. //+---------------------------------------------------------------------------
  958. //
  959. // Member: CNetApi::LoadRasApiDll, private
  960. //
  961. // Synopsis: If not already loaded, loads the RasApi Dll.
  962. //
  963. // Arguments:
  964. //
  965. // Returns: NOERROR if the dll was sucessfully loaded
  966. //
  967. // Modifies:
  968. //
  969. // History: 08-Dec-97 rogerg Created.
  970. //
  971. //----------------------------------------------------------------------------
  972. HRESULT CNetApi::LoadRasApiDll()
  973. {
  974. HRESULT hr = S_FALSE;;
  975. if (m_fTriedToLoadRas)
  976. {
  977. return m_hInstRasApiDll ? NOERROR : S_FALSE;
  978. }
  979. CLock lock(this);
  980. lock.Enter();
  981. if (!m_fTriedToLoadRas)
  982. {
  983. Assert(NULL == m_hInstRasApiDll);
  984. m_hInstRasApiDll = NULL;
  985. if (IsRasInstalled())
  986. {
  987. m_hInstRasApiDll = LoadLibrary(szRasDll);
  988. if (m_hInstRasApiDll)
  989. {
  990. m_pRasEnumConnectionsW = (RASENUMCONNECTIONSW)
  991. GetProcAddress(m_hInstRasApiDll, szRasEnumConnectionsW);
  992. m_pRasEnumConnectionsA = (RASENUMCONNECTIONSA)
  993. GetProcAddress(m_hInstRasApiDll, szRasEnumConnectionsA);
  994. m_pRasEnumEntriesA = (RASENUMENTRIESPROCA)
  995. GetProcAddress(m_hInstRasApiDll, szRasEnumEntriesA);
  996. m_pRasEnumEntriesW = (RASENUMENTRIESPROCW)
  997. GetProcAddress(m_hInstRasApiDll, szRasEnumEntriesW);
  998. m_pRasGetEntryPropertiesW = (RASGETENTRYPROPERTIESPROC)
  999. GetProcAddress(m_hInstRasApiDll, szRasGetEntryPropertiesW);
  1000. #if 0
  1001. m_pRasDial = (RASDIAL)
  1002. GetProcAddress(m_hInstRasApiDll, szRasDial);
  1003. m_pRasHangup = (RASHANGUP)
  1004. GetProcAddress(m_hInstRasApiDll, szRasHangup);
  1005. m_pRasEntryGetDialParams = (RASGETENTRYDIALPARAMSPROC)
  1006. GetProcAddress(m_hInstRasApiDll, szRasGetEntryDialParams);
  1007. m_pRasConnectStatus = (RASGETCONNECTSTATUSPROC)
  1008. GetProcAddress(m_hInstRasApiDll, szRasGetConnectStatus);
  1009. #endif // 0
  1010. m_pRasGetErrorStringW = (RASGETERRORSTRINGPROCW)
  1011. GetProcAddress(m_hInstRasApiDll, szRasGetErrorStringW);
  1012. m_pRasGetErrorStringA = (RASGETERRORSTRINGPROCA)
  1013. GetProcAddress(m_hInstRasApiDll, szRasGetErrorStringA);
  1014. m_pRasGetAutodialParam = (RASGETAUTODIALPARAM)
  1015. GetProcAddress(m_hInstRasApiDll, szRasGetAutodialParam);
  1016. m_pRasSetAutodialParam = (RASSETAUTODIALPARAM)
  1017. GetProcAddress(m_hInstRasApiDll, szRasSetAutodialParam);
  1018. }
  1019. }
  1020. //
  1021. // No check for Get/SetAutodialParam because they don't exist on Win 95
  1022. //
  1023. if (NULL == m_hInstRasApiDll
  1024. || NULL == m_hInstRasApiDll
  1025. || NULL == m_pRasEnumConnectionsA
  1026. || NULL == m_pRasGetErrorStringA
  1027. || NULL == m_pRasEnumEntriesA
  1028. #if 0
  1029. || NULL == m_pRasDial
  1030. || NULL == m_pRasConnectStatus
  1031. || NULL == m_pRasHangup
  1032. || NULL == m_pRasEntryGetDialParams
  1033. #endif // 0
  1034. )
  1035. {
  1036. if (m_hInstRasApiDll)
  1037. {
  1038. FreeLibrary(m_hInstRasApiDll);
  1039. m_hInstRasApiDll = NULL;
  1040. }
  1041. hr = S_FALSE;
  1042. }
  1043. else
  1044. {
  1045. hr = NOERROR;
  1046. }
  1047. m_fTriedToLoadRas = TRUE; // set after all init is done.
  1048. }
  1049. else
  1050. {
  1051. hr = m_hInstRasApiDll ? NOERROR : S_FALSE;
  1052. }
  1053. lock.Leave();
  1054. return hr;
  1055. }
  1056. //+---------------------------------------------------------------------------
  1057. //
  1058. // Member: CNetApi::LoadWinInetDll, private
  1059. //
  1060. // Synopsis: If not already loaded, loads the WinInet Dll.
  1061. //
  1062. // Arguments:
  1063. //
  1064. // Returns: NOERROR if the dll was sucessfully loaded
  1065. //
  1066. // Modifies:
  1067. //
  1068. // History: 26-May-98 rogerg Created.
  1069. //
  1070. //----------------------------------------------------------------------------
  1071. HRESULT CNetApi::LoadWinInetDll()
  1072. {
  1073. if (m_fTriedToLoadWinInet)
  1074. {
  1075. return m_hInstWinInetDll ? NOERROR : S_FALSE;
  1076. }
  1077. CLock lock(this);
  1078. lock.Enter();
  1079. HRESULT hr = NOERROR;
  1080. if (!m_fTriedToLoadWinInet)
  1081. {
  1082. Assert(NULL == m_hInstWinInetDll);
  1083. m_hInstWinInetDll = LoadLibrary(szWinInetDll);
  1084. if (m_hInstWinInetDll)
  1085. {
  1086. m_pInternetDial = (INTERNETDIAL) GetProcAddress(m_hInstWinInetDll, szInternetDial);
  1087. m_pInternetDialW = (INTERNETDIALW) GetProcAddress(m_hInstWinInetDll, szInternetDialW);
  1088. m_pInternetHangUp = (INTERNETHANGUP) GetProcAddress(m_hInstWinInetDll, szInternetHangup);
  1089. m_pInternetAutodial = (INTERNETAUTODIAL) GetProcAddress(m_hInstWinInetDll, szInternetAutodial);
  1090. m_pInternetAutodialHangup = (INTERNETAUTODIALHANGUP) GetProcAddress(m_hInstWinInetDll, szInternetAutodialHangup);
  1091. m_pInternetQueryOption = (INTERNETQUERYOPTION) GetProcAddress(m_hInstWinInetDll, szInternetQueryOption);
  1092. m_pInternetSetOption = (INTERNETSETOPTION) GetProcAddress(m_hInstWinInetDll, szInternetSetOption);
  1093. // note: not an error if can't get wide version of InternetDial
  1094. Assert(m_pInternetDial);
  1095. Assert(m_pInternetHangUp);
  1096. Assert(m_pInternetAutodial);
  1097. Assert(m_pInternetAutodialHangup);
  1098. Assert(m_pInternetQueryOption);
  1099. Assert(m_pInternetSetOption);
  1100. }
  1101. // note: don't bail if can't get wide version of InternetDial
  1102. if (NULL == m_hInstWinInetDll
  1103. || NULL == m_pInternetDial
  1104. || NULL == m_pInternetHangUp
  1105. || NULL == m_pInternetAutodial
  1106. || NULL == m_pInternetAutodialHangup
  1107. || NULL == m_pInternetQueryOption
  1108. || NULL == m_pInternetSetOption
  1109. )
  1110. {
  1111. if (m_hInstWinInetDll)
  1112. {
  1113. FreeLibrary(m_hInstWinInetDll);
  1114. m_hInstWinInetDll = NULL;
  1115. }
  1116. hr = S_FALSE;
  1117. }
  1118. else
  1119. {
  1120. hr = NOERROR;
  1121. }
  1122. m_fTriedToLoadWinInet = TRUE; // set after all init is done.
  1123. }
  1124. else
  1125. {
  1126. // someone took the lock before us, return the new resul
  1127. hr = m_hInstWinInetDll ? NOERROR : S_FALSE;
  1128. }
  1129. lock.Leave();
  1130. return hr;
  1131. }
  1132. //+---------------------------------------------------------------------------
  1133. //
  1134. // Member: CNetApi::InternetDial, private
  1135. //
  1136. // Synopsis: Calls the WinInet InternetDial API.
  1137. //
  1138. // Arguments:
  1139. //
  1140. // Returns: -1 can't load dll
  1141. // whatever API returns.
  1142. //
  1143. // Modifies:
  1144. //
  1145. // History: 26-May-98 rogerg Created.
  1146. //
  1147. //----------------------------------------------------------------------------
  1148. STDMETHODIMP_(DWORD) CNetApi::InternetDialA(HWND hwndParent,char* lpszConnectoid,DWORD dwFlags,
  1149. LPDWORD lpdwConnection, DWORD dwReserved)
  1150. {
  1151. DWORD dwRet = -1;
  1152. if (NOERROR == LoadWinInetDll())
  1153. {
  1154. dwRet = m_pInternetDial(hwndParent,lpszConnectoid,dwFlags,lpdwConnection,dwReserved);
  1155. }
  1156. return dwRet;
  1157. }
  1158. //+---------------------------------------------------------------------------
  1159. //
  1160. // Member: CNetApi::InternetDial, private
  1161. //
  1162. // Synopsis: Calls the WinInet InternetDial API.
  1163. //
  1164. // Arguments:
  1165. //
  1166. // Returns: -1 can't load dll
  1167. // whatever API returns.
  1168. //
  1169. // Modifies:
  1170. //
  1171. // History: 26-May-98 rogerg Created.
  1172. //
  1173. //----------------------------------------------------------------------------
  1174. STDMETHODIMP_(DWORD) CNetApi::InternetDialW(HWND hwndParent,WCHAR* lpszConnectoid,DWORD dwFlags,
  1175. LPDWORD lpdwConnection, DWORD dwReserved)
  1176. {
  1177. DWORD dwRet = -1;
  1178. if (NOERROR == LoadWinInetDll())
  1179. {
  1180. if (m_pInternetDialW)
  1181. {
  1182. dwRet = m_pInternetDialW(hwndParent,lpszConnectoid,dwFlags,lpdwConnection,dwReserved);
  1183. }
  1184. else
  1185. {
  1186. XArray<CHAR> xszConnectoid;
  1187. BOOL fOk = ConvertWideCharToMultiByte( lpszConnectoid, xszConnectoid );
  1188. if ( !fOk )
  1189. return dwRet;
  1190. dwRet = InternetDialA(hwndParent, xszConnectoid.Get(), dwFlags, lpdwConnection, dwReserved);
  1191. }
  1192. }
  1193. return dwRet;
  1194. }
  1195. //+---------------------------------------------------------------------------
  1196. //
  1197. // Member: CNetApi::InternetHangUp, private
  1198. //
  1199. // Synopsis: Calls the WinInet InternetHangUp API.
  1200. //
  1201. // Arguments:
  1202. //
  1203. // Returns: -1 can't load dll
  1204. // whatever API returns.
  1205. //
  1206. // Modifies:
  1207. //
  1208. // History: 26-May-98 rogerg Created.
  1209. //
  1210. //----------------------------------------------------------------------------
  1211. STDMETHODIMP_(DWORD) CNetApi::InternetHangUp(DWORD dwConnection,DWORD dwReserved)
  1212. {
  1213. DWORD dwRet = -1;
  1214. if (NOERROR == LoadWinInetDll())
  1215. {
  1216. dwRet = m_pInternetHangUp(dwConnection,dwReserved);
  1217. }
  1218. return dwRet;
  1219. }
  1220. //+---------------------------------------------------------------------------
  1221. //
  1222. // Member: CNetApi::InternetAutodial, private
  1223. //
  1224. // Synopsis: Calls the WinInet InternetAutodial API.
  1225. //
  1226. // Arguments:
  1227. //
  1228. // Returns: TRUE if connection was established.
  1229. //
  1230. // Modifies:
  1231. //
  1232. // History: 26-May-98 rogerg Created.
  1233. //
  1234. //----------------------------------------------------------------------------
  1235. STDMETHODIMP_(BOOL) WINAPI CNetApi::InternetAutodial(DWORD dwFlags,DWORD dwReserved)
  1236. {
  1237. BOOL fRet = FALSE;
  1238. if (NOERROR == LoadWinInetDll())
  1239. {
  1240. fRet = m_pInternetAutodial(dwFlags,dwReserved);
  1241. }
  1242. return fRet;
  1243. }
  1244. //+---------------------------------------------------------------------------
  1245. //
  1246. // Member: CNetApi::InternetAutodialHangup, private
  1247. //
  1248. // Synopsis: Calls the WinInet InternetAutodialHangup API.
  1249. //
  1250. // Arguments:
  1251. //
  1252. // Returns: TRUE if hangup was successful.
  1253. //
  1254. // Modifies:
  1255. //
  1256. // History: 26-May-98 rogerg Created.
  1257. //
  1258. //----------------------------------------------------------------------------
  1259. STDMETHODIMP_(BOOL) WINAPI CNetApi::InternetAutodialHangup(DWORD dwReserved)
  1260. {
  1261. BOOL fRet = FALSE;
  1262. if (NOERROR == LoadWinInetDll())
  1263. {
  1264. fRet = m_pInternetAutodialHangup(dwReserved);
  1265. }
  1266. return fRet;
  1267. }
  1268. //+---------------------------------------------------------------------------
  1269. //
  1270. // Member: CNetApi::InternetGetAutoDial
  1271. //
  1272. // Synopsis: Gets the wininet autodial state
  1273. //
  1274. // Arguments: [fDisabled] - Whether autodial is enabled or disabled
  1275. //
  1276. // History: 28-Jul-98 SitaramR Created
  1277. //
  1278. //----------------------------------------------------------------------------
  1279. STDMETHODIMP CNetApi::InternetGetAutodial( BOOL& fEnabled )
  1280. {
  1281. //
  1282. // In case of failures the default is to assume that Wininet autodial is enabled
  1283. //
  1284. fEnabled = TRUE;
  1285. if ( NOERROR == LoadWinInetDll() )
  1286. {
  1287. DWORD dwEnable = 1;
  1288. DWORD dwSize = sizeof(DWORD);
  1289. BOOL fOk = m_pInternetQueryOption(NULL, INTERNET_OPTION_DISABLE_AUTODIAL, &dwEnable, &dwSize);
  1290. if ( fOk )
  1291. {
  1292. //
  1293. // InternetQueryOption( .. AUTODIAL .. ) is available on IE 5 only
  1294. //
  1295. fEnabled = dwEnable;
  1296. return NOERROR;
  1297. }
  1298. }
  1299. //
  1300. // For IE < version 5, fall back to reading registry
  1301. //
  1302. HKEY hkIE;
  1303. LONG lr = RegOpenKeyExXp( HKEY_CURRENT_USER,
  1304. L"software\\microsoft\\windows\\currentversion\\Internet Settings",
  1305. NULL,KEY_READ,
  1306. &hkIE,FALSE /*fSetSecurity*/);
  1307. if ( lr != ERROR_SUCCESS )
  1308. return NOERROR;
  1309. DWORD dwType;
  1310. DWORD dwValue;
  1311. DWORD dwSize = sizeof(dwValue);
  1312. lr = RegQueryValueEx( hkIE,
  1313. L"EnableAutoDial",
  1314. NULL,
  1315. &dwType,
  1316. (BYTE *)&dwValue,
  1317. &dwSize );
  1318. RegCloseKey( hkIE );
  1319. if ( lr == ERROR_SUCCESS )
  1320. {
  1321. Assert( dwSize == sizeof(dwValue) && (dwType == REG_BINARY || dwType == REG_DWORD) );
  1322. fEnabled = (dwValue == 1);
  1323. }
  1324. return NOERROR;
  1325. }
  1326. //+---------------------------------------------------------------------------
  1327. //
  1328. // Member: CNetApi::InternetSetAutoDial
  1329. //
  1330. // Synopsis: Sets the wininet autodial state
  1331. //
  1332. // Arguments: [fEnabled] - Whether autodial is to be enabled or disabled
  1333. //
  1334. // History: 28-Jul-98 SitaramR Created
  1335. //
  1336. //----------------------------------------------------------------------------
  1337. STDMETHODIMP CNetApi::InternetSetAutodial( BOOL fEnabled )
  1338. {
  1339. //
  1340. // Ignore failures
  1341. //
  1342. if ( NOERROR == LoadWinInetDll() )
  1343. {
  1344. DWORD dwEnable = fEnabled;
  1345. BOOL fOk = m_pInternetSetOption(NULL, INTERNET_OPTION_DISABLE_AUTODIAL, &dwEnable, sizeof(DWORD));
  1346. if ( fOk )
  1347. {
  1348. //
  1349. // InternetSetOption( .. AUTODIAL .. ) is available on IE 5 only
  1350. //
  1351. return NOERROR;
  1352. }
  1353. }
  1354. //
  1355. // For IE < version 5, fall back to reading registry
  1356. //
  1357. HKEY hkIE;
  1358. LONG lr = RegOpenKeyExXp( HKEY_CURRENT_USER,
  1359. L"software\\microsoft\\windows\\currentversion\\Internet Settings",
  1360. NULL,KEY_READ | KEY_WRITE,
  1361. &hkIE,FALSE /*fSetSecurity*/ );
  1362. if ( lr != ERROR_SUCCESS )
  1363. return NOERROR;
  1364. DWORD dwValue = fEnabled;
  1365. lr = RegSetValueEx( hkIE,
  1366. L"EnableAutoDial",
  1367. NULL,
  1368. REG_BINARY,
  1369. (BYTE *)&dwValue,
  1370. sizeof(dwValue) );
  1371. RegCloseKey( hkIE );
  1372. return NOERROR;
  1373. }
  1374. //+-------------------------------------------------------------------
  1375. //
  1376. // Function: IsGlobalOffline
  1377. //
  1378. // Synopsis: Determines if in WorkOffline State
  1379. //
  1380. // Arguments:
  1381. //
  1382. // Notes: Code Provided by DarrenMi
  1383. //
  1384. //
  1385. // History:
  1386. //
  1387. //--------------------------------------------------------------------
  1388. STDMETHODIMP_(BOOL) CNetApi::IsGlobalOffline(void)
  1389. {
  1390. DWORD dwState = 0, dwSize = sizeof(DWORD);
  1391. BOOL fRet = FALSE;
  1392. LoadWinInetDll();
  1393. if (NULL == m_pInternetQueryOption)
  1394. {
  1395. Assert(m_pInternetQueryOption)
  1396. return FALSE; // USUAL NOT OFFLINE
  1397. }
  1398. if(m_pInternetQueryOption(NULL, INTERNET_OPTION_CONNECTED_STATE, &dwState,
  1399. &dwSize))
  1400. {
  1401. if(dwState & INTERNET_STATE_DISCONNECTED_BY_USER)
  1402. fRet = TRUE;
  1403. }
  1404. return fRet;
  1405. }
  1406. //+-------------------------------------------------------------------
  1407. //
  1408. // Function: SetOffline
  1409. //
  1410. // Synopsis: Sets the WorkOffline state to on or off.
  1411. //
  1412. // Arguments:
  1413. //
  1414. // Notes: Code Provided by DarrenMi
  1415. //
  1416. //
  1417. // History:
  1418. //
  1419. //--------------------------------------------------------------------
  1420. STDMETHODIMP_(BOOL) CNetApi::SetOffline(BOOL fOffline)
  1421. {
  1422. INTERNET_CONNECTED_INFO ci;
  1423. BOOL fReturn = FALSE;
  1424. LoadWinInetDll();
  1425. if (NULL == m_pInternetSetOption)
  1426. {
  1427. Assert(m_pInternetSetOption);
  1428. return FALSE;
  1429. }
  1430. memset(&ci, 0, sizeof(ci));
  1431. if(fOffline) {
  1432. ci.dwConnectedState = INTERNET_STATE_DISCONNECTED_BY_USER;
  1433. ci.dwFlags = ISO_FORCE_DISCONNECTED;
  1434. } else {
  1435. ci.dwConnectedState = INTERNET_STATE_CONNECTED;
  1436. }
  1437. fReturn = m_pInternetSetOption(NULL, INTERNET_OPTION_CONNECTED_STATE, &ci, sizeof(ci));
  1438. return fReturn;
  1439. }
  1440. //+-------------------------------------------------------------------
  1441. //
  1442. // Function: IsRasInstalled
  1443. //
  1444. // Synopsis: determine whether this machine has ras installed
  1445. //
  1446. // Arguments:
  1447. //
  1448. // Notes: Stole code from WinInent. Can call InternetGetConnectionEx
  1449. // to get this information but this is an IE 5.0 only function.
  1450. // If IE 5.0 wininet is available on all shipping platforms
  1451. // call the InternetGetConnectionEx funciton instead of this.
  1452. //
  1453. //
  1454. // History:
  1455. //
  1456. //--------------------------------------------------------------------
  1457. // from sdk\inc\inetreg.h
  1458. #ifdef _DONTINCLUDEINETREG
  1459. #define TSZMICROSOFTPATH TEXT("Software\\Microsoft")
  1460. #define TSZWINCURVERPATH TSZMICROSOFTPATH TEXT("\\windows\\CurrentVersion")
  1461. #define REGSTR_PATH_RNACOMPONENT TSZWINCURVERPATH TEXT("\\Setup\\OptionalComponents\\RNA")
  1462. #define REGSTR_VAL_RNAINSTALLED TEXT("Installed")
  1463. #endif // #ifdef _DONTINCLUDEINETREG
  1464. // from private\inet\wininet\dll\autodial.cxx
  1465. static const TCHAR szRegKeyRAS[] = TEXT("SOFTWARE\\Microsoft\\RAS");
  1466. STDMETHODIMP_(BOOL) IsRasInstalled(void)
  1467. {
  1468. BOOL fInstalled = FALSE;
  1469. OSVERSIONINFOA OSVersionInfo;
  1470. OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVersionInfo);
  1471. if (!GetVersionExA(&OSVersionInfo))
  1472. {
  1473. AssertSz(0,"Unable to GetOS Version");
  1474. return TRUE; // go ahead and try RAS anyway.
  1475. }
  1476. if(VER_PLATFORM_WIN32_WINDOWS == OSVersionInfo.dwPlatformId) {
  1477. //
  1478. // Check Win9x key
  1479. //
  1480. TCHAR szSmall[3]; // there should be a "1" or a "0" only
  1481. DWORD cb = 3 * sizeof(TCHAR);
  1482. HKEY hkey;
  1483. long lRes;
  1484. lRes = RegOpenKeyExXp(HKEY_LOCAL_MACHINE, REGSTR_PATH_RNACOMPONENT,
  1485. NULL, KEY_READ, &hkey,FALSE /*fSetSecurity*/);
  1486. if(ERROR_SUCCESS == lRes) {
  1487. // REGSTR_VAL_RNAINSTALLED is defined with TEXT() macro so
  1488. // if wininet is ever compiled unicode this will be a compile
  1489. // error.
  1490. lRes = RegQueryValueEx(hkey, REGSTR_VAL_RNAINSTALLED, NULL,
  1491. NULL, (LPBYTE)szSmall, &cb);
  1492. if(ERROR_SUCCESS == lRes) {
  1493. if((szSmall[0] == TEXT('1')) && (szSmall[1] == 0)) {
  1494. // 1 means ras installed
  1495. fInstalled = TRUE;
  1496. }
  1497. }
  1498. RegCloseKey(hkey);
  1499. }
  1500. } else {
  1501. if (OSVersionInfo.dwMajorVersion < 5)
  1502. {
  1503. //
  1504. // Check old NT key (5.x (and presumably later versions) always have RAS installed)
  1505. //
  1506. HKEY hKey;
  1507. long lerr;
  1508. lerr = RegOpenKeyExXp(HKEY_LOCAL_MACHINE, szRegKeyRAS, 0,
  1509. KEY_READ, &hKey,FALSE /*fSetSecurity*/);
  1510. if(ERROR_SUCCESS == lerr) {
  1511. // key exists - ras is installed
  1512. fInstalled = TRUE;
  1513. RegCloseKey(hKey);
  1514. }
  1515. } else {
  1516. // NT5 and later - always installed
  1517. fInstalled = TRUE;
  1518. }
  1519. }
  1520. return fInstalled;
  1521. }