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.

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