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.

1450 lines
48 KiB

  1. // =====================================================================================
  2. // R A S A P I . C P P
  3. // =====================================================================================
  4. #include "pch.hxx"
  5. #include "rasapi.h"
  6. #include "connect.h"
  7. #include "resource.h"
  8. #include "error.h"
  9. #include "strconst.h"
  10. #include "xpcomm.h"
  11. #include "rasdlgsp.h"
  12. #include "goptions.h"
  13. // =====================================================================================
  14. // Ras Dial Function Pointers
  15. // =====================================================================================
  16. static CRITICAL_SECTION g_rCritSec;
  17. static HINSTANCE g_hInstRas=NULL;
  18. static HINSTANCE g_hInstRasDlg=NULL;
  19. static RASDIALPROC g_pRasDial=NULL;
  20. static RASENUMCONNECTIONSPROC g_pRasEnumConnections=NULL;
  21. static RASENUMENTRIESPROC g_pRasEnumEntries=NULL;
  22. static RASGETCONNECTSTATUSPROC g_pRasGetConnectStatus=NULL;
  23. static RASGETERRORSTRINGPROC g_pRasGetErrorString=NULL;
  24. static RASHANGUPPROC g_pRasHangup=NULL;
  25. static RASSETENTRYDIALPARAMSPROC g_pRasSetEntryDialParams=NULL;
  26. static RASGETENTRYDIALPARAMSPROC g_pRasGetEntryDialParams=NULL;
  27. static RASCREATEPHONEBOOKENTRYPROC g_pRasCreatePhonebookEntry=NULL;
  28. static RASEDITPHONEBOOKENTRYPROC g_pRasEditPhonebookEntry=NULL;
  29. static RASDIALDLGPROC g_pRasDialDlg=NULL;
  30. // =====================================================================================
  31. // Make our code look prettier
  32. // =====================================================================================
  33. #undef RasDial
  34. #undef RasEnumConnections
  35. #undef RasEnumEntries
  36. #undef RasGetConnectStatus
  37. #undef RasGetErrorString
  38. #undef RasHangup
  39. #undef RasSetEntryDialParams
  40. #undef RasGetEntryDialParams
  41. #undef RasCreatePhonebookEntry
  42. #undef RasEditPhonebookEntry
  43. #undef RasDialDlg
  44. #define RasDial (*g_pRasDial)
  45. #define RasEnumConnections (*g_pRasEnumConnections)
  46. #define RasEnumEntries (*g_pRasEnumEntries)
  47. #define RasGetConnectStatus (*g_pRasGetConnectStatus)
  48. #define RasGetErrorString (*g_pRasGetErrorString)
  49. #define RasHangup (*g_pRasHangup)
  50. #define RasSetEntryDialParams (*g_pRasSetEntryDialParams)
  51. #define RasGetEntryDialParams (*g_pRasGetEntryDialParams)
  52. #define RasCreatePhonebookEntry (*g_pRasCreatePhonebookEntry)
  53. #define RasEditPhonebookEntry (*g_pRasEditPhonebookEntry)
  54. #define RasDialDlg (*g_pRasDialDlg)
  55. #define DEF_HANGUP_WAIT 10 // Seconds
  56. static const TCHAR s_szRasDlgDll[] = "RASDLG.DLL";
  57. #ifdef UNICODE
  58. static const TCHAR s_szRasDialDlg[] = "RasDialDlgW";
  59. #else
  60. static const TCHAR s_szRasDialDlg[] = "RasDialDlgA";
  61. #endif
  62. // =====================================================================================
  63. // Cool little RAS Utilities
  64. // =====================================================================================
  65. HRESULT HrVerifyRasLoaded(VOID);
  66. BOOL FEnumerateConnections(LPRASCONN *ppRasConn, ULONG *pcConnections);
  67. BOOL FFindConnection(LPTSTR lpszEntry, LPHRASCONN phRasConn);
  68. BOOL FRasHangupAndWait(HRASCONN hRasConn, DWORD dwMaxWaitSeconds);
  69. HRESULT HrRasError(HWND hwnd, HRESULT hrRasError, DWORD dwRasDial);
  70. VOID CombinedRasError(HWND hwnd, UINT unids, LPTSTR pszRasError, DWORD dwRasError);
  71. extern BOOL FIsPlatformWinNT();
  72. // =====================================================================================
  73. // LpCreateRasObject
  74. // =====================================================================================
  75. CRas *LpCreateRasObject(VOID)
  76. {
  77. CRas *pRas = new CRas;
  78. return pRas;
  79. }
  80. // =====================================================================================
  81. // RasInit
  82. // =====================================================================================
  83. VOID RasInit(VOID)
  84. {
  85. InitializeCriticalSection(&g_rCritSec);
  86. }
  87. // =====================================================================================
  88. // RasDeinit
  89. // =====================================================================================
  90. VOID RasDeinit(VOID)
  91. {
  92. if(g_hInstRas)
  93. {
  94. EnterCriticalSection(&g_rCritSec);
  95. g_pRasEnumConnections=NULL;
  96. g_pRasEnumEntries=NULL;
  97. g_pRasGetConnectStatus=NULL;
  98. g_pRasGetErrorString=NULL;
  99. g_pRasHangup=NULL;
  100. g_pRasSetEntryDialParams=NULL;
  101. g_pRasGetEntryDialParams=NULL;
  102. g_pRasCreatePhonebookEntry=NULL;
  103. g_pRasEditPhonebookEntry=NULL;
  104. FreeLibrary(g_hInstRas);
  105. g_hInstRas=NULL;
  106. LeaveCriticalSection(&g_rCritSec);
  107. }
  108. if(g_hInstRasDlg)
  109. {
  110. EnterCriticalSection(&g_rCritSec);
  111. g_pRasDialDlg=NULL;
  112. FreeLibrary(g_hInstRasDlg);
  113. g_hInstRasDlg=NULL;
  114. LeaveCriticalSection(&g_rCritSec);
  115. }
  116. DeleteCriticalSection(&g_rCritSec);
  117. }
  118. // =====================================================================================
  119. // CRas::CRas
  120. // =====================================================================================
  121. CRas::CRas()
  122. {
  123. DOUT("CRas::CRas");
  124. m_cRef = 1;
  125. m_fIStartedRas = FALSE;
  126. m_iConnectType = 0;
  127. *m_szConnectName = _T('\0');
  128. *m_szCurrentConnectName = _T('\0');
  129. m_hRasConn = NULL;
  130. m_fForceHangup = FALSE;
  131. ZeroMemory(&m_rdp, sizeof(RASDIALPARAMS));
  132. m_fSavePassword = FALSE;
  133. m_fShutdown = FALSE;
  134. }
  135. // =====================================================================================
  136. // CRas::~CRas
  137. // =====================================================================================
  138. CRas::~CRas()
  139. {
  140. DOUT("CRas::~CRas");
  141. }
  142. // =====================================================================================
  143. // CRas::AddRef
  144. // =====================================================================================
  145. ULONG CRas::AddRef(VOID)
  146. {
  147. DOUT("CRas::AddRef %lx ==> %d", this, m_cRef+1);
  148. return ++m_cRef;
  149. }
  150. // =====================================================================================
  151. // CRas::Release
  152. // =====================================================================================
  153. ULONG CRas::Release(VOID)
  154. {
  155. DOUT("CRas::Release %lx ==> %d", this, m_cRef-1);
  156. if (--m_cRef == 0)
  157. {
  158. delete this;
  159. return 0;
  160. }
  161. return m_cRef;
  162. }
  163. // =====================================================================================
  164. // CRas::FUsingRAS
  165. // =====================================================================================
  166. BOOL CRas::FUsingRAS(VOID)
  167. {
  168. return m_iConnectType == iConnectRAS ? TRUE : FALSE;
  169. }
  170. // =====================================================================================
  171. // CRas::SetConnectInfo
  172. // =====================================================================================
  173. VOID CRas::SetConnectInfo(DWORD iConnectType, LPTSTR pszConnectName)
  174. {
  175. // Changing connection, drop current ?
  176. if (m_iConnectType == iConnectRAS && iConnectType != iConnectRAS)
  177. Disconnect(NULL, FALSE);
  178. // Save Connection Data
  179. StrCpyN (m_szConnectName, pszConnectName, RAS_MaxEntryName+1);
  180. m_iConnectType = iConnectType;
  181. // Not using RAS
  182. if (m_iConnectType != iConnectRAS)
  183. return;
  184. }
  185. // =====================================================================================
  186. // CRas::HrConnect
  187. // =====================================================================================
  188. HRESULT CRas::HrConnect(HWND hwnd)
  189. {
  190. // Locals
  191. HRESULT hr=S_OK;
  192. LPRASCONN pRasConn=NULL;
  193. ULONG cConnections;
  194. // Not using RAS
  195. if (m_iConnectType != iConnectRAS)
  196. goto exit;
  197. // Not inited
  198. hr = HrVerifyRasLoaded();
  199. if (FAILED(hr))
  200. {
  201. HrRasError(hwnd, hr, 0);
  202. TRAPHR(hr);
  203. goto exit;
  204. }
  205. // Get Current RAS Connection
  206. FEnumerateConnections(&pRasConn, &cConnections);
  207. // Connections ?
  208. if (cConnections)
  209. {
  210. m_hRasConn = pRasConn[0].hrasconn;
  211. StrCpyN(m_szCurrentConnectName, pRasConn[0].szEntryName, ARRAYSIZE(m_szCurrentConnectName));
  212. }
  213. else
  214. {
  215. m_fIStartedRas = FALSE;
  216. m_hRasConn = NULL;
  217. *m_szCurrentConnectName = _T('\0');
  218. }
  219. // If RAS Connection present, is it equal to suggested
  220. if (m_hRasConn)
  221. {
  222. // Current connection is what I want ?
  223. if (lstrcmpi(m_szCurrentConnectName, m_szConnectName) == 0)
  224. goto exit;
  225. // Otherwise, if we didn't start the RAS connection...
  226. else if (m_fIStartedRas == FALSE)
  227. {
  228. // Get option fo handling current connection
  229. UINT unAnswer = UnPromptCloseConn(hwnd);
  230. // Cancel ?
  231. if (IDCANCEL == unAnswer)
  232. {
  233. hr = TRAPHR(hrUserCancel);
  234. goto exit;
  235. }
  236. // Close Current ?
  237. else if (idrgDialNew == unAnswer)
  238. {
  239. m_fForceHangup = TRUE;
  240. Disconnect(NULL, FALSE);
  241. m_fForceHangup = FALSE;
  242. }
  243. // Otherwise, use current ?
  244. else if (idrgUseCurrent == unAnswer)
  245. goto exit;
  246. // Problems
  247. else
  248. Assert(FALSE);
  249. }
  250. // Otherwise, I started the connection, so close it
  251. else if (m_fIStartedRas == TRUE)
  252. Disconnect(NULL, FALSE);
  253. }
  254. // We probably shouldn't have a connection handle at this point
  255. Assert(m_hRasConn == NULL);
  256. // Dial the connection
  257. CHECKHR(hr = HrStartRasDial(hwnd));
  258. // If Synchronous -- Woo - hoo were connected and we started the connection
  259. m_fIStartedRas = TRUE;
  260. StrCpyN(m_szCurrentConnectName, m_szConnectName, ARRAYSIZE(m_szCurrentConnectName));
  261. exit:
  262. // Cleanup
  263. SafeMemFree(pRasConn);
  264. // Done
  265. return hr;
  266. }
  267. // =====================================================================================
  268. // CRas::UnPromptCloseConn
  269. // =====================================================================================
  270. UINT CRas::UnPromptCloseConn(HWND hwnd)
  271. {
  272. return DialogBoxParam(g_hLocRes, MAKEINTRESOURCE (iddRasCloseConn), hwnd, RasCloseConnDlgProc, (LPARAM)this);
  273. }
  274. // =====================================================================================
  275. // CRas::RasCloseConnDlgProc
  276. // =====================================================================================
  277. INT_PTR CALLBACK CRas::RasCloseConnDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  278. {
  279. // Locals
  280. CRas *pRas=NULL;
  281. TCHAR szRes[255],
  282. szMsg[255+RAS_MaxEntryName+1];
  283. switch(uMsg)
  284. {
  285. case WM_INITDIALOG:
  286. // Get lparam
  287. pRas = (CRas *)lParam;
  288. if (!pRas)
  289. {
  290. Assert (FALSE);
  291. EndDialog(hwnd, E_FAIL);
  292. return 1;
  293. }
  294. // Center
  295. CenterDialog(hwnd);
  296. // Set Text
  297. GetWindowText(GetDlgItem(hwnd, idcCurrentMsg), szRes, sizeof(szRes)/sizeof(TCHAR));
  298. wnsprintf(szMsg, ARRAYSIZE(szMsg),szRes, pRas->m_szCurrentConnectName);
  299. SetWindowText(GetDlgItem(hwnd, idcCurrentMsg), szMsg);
  300. // Set control
  301. GetWindowText(GetDlgItem(hwnd, idrgDialNew), szRes, sizeof(szRes)/sizeof(TCHAR));
  302. wnsprintf(szMsg, ARRAYSIZE(szMsg),szRes, pRas->m_szConnectName);
  303. SetWindowText(GetDlgItem(hwnd, idrgDialNew), szMsg);
  304. // Set Default
  305. CheckDlgButton(hwnd, idrgDialNew, TRUE);
  306. return 1;
  307. case WM_COMMAND:
  308. switch(GET_WM_COMMAND_ID(wParam,lParam))
  309. {
  310. case IDOK:
  311. EndDialog(hwnd, IsDlgButtonChecked(hwnd, idrgDialNew) ? idrgDialNew : idrgUseCurrent);
  312. return 1;
  313. case IDCANCEL:
  314. EndDialog(hwnd, IDCANCEL);
  315. return 1;
  316. }
  317. }
  318. return 0;
  319. }
  320. // =====================================================================================
  321. // CRas::HrRasLogon
  322. // =====================================================================================
  323. HRESULT CRas::HrRasLogon(HWND hwnd, BOOL fForcePrompt)
  324. {
  325. // Locals
  326. HRESULT hr=S_OK;
  327. DWORD dwRasError;
  328. // Do we need to prompt for logon information first ?
  329. ZeroMemory(&m_rdp, sizeof(RASDIALPARAMS));
  330. m_rdp.dwSize = sizeof(RASDIALPARAMS);
  331. StrCpyN(m_rdp.szEntryName, m_szConnectName, ARRAYSIZE(m_rdp.szEntryName));
  332. // Get params
  333. dwRasError = RasGetEntryDialParams(NULL, &m_rdp, &m_fSavePassword);
  334. if (dwRasError)
  335. {
  336. HrRasError(hwnd, hrGetDialParamsFailed, dwRasError);
  337. hr = TRAPHR(hrGetDialParamsFailed);
  338. goto exit;
  339. }
  340. if (g_pRasDialDlg)
  341. {
  342. // RasDialDlg will take it from here
  343. goto exit;
  344. }
  345. // Do we need to get password / account information
  346. if (fForcePrompt ||
  347. m_fSavePassword == FALSE ||
  348. FIsStringEmpty(m_rdp.szUserName) ||
  349. FIsStringEmpty(m_rdp.szPassword))
  350. {
  351. // RAS Logon
  352. hr = DialogBoxParam (g_hLocRes, MAKEINTRESOURCE (iddRasLogon), hwnd, RasLogonDlgProc, (LPARAM)this);
  353. if (hr == hrUserCancel)
  354. {
  355. HrRasError(hwnd, hrUserCancel, 0);
  356. hr = hrUserCancel;
  357. goto exit;
  358. }
  359. }
  360. exit:
  361. // Done
  362. return hr;
  363. }
  364. // =====================================================================================
  365. // CRas::RasLogonDlgProc
  366. // =====================================================================================
  367. INT_PTR CALLBACK CRas::RasLogonDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  368. {
  369. // Locals
  370. TCHAR sz[255],
  371. szText[255+RAS_MaxEntryName+1];
  372. CRas *pRas = (CRas *)GetWndThisPtr(hwnd);
  373. DWORD dwRasError;
  374. switch (uMsg)
  375. {
  376. case WM_INITDIALOG:
  377. // Get lparam
  378. pRas = (CRas *)lParam;
  379. if (!pRas)
  380. {
  381. Assert (FALSE);
  382. EndDialog(hwnd, E_FAIL);
  383. return 1;
  384. }
  385. // Center the window
  386. CenterDialog (hwnd);
  387. // Get Window Title
  388. GetWindowText(hwnd, sz, sizeof(sz));
  389. wnsprintf(szText, ARRAYSIZE(szText),sz, pRas->m_szConnectName);
  390. SetWindowText(hwnd, szText);
  391. // Word Default
  392. AthLoadString(idsDefault, sz, sizeof(sz));
  393. // Set Fields
  394. Edit_LimitText(GetDlgItem(hwnd, ideUserName), UNLEN);
  395. Edit_LimitText(GetDlgItem(hwnd, idePassword), PWLEN);
  396. //Edit_LimitText(GetDlgItem(hwnd, ideDomain), DNLEN);
  397. Edit_LimitText(GetDlgItem(hwnd, idePhone), RAS_MaxPhoneNumber);
  398. SetDlgItemText(hwnd, ideUserName, pRas->m_rdp.szUserName);
  399. SetDlgItemText(hwnd, idePassword, pRas->m_rdp.szPassword);
  400. /*
  401. if (FIsStringEmpty(pRas->m_rdp.szDomain))
  402. SetDlgItemText(hwnd, ideDomain, sz);
  403. else
  404. SetDlgItemText(hwnd, ideDomain, pRas->m_rdp.szDomain);
  405. */
  406. if (FIsStringEmpty(pRas->m_rdp.szPhoneNumber))
  407. SetDlgItemText(hwnd, idePhone, sz);
  408. else
  409. SetDlgItemText(hwnd, idePhone, pRas->m_rdp.szPhoneNumber);
  410. CheckDlgButton(hwnd, idchSavePassword, pRas->m_fSavePassword);
  411. // Save pRas
  412. SetWndThisPtr (hwnd, pRas);
  413. return 1;
  414. case WM_COMMAND:
  415. switch(GET_WM_COMMAND_ID(wParam,lParam))
  416. {
  417. case idbEditConnection:
  418. EditPhonebookEntry(hwnd, pRas->m_szConnectName);
  419. return 1;
  420. case IDCANCEL:
  421. EndDialog(hwnd, hrUserCancel);
  422. return 1;
  423. case IDOK:
  424. AthLoadString(idsDefault, sz, sizeof(sz));
  425. // Set Fields
  426. GetDlgItemText(hwnd, ideUserName, pRas->m_rdp.szUserName, UNLEN+1);
  427. GetDlgItemText(hwnd, idePassword, pRas->m_rdp.szPassword, PWLEN+1);
  428. /*
  429. GetDlgItemText(hwnd, ideDomain, pRas->m_rdp.szDomain, DNLEN+1);
  430. if (lstrcmp(pRas->m_rdp.szDomain, sz) == 0)
  431. *pRas->m_rdp.szDomain = _T('\0');
  432. */
  433. GetDlgItemText(hwnd, idePhone, pRas->m_rdp.szPhoneNumber, RAS_MaxPhoneNumber+1);
  434. if (lstrcmp(pRas->m_rdp.szPhoneNumber, sz) == 0)
  435. *pRas->m_rdp.szPhoneNumber = _T('\0');
  436. pRas->m_fSavePassword = IsDlgButtonChecked(hwnd, idchSavePassword);
  437. // Save Dial Parameters
  438. dwRasError = RasSetEntryDialParams(NULL, &pRas->m_rdp, !pRas->m_fSavePassword);
  439. if (dwRasError)
  440. {
  441. HrRasError(hwnd, hrSetDialParamsFailed, dwRasError);
  442. return 1;
  443. }
  444. EndDialog(hwnd, S_OK);
  445. return 1;
  446. }
  447. break;
  448. case WM_DESTROY:
  449. SetWndThisPtr (hwnd, NULL);
  450. break;
  451. }
  452. return 0;
  453. }
  454. // =====================================================================================
  455. // CRas::HrShowRasDialError
  456. // =====================================================================================
  457. HRESULT CRas::HrStartRasDial(HWND hwndParent)
  458. {
  459. // Locals
  460. HRESULT hr=S_OK;
  461. // Logon first ?
  462. CHECKHR (hr = HrRasLogon(hwndParent, FALSE));
  463. if (g_pRasDialDlg)
  464. {
  465. RASDIALDLG rdd = {0};
  466. BOOL fRet;
  467. rdd.dwSize = sizeof(rdd);
  468. rdd.hwndOwner = hwndParent;
  469. #if (WINVER >= 0x401)
  470. rdd.dwSubEntry = m_rdp.dwSubEntry;
  471. #else
  472. rdd.dwSubEntry = 0;
  473. #endif
  474. fRet = RasDialDlg(NULL, m_rdp.szEntryName, lstrlen(m_rdp.szPhoneNumber) ? m_rdp.szPhoneNumber : NULL, &rdd);
  475. if (!fRet)
  476. {
  477. hr = E_FAIL;
  478. }
  479. }
  480. else
  481. {
  482. // Done
  483. hr = DialogBoxParam (g_hLocRes, MAKEINTRESOURCE (iddRasProgress), hwndParent, RasProgressDlgProc, (LPARAM)this);
  484. }
  485. exit:
  486. // Done
  487. return hr;
  488. }
  489. // =====================================================================================
  490. // CRas::RasProgressDlgProc
  491. // =====================================================================================
  492. INT_PTR CALLBACK CRas::RasProgressDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  493. {
  494. // Locals
  495. TCHAR szText[255+RAS_MaxEntryName+1],
  496. sz[255];
  497. CRas *pRas = (CRas *)GetWndThisPtr(hwnd);
  498. static TCHAR s_szCancel[40];
  499. static UINT s_unRasEventMsg=0;
  500. static BOOL s_fDetails=FALSE;
  501. static RECT s_rcDialog;
  502. static BOOL s_fAuthStarted=FALSE;
  503. DWORD dwRasError,
  504. cyDetails;
  505. RASCONNSTATUS rcs;
  506. RECT rcDetails,
  507. rcDlg;
  508. switch (uMsg)
  509. {
  510. case WM_INITDIALOG:
  511. // Get lparam
  512. pRas = (CRas *)lParam;
  513. if (!pRas)
  514. {
  515. Assert (FALSE);
  516. EndDialog(hwnd, E_FAIL);
  517. return 1;
  518. }
  519. // Save this pointer
  520. SetWndThisPtr (hwnd, pRas);
  521. // Save Original Size of the dialog
  522. GetWindowRect (hwnd, &s_rcDialog);
  523. // Details enabled
  524. s_fDetails = DwGetOption(OPT_RASCONNDETAILS);
  525. // Hide details drop down
  526. if (s_fDetails == FALSE)
  527. {
  528. // Hid
  529. GetWindowRect (GetDlgItem (hwnd, idcSplitter), &rcDetails);
  530. // Height of details
  531. cyDetails = s_rcDialog.bottom - rcDetails.top;
  532. // Re-size
  533. MoveWindow (hwnd, s_rcDialog.left,
  534. s_rcDialog.top,
  535. s_rcDialog.right - s_rcDialog.left,
  536. s_rcDialog.bottom - s_rcDialog.top - cyDetails - 1,
  537. FALSE);
  538. }
  539. else
  540. {
  541. AthLoadString (idsHideDetails, sz, sizeof (sz));
  542. SetWindowText (GetDlgItem (hwnd, idbDet), sz);
  543. }
  544. // Get registered RAS event message id
  545. s_unRasEventMsg = RegisterWindowMessageA(RASDIALEVENT);
  546. if (s_unRasEventMsg == 0)
  547. s_unRasEventMsg = WM_RASDIALEVENT;
  548. // Center the window
  549. CenterDialog (hwnd);
  550. // Get Window Title
  551. GetWindowText(hwnd, sz, sizeof(sz));
  552. wnsprintf(szText, ARRAYSIZE(szText),sz, pRas->m_szConnectName);
  553. SetWindowText(hwnd, szText);
  554. // Dialog Xxxxxxx.....
  555. AthLoadString(idsRas_Dialing, sz, sizeof(sz)/sizeof(TCHAR));
  556. wnsprintf(szText, ARRAYSIZE(szText),sz, pRas->m_rdp.szPhoneNumber);
  557. SetWindowText(GetDlgItem(hwnd, ideProgress), szText);
  558. // Get Cancel Text
  559. GetWindowText(GetDlgItem(hwnd, IDCANCEL), s_szCancel, sizeof(s_szCancel));
  560. // Give the list box and hscroll
  561. SendMessage(GetDlgItem(hwnd, idlbDetails), LB_SETHORIZONTALEXTENT, 600, 0);
  562. // Dial the connection
  563. pRas->m_hRasConn = NULL;
  564. dwRasError = RasDial(NULL, NULL, &pRas->m_rdp, 0xFFFFFFFF, hwnd, &pRas->m_hRasConn);
  565. if (dwRasError)
  566. {
  567. pRas->FailedRasDial(hwnd, hrRasDialFailure, dwRasError);
  568. if (!pRas->FLogonRetry(hwnd, s_szCancel))
  569. {
  570. SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL,IDCANCEL), NULL);
  571. return 1;
  572. }
  573. }
  574. return 1;
  575. case WM_COMMAND:
  576. switch(GET_WM_COMMAND_ID(wParam,lParam))
  577. {
  578. case IDCANCEL:
  579. SetDwOption(OPT_RASCONNDETAILS, s_fDetails);
  580. EnableWindow(GetDlgItem(hwnd, IDCANCEL), FALSE);
  581. if (pRas)
  582. pRas->FailedRasDial(hwnd, hrUserCancel, 0);
  583. EndDialog(hwnd, hrUserCancel);
  584. return 1;
  585. case idbDet:
  586. // Get current location of the dialog
  587. GetWindowRect (hwnd, &rcDlg);
  588. // If currently hidden
  589. if (s_fDetails == FALSE)
  590. {
  591. // Re-size
  592. MoveWindow (hwnd, rcDlg.left,
  593. rcDlg.top,
  594. s_rcDialog.right - s_rcDialog.left,
  595. s_rcDialog.bottom - s_rcDialog.top,
  596. TRUE);
  597. AthLoadString (idsHideDetails, sz, sizeof (sz));
  598. SetWindowText (GetDlgItem (hwnd, idbDet), sz);
  599. s_fDetails = TRUE;
  600. }
  601. else
  602. {
  603. // Size of details
  604. GetWindowRect (GetDlgItem (hwnd, idcSplitter), &rcDetails);
  605. cyDetails = rcDlg.bottom - rcDetails.top;
  606. MoveWindow (hwnd, rcDlg.left,
  607. rcDlg.top,
  608. s_rcDialog.right - s_rcDialog.left,
  609. s_rcDialog.bottom - s_rcDialog.top - cyDetails - 1,
  610. TRUE);
  611. AthLoadString (idsShowDetails, sz, sizeof (sz));
  612. SetWindowText (GetDlgItem (hwnd, idbDet), sz);
  613. s_fDetails = FALSE;
  614. }
  615. break;
  616. }
  617. break;
  618. case WM_DESTROY:
  619. SetWndThisPtr (hwnd, NULL);
  620. break;
  621. default:
  622. if (!pRas)
  623. break;
  624. if (uMsg == s_unRasEventMsg)
  625. {
  626. HWND hwndLB = GetDlgItem(hwnd, idlbDetails);
  627. // Error ?
  628. if (lParam)
  629. {
  630. // Disconnected
  631. AthLoadString(idsRASCS_Disconnected, sz, sizeof(sz)/sizeof(TCHAR));
  632. ListBox_AddString(hwndLB, sz);
  633. // Log Error
  634. TCHAR szRasError[512];
  635. if (RasGetErrorString(lParam, szRasError, sizeof(szRasError)) == 0)
  636. {
  637. TCHAR szError[512 + 255];
  638. AthLoadString(idsErrorText, sz, sizeof(sz));
  639. wnsprintf(szError, ARRAYSIZE(szError),"%s %d: %s", sz, lParam, szRasError);
  640. ListBox_AddString(hwndLB, szError);
  641. }
  642. // Select last item
  643. SendMessage(hwndLB, LB_SETCURSEL, ListBox_GetCount(hwndLB)-1, 0);
  644. // Show Error
  645. pRas->FailedRasDial(hwnd, hrRasDialFailure, lParam);
  646. // Re logon
  647. if (!pRas->FLogonRetry(hwnd, s_szCancel))
  648. {
  649. SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL,IDCANCEL), NULL);
  650. return 1;
  651. }
  652. }
  653. // Otherwise, process RAS event
  654. else
  655. {
  656. switch(wParam)
  657. {
  658. case RASCS_OpenPort:
  659. AthLoadString(idsRASCS_OpenPort, sz, sizeof(sz)/sizeof(TCHAR));
  660. ListBox_AddString(hwndLB, sz);
  661. break;
  662. case RASCS_PortOpened:
  663. AthLoadString(idsRASCS_PortOpened, sz, sizeof(sz)/sizeof(TCHAR));
  664. ListBox_AddString(hwndLB, sz);
  665. break;
  666. case RASCS_ConnectDevice:
  667. rcs.dwSize = sizeof(RASCONNSTATUS);
  668. if (pRas->m_hRasConn && RasGetConnectStatus(pRas->m_hRasConn, &rcs) == 0)
  669. {
  670. AthLoadString(idsRASCS_ConnectDevice, sz, sizeof(sz)/sizeof(TCHAR));
  671. wnsprintf(szText, ARRAYSIZE(szText),sz, rcs.szDeviceName, rcs.szDeviceType);
  672. ListBox_AddString(hwndLB, szText);
  673. }
  674. break;
  675. case RASCS_DeviceConnected:
  676. rcs.dwSize = sizeof(RASCONNSTATUS);
  677. if (pRas->m_hRasConn && RasGetConnectStatus(pRas->m_hRasConn, &rcs) == 0)
  678. {
  679. AthLoadString(idsRASCS_DeviceConnected, sz, sizeof(sz)/sizeof(TCHAR));
  680. wnsprintf(szText, ARRAYSIZE(szText),sz, rcs.szDeviceName, rcs.szDeviceType);
  681. ListBox_AddString(hwndLB, szText);
  682. }
  683. break;
  684. case RASCS_AllDevicesConnected:
  685. AthLoadString(idsRASCS_AllDevicesConnected, sz, sizeof(sz)/sizeof(TCHAR));
  686. ListBox_AddString(hwndLB, sz);
  687. break;
  688. case RASCS_Authenticate:
  689. if (s_fAuthStarted == FALSE)
  690. {
  691. AthLoadString(idsRas_Authentication, sz, sizeof(sz)/sizeof(TCHAR));
  692. SetWindowText(GetDlgItem(hwnd, ideProgress), sz);
  693. ListBox_AddString(hwndLB, sz);
  694. s_fAuthStarted = TRUE;
  695. }
  696. break;
  697. case RASCS_AuthNotify:
  698. rcs.dwSize = sizeof(RASCONNSTATUS);
  699. if (pRas->m_hRasConn && RasGetConnectStatus(pRas->m_hRasConn, &rcs) == 0)
  700. {
  701. AthLoadString(idsRASCS_AuthNotify, sz, sizeof(sz)/sizeof(TCHAR));
  702. wnsprintf(szText, ARRAYSIZE(szText),sz, rcs.dwError);
  703. ListBox_AddString(hwndLB, szText);
  704. if (rcs.dwError)
  705. {
  706. pRas->FailedRasDial(hwnd, hrRasDialFailure, rcs.dwError);
  707. if (!pRas->FLogonRetry(hwnd, s_szCancel))
  708. {
  709. SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL,IDCANCEL), NULL);
  710. return 1;
  711. }
  712. }
  713. }
  714. break;
  715. case RASCS_AuthRetry:
  716. AthLoadString(idsRASCS_AuthRetry, sz, sizeof(sz)/sizeof(TCHAR));
  717. ListBox_AddString(hwndLB, sz);
  718. break;
  719. case RASCS_AuthCallback:
  720. AthLoadString(idsRASCS_AuthCallback, sz, sizeof(sz)/sizeof(TCHAR));
  721. ListBox_AddString(hwndLB, sz);
  722. break;
  723. case RASCS_AuthChangePassword:
  724. AthLoadString(idsRASCS_AuthChangePassword, sz, sizeof(sz)/sizeof(TCHAR));
  725. ListBox_AddString(hwndLB, sz);
  726. break;
  727. case RASCS_AuthProject:
  728. AthLoadString(idsRASCS_AuthProject, sz, sizeof(sz)/sizeof(TCHAR));
  729. ListBox_AddString(hwndLB, sz);
  730. break;
  731. case RASCS_AuthLinkSpeed:
  732. AthLoadString(idsRASCS_AuthLinkSpeed, sz, sizeof(sz)/sizeof(TCHAR));
  733. ListBox_AddString(hwndLB, sz);
  734. break;
  735. case RASCS_AuthAck:
  736. AthLoadString(idsRASCS_AuthAck, sz, sizeof(sz)/sizeof(TCHAR));
  737. ListBox_AddString(hwndLB, sz);
  738. break;
  739. case RASCS_ReAuthenticate:
  740. AthLoadString(idsRas_Authenticated, sz, sizeof(sz)/sizeof(TCHAR));
  741. SetWindowText(GetDlgItem(hwnd, ideProgress), sz);
  742. AthLoadString(idsRASCS_Authenticated, sz, sizeof(sz)/sizeof(TCHAR));
  743. ListBox_AddString(hwndLB, sz);
  744. break;
  745. case RASCS_PrepareForCallback:
  746. AthLoadString(idsRASCS_PrepareForCallback, sz, sizeof(sz)/sizeof(TCHAR));
  747. ListBox_AddString(hwndLB, sz);
  748. break;
  749. case RASCS_WaitForModemReset:
  750. AthLoadString(idsRASCS_WaitForModemReset, sz, sizeof(sz)/sizeof(TCHAR));
  751. ListBox_AddString(hwndLB, sz);
  752. break;
  753. case RASCS_WaitForCallback:
  754. AthLoadString(idsRASCS_WaitForCallback, sz, sizeof(sz)/sizeof(TCHAR));
  755. ListBox_AddString(hwndLB, sz);
  756. break;
  757. case RASCS_Projected:
  758. AthLoadString(idsRASCS_Projected, sz, sizeof(sz)/sizeof(TCHAR));
  759. ListBox_AddString(hwndLB, sz);
  760. break;
  761. case RASCS_Disconnected:
  762. AthLoadString(idsRASCS_Disconnected, sz, sizeof(sz)/sizeof(TCHAR));
  763. SetWindowText(GetDlgItem(hwnd, ideProgress), sz);
  764. ListBox_AddString(hwndLB, sz);
  765. pRas->FailedRasDial(hwnd, hrRasDialFailure, 0);
  766. if (!pRas->FLogonRetry(hwnd, s_szCancel))
  767. {
  768. SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL,IDCANCEL), NULL);
  769. return 1;
  770. }
  771. break;
  772. case RASCS_Connected:
  773. SetDwOption(OPT_RASCONNDETAILS, s_fDetails);
  774. AthLoadString(idsRASCS_Connected, sz, sizeof(sz)/sizeof(TCHAR));
  775. SetWindowText(GetDlgItem(hwnd, ideProgress), sz);
  776. ListBox_AddString(hwndLB, sz);
  777. EndDialog(hwnd, S_OK);
  778. break;
  779. }
  780. // Select last lb item
  781. SendMessage(hwndLB, LB_SETCURSEL, ListBox_GetCount(hwndLB)-1, 0);
  782. }
  783. return 1;
  784. }
  785. break;
  786. }
  787. // Done
  788. return 0;
  789. }
  790. // =====================================================================================
  791. // CRas::FLogonRetry
  792. // =====================================================================================
  793. BOOL CRas::FLogonRetry(HWND hwnd, LPTSTR pszCancel)
  794. {
  795. // Locals
  796. DWORD dwRasError;
  797. // Reset Cancel button
  798. SetWindowText(GetDlgItem(hwnd, IDCANCEL), pszCancel);
  799. // Empty the listbox
  800. ListBox_ResetContent(GetDlgItem(hwnd, idlbDetails));
  801. while(1)
  802. {
  803. // If failed...
  804. if (FAILED(HrRasLogon(hwnd, TRUE)))
  805. return FALSE;
  806. // Dial the connection
  807. m_hRasConn = NULL;
  808. dwRasError = RasDial(NULL, NULL, &m_rdp, 0xFFFFFFFF, hwnd, &m_hRasConn);
  809. if (dwRasError)
  810. {
  811. FailedRasDial(hwnd, hrRasDialFailure, dwRasError);
  812. continue;
  813. }
  814. // Success
  815. break;
  816. }
  817. // Done
  818. return TRUE;
  819. }
  820. // =====================================================================================
  821. // CRas::FailedRasDial
  822. // =====================================================================================
  823. VOID CRas::FailedRasDial(HWND hwnd, HRESULT hrRasError, DWORD dwRasError)
  824. {
  825. // Locals
  826. TCHAR sz[255];
  827. // Hangup the connection
  828. if (m_hRasConn)
  829. FRasHangupAndWait(m_hRasConn, DEF_HANGUP_WAIT);
  830. // Disconnected
  831. AthLoadString(idsRASCS_Disconnected, sz, sizeof(sz)/sizeof(TCHAR));
  832. SetWindowText(GetDlgItem(hwnd, ideProgress), sz);
  833. // Save dwRasError
  834. HrRasError(hwnd, hrRasError, dwRasError);
  835. // NULL it
  836. m_hRasConn = NULL;
  837. // Change dialog button to OK
  838. AthLoadString(idsOK, sz, sizeof(sz)/sizeof(TCHAR));
  839. SetWindowText(GetDlgItem(hwnd, IDCANCEL), sz);
  840. }
  841. // =====================================================================================
  842. // CRas::Disconnect
  843. // =====================================================================================
  844. VOID CRas::Disconnect(HWND hwnd, BOOL fShutdown)
  845. {
  846. // If not using RAS, who give a crap
  847. if (m_iConnectType != iConnectRAS)
  848. {
  849. Assert(m_hRasConn == NULL);
  850. Assert(m_fIStartedRas == FALSE);
  851. goto exit;
  852. }
  853. // Do we have a RAS connection
  854. if (m_hRasConn && (m_fIStartedRas || m_fForceHangup))
  855. {
  856. // Locals
  857. TCHAR szRes[255];
  858. TCHAR szMsg[255];
  859. INT nAnswer=IDYES;
  860. // If Shutdown, lets prompt the user
  861. if (fShutdown)
  862. {
  863. // Remember were shuting down
  864. m_fShutdown = TRUE;
  865. // Prompt
  866. AthLoadString(idsRasPromptDisconnect, szRes, sizeof(szRes)/sizeof(TCHAR));
  867. wnsprintf(szMsg, ARRAYSIZE(szMsg),szRes, m_szCurrentConnectName);
  868. // Prompt shutdown ?
  869. nAnswer = AthMessageBox(hwnd, MAKEINTRESOURCE(idsAthena), szMsg, 0, MB_YESNO | MB_ICONEXCLAMATION );
  870. }
  871. else
  872. AssertSz(m_fShutdown == FALSE, "Disconnect better not have been called with fShutdown = TRUE, and then FALSE");
  873. // Hangup
  874. if (nAnswer == IDYES)
  875. {
  876. FRasHangupAndWait(m_hRasConn, DEF_HANGUP_WAIT);
  877. m_hRasConn = NULL;
  878. m_fIStartedRas = FALSE;
  879. *m_szCurrentConnectName = _T('\0');
  880. }
  881. }
  882. // Otherwise, reset state
  883. else
  884. {
  885. // Leave current connection informtaion
  886. m_hRasConn = NULL;
  887. m_fIStartedRas = FALSE;
  888. }
  889. exit:
  890. // Done
  891. return;
  892. }
  893. // ****************************************************************************************
  894. // Simple RAS Utility Functions
  895. // ****************************************************************************************
  896. // =====================================================================================
  897. // HrVerifyRasLoaded
  898. // =====================================================================================
  899. HRESULT HrVerifyRasLoaded(VOID)
  900. {
  901. // Locals
  902. UINT uOldErrorMode;
  903. // Protected
  904. EnterCriticalSection(&g_rCritSec);
  905. // If dll is loaded, lets verify all of my function pointers
  906. if (!g_hInstRas)
  907. {
  908. // Try loading Ras.
  909. uOldErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  910. g_hInstRas = LoadLibrary(szRasDll);
  911. SetErrorMode(uOldErrorMode);
  912. // Failure ?
  913. if (!g_hInstRas)
  914. goto failure;
  915. // Did we load it
  916. g_pRasDial = (RASDIALPROC)GetProcAddress(g_hInstRas, szRasDial);
  917. g_pRasEnumConnections = (RASENUMCONNECTIONSPROC)GetProcAddress(g_hInstRas, szRasEnumConnections);
  918. g_pRasEnumEntries = (RASENUMENTRIESPROC)GetProcAddress(g_hInstRas, szRasEnumEntries);
  919. g_pRasGetConnectStatus = (RASGETCONNECTSTATUSPROC)GetProcAddress(g_hInstRas, szRasGetConnectStatus);
  920. g_pRasGetErrorString = (RASGETERRORSTRINGPROC)GetProcAddress(g_hInstRas, szRasGetErrorString);
  921. g_pRasHangup = (RASHANGUPPROC)GetProcAddress(g_hInstRas, szRasHangup);
  922. g_pRasSetEntryDialParams = (RASSETENTRYDIALPARAMSPROC)GetProcAddress(g_hInstRas, szRasSetEntryDialParams);
  923. g_pRasGetEntryDialParams = (RASGETENTRYDIALPARAMSPROC)GetProcAddress(g_hInstRas, szRasGetEntryDialParams);
  924. g_pRasCreatePhonebookEntry = (RASCREATEPHONEBOOKENTRYPROC)GetProcAddress(g_hInstRas, szRasCreatePhonebookEntry);
  925. g_pRasEditPhonebookEntry = (RASEDITPHONEBOOKENTRYPROC)GetProcAddress(g_hInstRas, szRasEditPhonebookEntry);
  926. }
  927. if (!g_hInstRasDlg && FIsPlatformWinNT())
  928. {
  929. // Try loading Ras.
  930. uOldErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  931. g_hInstRasDlg = LoadLibrary(s_szRasDlgDll);
  932. SetErrorMode(uOldErrorMode);
  933. // Failure ?
  934. if (!g_hInstRasDlg)
  935. goto failure;
  936. g_pRasDialDlg = (RASDIALDLGPROC)GetProcAddress(g_hInstRasDlg, s_szRasDialDlg);
  937. if (!g_pRasDialDlg)
  938. goto failure;
  939. }
  940. // Make sure all functions have been loaded
  941. if (g_pRasDial &&
  942. g_pRasEnumConnections &&
  943. g_pRasEnumEntries &&
  944. g_pRasGetConnectStatus &&
  945. g_pRasGetErrorString &&
  946. g_pRasHangup &&
  947. g_pRasSetEntryDialParams &&
  948. g_pRasGetEntryDialParams &&
  949. g_pRasCreatePhonebookEntry &&
  950. g_pRasEditPhonebookEntry)
  951. {
  952. // Protected
  953. LeaveCriticalSection(&g_rCritSec);
  954. // Success
  955. return S_OK;
  956. }
  957. failure:
  958. // Protected
  959. LeaveCriticalSection(&g_rCritSec);
  960. // Otherwise, were hosed
  961. return TRAPHR(hrRasInitFailure);
  962. }
  963. // =====================================================================================
  964. // CombinedRasError
  965. // =====================================================================================
  966. VOID CombinedRasError(HWND hwnd, UINT unids, LPTSTR pszRasError, DWORD dwRasError)
  967. {
  968. // Locals
  969. TCHAR szRes[255],
  970. sz[30];
  971. LPTSTR pszError=NULL;
  972. // Load string
  973. AthLoadString(unids, szRes, sizeof(szRes));
  974. // Allocate memory for errors
  975. DWORD cchSize = lstrlen(szRes) + lstrlen(pszRasError) + 100;
  976. pszError = SzStrAlloc(cchSize);
  977. // Out of Memory ?
  978. if (!pszError)
  979. AthMessageBox(hwnd, MAKEINTRESOURCE(idsRasError), szRes, 0, MB_OK | MB_ICONSTOP);
  980. // Build Error message
  981. else
  982. {
  983. AthLoadString(idsErrorText, sz, sizeof(sz));
  984. wnsprintf(pszError, cchSize, "%s\n\n%s %d: %s", szRes, sz, dwRasError, pszRasError);
  985. AthMessageBox(hwnd, MAKEINTRESOURCE(idsRasError), pszError, 0, MB_OK | MB_ICONSTOP);
  986. MemFree(pszError);
  987. }
  988. }
  989. // =====================================================================================
  990. // HrRasError
  991. // =====================================================================================
  992. HRESULT HrRasError(HWND hwnd, HRESULT hrRasError, DWORD dwRasError)
  993. {
  994. // Locals
  995. TCHAR szRasError[256];
  996. BOOL fRasError=FALSE;
  997. // No Error
  998. if (SUCCEEDED(hrRasError))
  999. return hrRasError;
  1000. // Look up RAS error
  1001. if (dwRasError)
  1002. {
  1003. if (RasGetErrorString(dwRasError, szRasError, sizeof(szRasError)) == 0)
  1004. fRasError = TRUE;
  1005. else
  1006. *szRasError = _T('\0');
  1007. }
  1008. // General Error
  1009. switch(hrRasError)
  1010. {
  1011. case hrUserCancel:
  1012. break;
  1013. case hrMemory:
  1014. AthMessageBoxW(hwnd, MAKEINTRESOURCEW(idsRasError), MAKEINTRESOURCEW(idsMemory), 0, MB_OK | MB_ICONSTOP);
  1015. break;
  1016. case hrRasInitFailure:
  1017. AthMessageBoxW(hwnd, MAKEINTRESOURCEW(idsRasError), MAKEINTRESOURCEW(hrRasInitFailure), 0, MB_OK | MB_ICONSTOP);
  1018. break;
  1019. case hrRasDialFailure:
  1020. if (fRasError)
  1021. CombinedRasError(hwnd, HR_CODE(hrRasDialFailure), szRasError, dwRasError);
  1022. else
  1023. AthMessageBoxW(hwnd, MAKEINTRESOURCEW(idsRasError), MAKEINTRESOURCEW(hrRasDialFailure), 0, MB_OK | MB_ICONSTOP);
  1024. break;
  1025. case hrRasServerNotFound:
  1026. AthMessageBoxW(hwnd, MAKEINTRESOURCEW(idsRasError), MAKEINTRESOURCEW(hrRasServerNotFound), 0, MB_OK | MB_ICONSTOP);
  1027. break;
  1028. case hrGetDialParamsFailed:
  1029. AthMessageBoxW(hwnd, MAKEINTRESOURCEW(idsRasError), MAKEINTRESOURCEW(hrGetDialParamsFailed), 0, MB_OK | MB_ICONSTOP);
  1030. break;
  1031. case E_FAIL:
  1032. default:
  1033. AthMessageBoxW(hwnd, MAKEINTRESOURCEW(idsRasError), MAKEINTRESOURCEW(idsRasErrorGeneral), 0, MB_OK | MB_ICONSTOP);
  1034. break;
  1035. }
  1036. // Done
  1037. return hrRasError;
  1038. }
  1039. // =====================================================================================
  1040. // FEnumerateConnections
  1041. // =====================================================================================
  1042. BOOL FEnumerateConnections(LPRASCONN *ppRasConn, ULONG *pcConnections)
  1043. {
  1044. // Locals
  1045. DWORD dw,
  1046. dwSize;
  1047. BOOL fResult=FALSE;
  1048. // Check Params
  1049. Assert(ppRasConn && pcConnections);
  1050. // Make sure RAS is loaded
  1051. if (FAILED(HrVerifyRasLoaded()))
  1052. goto exit;
  1053. // Init
  1054. *ppRasConn = NULL;
  1055. *pcConnections = 0;
  1056. // Sizeof my buffer
  1057. dwSize = sizeof(RASCONN);
  1058. // Allocate enough for 1 ras connection info object
  1059. if (!MemAlloc((LPVOID *)ppRasConn, dwSize))
  1060. {
  1061. TRAPHR(hrMemory);
  1062. goto exit;
  1063. }
  1064. // Buffer size
  1065. (*ppRasConn)->dwSize = dwSize;
  1066. // Enumerate ras connections
  1067. dw = RasEnumConnections(*ppRasConn, &dwSize, pcConnections);
  1068. // Not enough memory ?
  1069. if (dw == ERROR_BUFFER_TOO_SMALL)
  1070. {
  1071. // Reallocate
  1072. if (!MemRealloc((LPVOID *)ppRasConn, dwSize))
  1073. {
  1074. TRAPHR(hrMemory);
  1075. goto exit;
  1076. }
  1077. // Call enumerate again
  1078. *pcConnections = 0;
  1079. (*ppRasConn)->dwSize = sizeof(RASCONN);
  1080. dw = RasEnumConnections(*ppRasConn, &dwSize, pcConnections);
  1081. }
  1082. // If still failed
  1083. if (dw)
  1084. {
  1085. AssertSz(FALSE, "RasEnumConnections failed");
  1086. goto exit;
  1087. }
  1088. // Success
  1089. fResult = TRUE;
  1090. exit:
  1091. // Done
  1092. return fResult;
  1093. }
  1094. // =====================================================================================
  1095. // FFindConnection
  1096. // =====================================================================================
  1097. BOOL FFindConnection(LPTSTR lpszEntry, LPHRASCONN phRasConn)
  1098. {
  1099. // Locals
  1100. ULONG cConnections,
  1101. i;
  1102. LPRASCONN pRasConn=NULL;
  1103. BOOL fResult=FALSE;
  1104. // Check Params
  1105. Assert(lpszEntry && phRasConn);
  1106. // Make sure RAS is loaded
  1107. if (FAILED(HrVerifyRasLoaded()))
  1108. goto exit;
  1109. // Init
  1110. *phRasConn = NULL;
  1111. // Enumerate Connections
  1112. if (!FEnumerateConnections(&pRasConn, &cConnections))
  1113. goto exit;
  1114. // If still failed
  1115. for (i=0; i<cConnections; i++)
  1116. {
  1117. if (lstrcmpi(pRasConn[i].szEntryName, lpszEntry) == 0)
  1118. {
  1119. *phRasConn = pRasConn[i].hrasconn;
  1120. fResult = TRUE;
  1121. goto exit;
  1122. }
  1123. }
  1124. exit:
  1125. // Cleanup
  1126. SafeMemFree(pRasConn);
  1127. // Done
  1128. return fResult;
  1129. }
  1130. // ==================================================================================================================
  1131. // FRasHangupAndWait
  1132. // ==================================================================================================================
  1133. BOOL FRasHangupAndWait(HRASCONN hRasConn, DWORD dwMaxWaitSeconds)
  1134. {
  1135. // Locals
  1136. RASCONNSTATUS rcs;
  1137. DWORD dwTicks=GetTickCount();
  1138. // Check Params
  1139. if (!hRasConn)
  1140. return 0;
  1141. // Make sure RAS is loaded
  1142. if (FAILED (HrVerifyRasLoaded()))
  1143. return FALSE;
  1144. // Call Ras hangup
  1145. if (RasHangup(hRasConn))
  1146. return FALSE;
  1147. // Wait for connection to really close
  1148. while (RasGetConnectStatus(hRasConn, &rcs) == 0)
  1149. {
  1150. // Wait timeout
  1151. if (GetTickCount() - dwTicks >= dwMaxWaitSeconds * 1000)
  1152. break;
  1153. // Sleep and yields
  1154. Sleep(0);
  1155. }
  1156. // Wait 1/2 seconds for modem to reset
  1157. Sleep(500);
  1158. // Done
  1159. return TRUE;
  1160. }
  1161. // ==================================================================================================================
  1162. // FillRasCombo
  1163. // ==================================================================================================================
  1164. VOID FillRasCombo(HWND hwndCtl, BOOL fUpdateOnly)
  1165. {
  1166. LPRASENTRYNAME lprasentry=NULL;
  1167. DWORD dwSize;
  1168. DWORD cEntries;
  1169. DWORD dwError;
  1170. // Make sure RAS is loaded
  1171. if (FAILED (HrVerifyRasLoaded()))
  1172. return;
  1173. if (!fUpdateOnly)
  1174. SendMessage(hwndCtl, CB_RESETCONTENT,0,0);
  1175. dwSize = sizeof(RASENTRYNAME);
  1176. if (!MemAlloc((LPVOID*) &lprasentry, dwSize))
  1177. return;
  1178. lprasentry->dwSize = sizeof(RASENTRYNAME);
  1179. cEntries = 0;
  1180. dwError = RasEnumEntries(NULL, NULL, lprasentry, &dwSize, &cEntries);
  1181. if (dwError == ERROR_BUFFER_TOO_SMALL)
  1182. {
  1183. MemFree(lprasentry);
  1184. MemAlloc((LPVOID*) &lprasentry, dwSize);
  1185. lprasentry->dwSize = sizeof(RASENTRYNAME);
  1186. cEntries = 0;
  1187. dwError = RasEnumEntries(NULL, NULL, lprasentry, &dwSize, &cEntries);
  1188. }
  1189. if (dwError)
  1190. goto error;
  1191. while(cEntries)
  1192. {
  1193. if (!fUpdateOnly)
  1194. SendMessage(hwndCtl, CB_ADDSTRING, 0,
  1195. (LPARAM)(lprasentry[cEntries-1].szEntryName));
  1196. else
  1197. {
  1198. if (ComboBox_FindStringExact(hwndCtl, 0,
  1199. lprasentry[cEntries - 1].szEntryName) < 0)
  1200. {
  1201. int iSel = ComboBox_AddString(hwndCtl,
  1202. lprasentry[cEntries - 1].szEntryName);
  1203. Assert(iSel >= 0);
  1204. ComboBox_SetCurSel(hwndCtl, iSel);
  1205. }
  1206. }
  1207. cEntries--;
  1208. }
  1209. error:
  1210. MemFree(lprasentry);
  1211. }
  1212. // ==================================================================================================================
  1213. // EditPhoneBookEntry
  1214. // ==================================================================================================================
  1215. DWORD EditPhonebookEntry(HWND hwnd, LPTSTR pszEntryName)
  1216. {
  1217. if (FAILED(HrVerifyRasLoaded()))
  1218. return (DWORD)E_FAIL;
  1219. return RasEditPhonebookEntry(hwnd, NULL, pszEntryName);
  1220. }
  1221. // ==================================================================================================================
  1222. // CreatePhonebookEntry
  1223. // ==================================================================================================================
  1224. DWORD CreatePhonebookEntry(HWND hwnd)
  1225. {
  1226. if (FAILED(HrVerifyRasLoaded()))
  1227. return (DWORD)E_FAIL;
  1228. return RasCreatePhonebookEntry(hwnd, NULL);
  1229. }