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.

469 lines
12 KiB

  1. /*-----------------------------------------------------------------------------
  2. enumodem.cpp
  3. Holds code that deals with the "Choose a modem" dialog needed when user has
  4. multiple modems installed
  5. Copyright (C) 1996 Microsoft Corporation
  6. All rights reserved
  7. Authors:
  8. jmazner Jeremy Mazner
  9. History:
  10. 10/19/96 jmazner Created, cloned almost verbatim from
  11. INETCFG's rnacall.cpp and export.cpp
  12. -----------------------------------------------------------------------------*/
  13. #include "isignup.h"
  14. #include "enumodem.h"
  15. #include <WINDOWSX.H>
  16. //+---------------------------------------------------------------------------
  17. //
  18. // Function: ProcessDBCS
  19. //
  20. // Synopsis: Converts control to use DBCS compatible font
  21. // Use this at the beginning of the dialog procedure
  22. //
  23. // Note that this is required due to a bug in Win95-J that prevents
  24. // it from properly mapping MS Shell Dlg. This hack is not needed
  25. // under winNT.
  26. //
  27. // Arguments: hwnd - Window handle of the dialog
  28. // cltID - ID of the control you want changed.
  29. //
  30. // Returns: ERROR_SUCCESS
  31. //
  32. // History: 4/31/97 a-frankh Created
  33. // 5/13/97 jmazner Stole from CM to use here
  34. //----------------------------------------------------------------------------
  35. void ProcessDBCS(HWND hDlg, int ctlID)
  36. {
  37. #if defined(WIN16)
  38. return;
  39. #else
  40. HFONT hFont = NULL;
  41. if( IsNT() )
  42. {
  43. return;
  44. }
  45. hFont = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
  46. if (hFont == NULL)
  47. hFont = (HFONT) GetStockObject(SYSTEM_FONT);
  48. if (hFont != NULL)
  49. SendMessage(GetDlgItem(hDlg,ctlID), WM_SETFONT, (WPARAM) hFont, MAKELPARAM(TRUE, 0));
  50. #endif
  51. }
  52. /*******************************************************************
  53. NAME: CEnumModem::CEnumModem
  54. SYNOPSIS: Constructor for class to enumerate modems
  55. NOTES: Useful to have a class rather than C functions for
  56. this, due to how the enumerators function
  57. ********************************************************************/
  58. CEnumModem::CEnumModem() :
  59. m_dwError(ERROR_SUCCESS),m_lpData(NULL),m_dwIndex(0)
  60. {
  61. DWORD cbSize = 0;
  62. if (!LoadRnaFunctions(NULL))
  63. m_dwError = GetLastError();
  64. else
  65. // Use the reinit member function to do the work.
  66. this->ReInit();
  67. }
  68. /*******************************************************************
  69. NAME: CEnumModem::ReInit
  70. SYNOPSIS: Re-enumerate the modems, freeing the old memory.
  71. ********************************************************************/
  72. DWORD CEnumModem::ReInit()
  73. {
  74. DWORD cbSize = 0;
  75. // Clean up the old list
  76. if (m_lpData)
  77. {
  78. delete m_lpData;
  79. m_lpData = NULL;
  80. }
  81. m_dwNumEntries = 0;
  82. m_dwIndex = 0;
  83. // call RasEnumDevices with no buffer to find out required buffer size
  84. m_dwError = (lpfnRasEnumDevices)(NULL, &cbSize, &m_dwNumEntries);
  85. // Special case check to work around RNA bug where ERROR_BUFFER_TOO_SMALL
  86. // is returned even if there are no devices.
  87. // If there are no devices, we are finished.
  88. if (0 == m_dwNumEntries)
  89. {
  90. m_dwError = ERROR_SUCCESS;
  91. return m_dwError;
  92. }
  93. // Since we were just checking how much mem we needed, we expect
  94. // a return value of ERROR_BUFFER_TOO_SMALL, or it may just return
  95. // ERROR_SUCCESS (ChrisK 7/9/96).
  96. if (ERROR_BUFFER_TOO_SMALL != m_dwError && ERROR_SUCCESS != m_dwError)
  97. {
  98. return m_dwError;
  99. }
  100. // Allocate the space for the data
  101. m_lpData = (LPRASDEVINFO) new TCHAR[cbSize];
  102. if (NULL == m_lpData)
  103. {
  104. DebugOut("ICWCONN1: CEnumModem: Failed to allocate device list buffer\n");
  105. m_dwError = ERROR_NOT_ENOUGH_MEMORY;
  106. return m_dwError;
  107. }
  108. m_lpData->dwSize = sizeof(RASDEVINFO);
  109. m_dwNumEntries = 0;
  110. // enumerate the modems into buffer
  111. m_dwError = (lpfnRasEnumDevices)(m_lpData, &cbSize,
  112. &m_dwNumEntries);
  113. if (ERROR_SUCCESS != m_dwError)
  114. return m_dwError;
  115. //
  116. // ChrisK Olympus 4560 do not include VPN's in the list
  117. //
  118. DWORD dwTempNumEntries;
  119. DWORD idx;
  120. LPRASDEVINFO lpNextValidDevice;
  121. dwTempNumEntries = m_dwNumEntries;
  122. lpNextValidDevice = m_lpData;
  123. //
  124. // Walk through the list of devices and copy non-VPN device to the first
  125. // available element of the array.
  126. //
  127. for (idx = 0;idx < dwTempNumEntries; idx++)
  128. {
  129. if (0 != lstrcmpi(TEXT("VPN"),m_lpData[idx].szDeviceType))
  130. {
  131. if (lpNextValidDevice != &m_lpData[idx])
  132. {
  133. MoveMemory(lpNextValidDevice ,&m_lpData[idx],sizeof(RASDEVINFO));
  134. }
  135. lpNextValidDevice++;
  136. }
  137. else
  138. {
  139. m_dwNumEntries--;
  140. }
  141. }
  142. return m_dwError;
  143. }
  144. /*******************************************************************
  145. NAME: CEnumModem::~CEnumModem
  146. SYNOPSIS: Destructor for class
  147. ********************************************************************/
  148. CEnumModem::~CEnumModem()
  149. {
  150. if (m_lpData)
  151. {
  152. delete m_lpData;
  153. m_lpData = NULL;
  154. }
  155. }
  156. /*******************************************************************
  157. NAME: CEnumModem::Next
  158. SYNOPSIS: Enumerates next modem
  159. EXIT: Returns a pointer to device info structure. Returns
  160. NULL if no more modems or error occurred. Call GetError
  161. to determine if error occurred.
  162. ********************************************************************/
  163. TCHAR * CEnumModem::Next()
  164. {
  165. if (m_dwIndex < m_dwNumEntries)
  166. {
  167. return m_lpData[m_dwIndex++].szDeviceName;
  168. }
  169. return NULL;
  170. }
  171. /*******************************************************************
  172. NAME: CEnumModem::GetDeviceTypeFromName
  173. SYNOPSIS: Returns type string for specified device.
  174. EXIT: Returns a pointer to device type string for first
  175. device name that matches. Returns
  176. NULL if no device with specified name is found
  177. ********************************************************************/
  178. TCHAR * CEnumModem::GetDeviceTypeFromName(LPTSTR szDeviceName)
  179. {
  180. DWORD dwIndex = 0;
  181. while (dwIndex < m_dwNumEntries)
  182. {
  183. if (!lstrcmp(m_lpData[dwIndex].szDeviceName, szDeviceName))
  184. {
  185. return m_lpData[dwIndex].szDeviceType;
  186. }
  187. dwIndex++;
  188. }
  189. return NULL;
  190. }
  191. /*******************************************************************
  192. NAME: CEnumModem::GetDeviceNameFromType
  193. SYNOPSIS: Returns type string for specified device.
  194. EXIT: Returns a pointer to device name string for first
  195. device type that matches. Returns
  196. NULL if no device with specified Type is found
  197. ********************************************************************/
  198. TCHAR * CEnumModem::GetDeviceNameFromType(LPTSTR szDeviceType)
  199. {
  200. DWORD dwIndex = 0;
  201. while (dwIndex < m_dwNumEntries)
  202. {
  203. if (!lstrcmp(m_lpData[dwIndex].szDeviceType, szDeviceType))
  204. {
  205. return m_lpData[dwIndex].szDeviceName;
  206. }
  207. dwIndex++;
  208. }
  209. return NULL;
  210. }
  211. /*******************************************************************
  212. NAME: CEnumModem::VerifyDeviceNameAndType
  213. SYNOPSIS: Determines whether there is a device with the name
  214. and type given.
  215. EXIT: Returns TRUE if the specified device was found,
  216. FALSE otherwise.
  217. ********************************************************************/
  218. BOOL CEnumModem::VerifyDeviceNameAndType(LPTSTR szDeviceName, LPTSTR szDeviceType)
  219. {
  220. DWORD dwIndex = 0;
  221. while (dwIndex < m_dwNumEntries)
  222. {
  223. if (!lstrcmp(m_lpData[dwIndex].szDeviceType, szDeviceType) &&
  224. !lstrcmp(m_lpData[dwIndex].szDeviceName, szDeviceName))
  225. {
  226. return TRUE;
  227. }
  228. dwIndex++;
  229. }
  230. return FALSE;
  231. }
  232. /*******************************************************************
  233. NAME: ChooseModemDlgProc
  234. SYNOPSIS: Dialog proc for choosing modem
  235. ********************************************************************/
  236. INT_PTR CALLBACK ChooseModemDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam,
  237. LPARAM lParam)
  238. {
  239. BOOL fRet;
  240. switch (uMsg)
  241. {
  242. case WM_INITDIALOG:
  243. // lParam contains pointer to CHOOSEMODEMDLGINFO struct, set it
  244. // in window data
  245. //Assert(lParam);
  246. SetWindowLongPtr(hDlg,DWLP_USER,lParam);
  247. fRet = ChooseModemDlgInit(hDlg,(PCHOOSEMODEMDLGINFO) lParam);
  248. if (!fRet)
  249. {
  250. // An error occured.
  251. EndDialog(hDlg,FALSE);
  252. }
  253. #if !defined(WIN16)
  254. SetForegroundWindow(hDlg);
  255. #endif
  256. return fRet;
  257. break;
  258. case WM_COMMAND:
  259. switch (LOWORD(wParam))
  260. {
  261. case IDC_CMDOK:
  262. {
  263. // get data pointer from window data
  264. PCHOOSEMODEMDLGINFO pChooseModemDlgInfo =
  265. (PCHOOSEMODEMDLGINFO) GetWindowLongPtr(hDlg,DWLP_USER);
  266. //Assert(pChooseModemDlgInfo);
  267. // pass the data to the OK handler
  268. fRet=ChooseModemDlgOK(hDlg,pChooseModemDlgInfo);
  269. if (fRet)
  270. {
  271. EndDialog(hDlg,TRUE);
  272. }
  273. }
  274. break;
  275. case IDC_CMDCANCEL:
  276. SetLastError(ERROR_CANCELLED);
  277. EndDialog(hDlg,FALSE);
  278. break;
  279. }
  280. break;
  281. }
  282. return FALSE;
  283. }
  284. /*******************************************************************
  285. NAME: ChooseModemDlgInit
  286. SYNOPSIS: proc to handle initialization of dialog for choosing modem
  287. ********************************************************************/
  288. BOOL ChooseModemDlgInit(HWND hDlg,PCHOOSEMODEMDLGINFO pChooseModemDlgInfo)
  289. {
  290. //Assert(pChooseModemDlgInfo);
  291. // put the dialog in the center of the screen
  292. //RECT rc;
  293. //GetWindowRect(hDlg, &rc);
  294. //SetWindowPos(hDlg, NULL,
  295. // ((GetSystemMetrics(SM_CXSCREEN) - (rc.right - rc.left)) / 2),
  296. // ((GetSystemMetrics(SM_CYSCREEN) - (rc.bottom - rc.top)) / 2),
  297. // 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
  298. // fill the combobox with available modems
  299. ProcessDBCS(hDlg,IDC_MODEM);
  300. DWORD dwRet = InitModemList(GetDlgItem(hDlg,IDC_MODEM));
  301. if (ERROR_SUCCESS != dwRet)
  302. {
  303. DebugOut("ICWCONN1: ChooseModemDlgInit: Error initializing modem list!\n");
  304. SetLastError(dwRet);
  305. return FALSE;
  306. }
  307. return TRUE;
  308. }
  309. /*******************************************************************
  310. NAME: ChooseModemDlgOK
  311. SYNOPSIS: OK handler for dialog for choosing modem
  312. ********************************************************************/
  313. BOOL ChooseModemDlgOK(HWND hDlg,PCHOOSEMODEMDLGINFO pChooseModemDlgInfo)
  314. {
  315. //Assert(pChooseModemDlgInfo);
  316. // should always have a selection in combo box if we get here
  317. //Assert(ComboBox_GetCurSel(GetDlgItem(hDlg,IDC_MODEM)) >= 0);
  318. // get modem name out of combo box
  319. ComboBox_GetText(GetDlgItem(hDlg,IDC_MODEM),
  320. pChooseModemDlgInfo->szModemName,
  321. SIZEOF_TCHAR_BUFFER(pChooseModemDlgInfo->szModemName));
  322. //Assert(lstrlen(pChooseModemDlgInfo->szModemName));
  323. // clear the modem list
  324. ComboBox_ResetContent(GetDlgItem(hDlg,IDC_MODEM));
  325. return TRUE;
  326. }
  327. /*******************************************************************
  328. NAME: InitModemList
  329. SYNOPSIS: Fills a combo box window with installed modems
  330. ENTRY: hCB - combo box window to fill
  331. ********************************************************************/
  332. HRESULT InitModemList (HWND hCB)
  333. {
  334. DebugOut("ICWCONN1::enumodem.cpp InitModemList()\n");
  335. LPTSTR pNext;
  336. int nIndex;
  337. DWORD dwRet;
  338. //Assert(hCB);
  339. CEnumModem cEnumModem;
  340. // clear out the combo box
  341. ComboBox_ResetContent(hCB);
  342. while ( pNext = cEnumModem.Next() )
  343. {
  344. // Add the device to the combo box
  345. nIndex = ComboBox_AddString(hCB, pNext);
  346. ComboBox_SetItemData(hCB, nIndex, NULL);
  347. }
  348. // Select the default device
  349. ComboBox_SetCurSel(hCB, nIndex);
  350. return ERROR_SUCCESS;
  351. }