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.

465 lines
14 KiB

  1. /*****************************************************************************
  2. *
  3. * $Workfile: AddGetAd.cpp $
  4. *
  5. * Copyright (C) 1997 Hewlett-Packard Company.
  6. * Copyright (c) 1997 Microsoft Corporation.
  7. * All rights reserved.
  8. *
  9. * 11311 Chinden Blvd.
  10. * Boise, Idaho 83714
  11. *
  12. *****************************************************************************/
  13. /*
  14. * Author: Becky Jacobsen
  15. */
  16. #include "precomp.h"
  17. #include "TCPMonUI.h"
  18. #include "UIMgr.h"
  19. #include "InptChkr.h"
  20. #include "AddGetAd.h"
  21. #include "Resource.h"
  22. #include "TCPMonUI.h"
  23. #include "RTcpData.h"
  24. #include "LprData.h"
  25. #include "inisection.h"
  26. //
  27. // FUNCTION: CGetAddrDlg constructor
  28. //
  29. // PURPOSE: initialize a CGetAddrDlg class
  30. //
  31. CGetAddrDlg::CGetAddrDlg()
  32. {
  33. m_bDontAllowThisPageToBeDeactivated = FALSE;
  34. } // constructor
  35. //
  36. // FUNCTION: CGetAddrDlg destructor
  37. //
  38. // PURPOSE: deinitialize a CGetAddrDlg class
  39. //
  40. CGetAddrDlg::~CGetAddrDlg()
  41. {
  42. } // destructor
  43. //
  44. // FUNCTION: GetAddressDialog(HWND, UINT, WPARAM, LPARAM)
  45. //
  46. // PURPOSE: To process messages from the main dialog for adding a port.
  47. //
  48. // MESSAGES:
  49. //
  50. // WM_INITDIALOG - intializes the page
  51. // WM_COMMAND - handles button presses and text changes in edit controls.
  52. //
  53. //
  54. INT_PTR CALLBACK GetAddressDialog(
  55. HWND hDlg,
  56. UINT message,
  57. WPARAM wParam,
  58. LPARAM lParam)
  59. {
  60. BOOL bRc = FALSE;
  61. CGetAddrDlg *wndDlg = NULL;
  62. wndDlg = (CGetAddrDlg *)GetWindowLongPtr(hDlg, GWLP_USERDATA);
  63. switch (message) {
  64. case WM_INITDIALOG:
  65. wndDlg = new CGetAddrDlg;
  66. if( wndDlg != NULL )
  67. {
  68. SetLastError(ERROR_SUCCESS);
  69. if ( (!SetWindowLongPtr(hDlg, GWLP_USERDATA, (UINT_PTR)wndDlg) ) &&
  70. GetLastError() != ERROR_SUCCESS )
  71. {
  72. delete wndDlg;
  73. bRc = TRUE;
  74. }
  75. else
  76. bRc = wndDlg->OnInitDialog(hDlg, wParam, lParam);
  77. }
  78. break;
  79. case WM_COMMAND:
  80. if (wndDlg)
  81. bRc = wndDlg->OnCommand(hDlg, wParam, lParam);
  82. break;
  83. case WM_NOTIFY:
  84. if (wndDlg)
  85. bRc = wndDlg->OnNotify(hDlg, wParam, lParam);
  86. break;
  87. case WM_DESTROY:
  88. delete wndDlg;
  89. bRc = TRUE;
  90. break;
  91. default:
  92. return FALSE;
  93. }
  94. return bRc;
  95. } // AddPortDialog
  96. //
  97. // FUNCTION: OnInitDialog(HWND hDlg)
  98. //
  99. // PURPOSE: Initialize the dialog.
  100. //
  101. BOOL CGetAddrDlg::OnInitDialog(HWND hDlg, WPARAM, LPARAM lParam)
  102. {
  103. TCHAR sztAddPortInfo[ADD_PORT_INFO_LEN] = NULLSTR;
  104. LoadString(g_hInstance, IDS_STRING_ADD_PORT, sztAddPortInfo, ADD_PORT_INFO_LEN);
  105. SetWindowText(GetDlgItem(hDlg, IDC_STATIC_ADD_PORT), sztAddPortInfo);
  106. // initialize data members
  107. m_pParams = (ADD_PARAM_PACKAGE *) ((PROPSHEETPAGE *) lParam)->lParam;
  108. m_pParams->pData->sztHostAddress[0] = '\0';
  109. m_pParams->pData->sztPortName[0] = '\0';
  110. // Set limits on the address and port name lengths
  111. Edit_LimitText(GetDlgItem(hDlg, IDC_EDIT_DEVICE_ADDRESS), MAX_ADDRESS_LENGTH - 1);
  112. Edit_LimitText(GetDlgItem(hDlg, IDC_EDIT_PORT_NAME), MAX_PORTNAME_LEN - 1);
  113. return TRUE;
  114. } // OnInitDialog
  115. //
  116. // FUNCTION: OnCommand()
  117. //
  118. // PURPOSE: Process WM_COMMAND message
  119. //
  120. BOOL CGetAddrDlg::OnCommand(HWND hDlg, WPARAM wParam, LPARAM lParam)
  121. {
  122. switch(HIWORD(wParam)) {
  123. case EN_UPDATE:
  124. // one of the text controls had text changed in it.
  125. return OnEnUpdate(hDlg, wParam, lParam);
  126. break;
  127. default:
  128. return FALSE;
  129. }
  130. return TRUE;
  131. } // OnCommand
  132. //
  133. // FUNCTION: OnNotify()
  134. //
  135. // PURPOSE: Process WM_NOTIFY message
  136. //
  137. BOOL CGetAddrDlg::OnNotify(HWND hDlg, WPARAM wParam, LPARAM lParam)
  138. {
  139. switch (((NMHDR FAR *) lParam)->code) {
  140. case PSN_KILLACTIVE:
  141. // If the page requires additional user input before losing the
  142. // activation, it should use the SetWindowLong function to set the
  143. // DWL_MSGRESULT value of the page to TRUE. Also, the page should
  144. // display a message box that describes the problem and provides
  145. // the recommended action. The page should set DWL_MSGRESULT to FALSE
  146. // when it is okay to lose the activation.
  147. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, m_bDontAllowThisPageToBeDeactivated);
  148. return 1;
  149. break;
  150. case PSN_RESET:
  151. // reset to the original values
  152. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
  153. break;
  154. case PSN_SETACTIVE:
  155. TCHAR szTemp[MAX_PATH];
  156. lstrcpyn( szTemp, m_pParams->pData->sztHostAddress,
  157. SIZEOF_IN_CHAR(szTemp) );
  158. m_InputChkr.MakePortName( szTemp, COUNTOF (szTemp) );
  159. if((_tcscmp(m_pParams->pData->sztHostAddress,
  160. m_pParams->pData->sztPortName) == 0) ||
  161. (_tcscmp( m_pParams->pData->sztPortName, szTemp ) == 0 ))
  162. {
  163. m_InputChkr.LinkPortNameAndAddressInput();
  164. } else {
  165. m_InputChkr.UnlinkPortNameAndAddressInput();
  166. }
  167. SetWindowText(GetDlgItem(hDlg, IDC_EDIT_DEVICE_ADDRESS), m_pParams->pData->sztHostAddress);
  168. SetWindowText(GetDlgItem(hDlg, IDC_EDIT_PORT_NAME), m_pParams->pData->sztPortName);
  169. m_bDontAllowThisPageToBeDeactivated = FALSE;
  170. PropSheet_SetWizButtons(GetParent(hDlg), PSWIZB_NEXT | PSWIZB_BACK /* | PSWIZB_FINISH */);
  171. break;
  172. case PSN_WIZNEXT:
  173. // the Next button was pressed
  174. m_bDontAllowThisPageToBeDeactivated = FALSE;
  175. OnNext(hDlg);
  176. // To jump to a page other than the previous or next one,
  177. // an application should set DWL_MSGRESULT to the identifier
  178. // of the dialog box to be displayed.
  179. switch ( m_pParams->dwDeviceType ) {
  180. case SUCCESS_DEVICE_SINGLE_PORT:
  181. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, IDD_DIALOG_SUMMARY);
  182. break;
  183. case SUCCESS_DEVICE_MULTI_PORT:
  184. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, IDD_DIALOG_MULTIPORT);
  185. break;
  186. default:
  187. //
  188. // No action necessary
  189. //
  190. break;
  191. }
  192. break;
  193. case PSN_WIZBACK:
  194. m_bDontAllowThisPageToBeDeactivated = FALSE;
  195. break;
  196. case PSN_QUERYCANCEL:
  197. m_pParams->dwLastError = ERROR_CANCELLED;
  198. return FALSE;
  199. break;
  200. default:
  201. return FALSE;
  202. }
  203. return TRUE;
  204. } // OnCommand
  205. //
  206. // FUNCTION: OnEnUpdate(HWND hDlg, WPARAM wParam, LPARAM lParam)
  207. //
  208. // PURPOSE:
  209. //
  210. //
  211. BOOL CGetAddrDlg::OnEnUpdate(HWND hDlg, WPARAM wParam, LPARAM lParam)
  212. {
  213. int idEditCtrl = (int) LOWORD(wParam);
  214. HWND hwndEditCtrl = NULL;
  215. hwndEditCtrl = (HWND)lParam;
  216. switch(idEditCtrl) {
  217. case IDC_EDIT_PORT_NAME:
  218. m_InputChkr.OnUpdatePortName(idEditCtrl, hwndEditCtrl);
  219. break;
  220. case IDC_EDIT_DEVICE_ADDRESS:
  221. if (SendMessage(hwndEditCtrl, EM_GETMODIFY, 0, 0)) {
  222. // The port address has changed
  223. // so we need to probe the network again
  224. //
  225. m_pParams->bBypassNetProbe = FALSE;
  226. }
  227. m_InputChkr.OnUpdateAddress(hDlg, idEditCtrl, hwndEditCtrl, m_pParams->pszServer);
  228. break;
  229. default:
  230. //
  231. // Should never get here
  232. //
  233. break;
  234. }
  235. return TRUE;
  236. } // OnEnUpdate
  237. //
  238. // FUNCTION: OnNext(HWND hDlg)
  239. //
  240. // PURPOSE: When the user clicks Next this function does all the necessary
  241. // things to create a port. Verify the address, check to see if there
  242. // is already a port existing with the given name/address, get the
  243. // device type, and set the values in the m_PortData structure.
  244. //
  245. void CGetAddrDlg::OnNext(HWND hDlg)
  246. {
  247. HCURSOR hNewCursor = NULL;
  248. HCURSOR hOldCursor = NULL;
  249. IniSection *pIniSection = NULL;
  250. TCHAR ptcsAddress[MAX_ADDRESS_LENGTH] = NULLSTR;
  251. TCHAR ptcsPortName[MAX_PORTNAME_LEN] = NULLSTR;
  252. TCHAR sztSystemDesc[MAX_PORT_DESCRIPTION_LEN] = NULLSTR;
  253. DWORD dwDeviceType = SUCCESS_DEVICE_UNKNOWN;
  254. DWORD dwPortNum = DEFAULT_PORT_NUMBER;
  255. DWORD dwNumMultiPorts = 0;
  256. DWORD dwRet = ERROR_DEVICE_NOT_FOUND;
  257. if ( hNewCursor = LoadCursor(NULL, IDC_WAIT) )
  258. hOldCursor = SetCursor(hNewCursor);
  259. GetWindowText(GetDlgItem(hDlg, IDC_EDIT_DEVICE_ADDRESS), ptcsAddress, MAX_ADDRESS_LENGTH);
  260. GetWindowText(GetDlgItem(hDlg, IDC_EDIT_PORT_NAME), ptcsPortName, MAX_PORTNAME_LEN);
  261. if(! m_InputChkr.AddressIsLegal(ptcsAddress)) {
  262. m_bDontAllowThisPageToBeDeactivated = TRUE;
  263. DisplayErrorMessage(hDlg, IDS_STRING_ERROR_TITLE, IDS_STRING_ERROR_ADDRESS_NOT_VALID);
  264. return;
  265. }
  266. if(! m_InputChkr.PortNameIsLegal(ptcsPortName)) {
  267. m_bDontAllowThisPageToBeDeactivated = TRUE;
  268. DisplayErrorMessage(hDlg, IDS_STRING_ERROR_TITLE, IDS_STRING_ERROR_PORTNAME_NOT_VALID);
  269. return;
  270. }
  271. if(! m_InputChkr.PortNameIsUnique(ptcsPortName, m_pParams->pszServer)) {
  272. m_bDontAllowThisPageToBeDeactivated = TRUE;
  273. DisplayErrorMessage(hDlg, IDS_STRING_ERROR_TITLE, IDS_STRING_ERROR_PORTNAME_NOT_UNIQUE);
  274. return;
  275. }
  276. memset( m_pParams->sztPortDesc, '\0', sizeof( m_pParams->sztPortDesc ));
  277. memset( m_pParams->sztSectionName, '\0', sizeof( m_pParams->sztSectionName ) );
  278. m_pParams->bMultiPort = FALSE;
  279. dwRet = GetDeviceDescription(ptcsAddress, sztSystemDesc, SIZEOF_IN_CHAR(sztSystemDesc));
  280. switch( dwRet ) {
  281. case NO_ERROR:
  282. if ( pIniSection = new IniSection() ) {
  283. if ( pIniSection->GetIniSection( sztSystemDesc )) {
  284. if ( pIniSection->GetDWord(PORTS_KEY, &dwNumMultiPorts) &&
  285. dwNumMultiPorts > 1 ) {
  286. dwDeviceType = SUCCESS_DEVICE_MULTI_PORT;
  287. m_pParams->bMultiPort = TRUE;
  288. lstrcpyn(m_pParams->sztSectionName,
  289. pIniSection->GetSectionName(),
  290. MAX_SECTION_NAME);
  291. } else {
  292. dwDeviceType = SUCCESS_DEVICE_SINGLE_PORT;
  293. if (! pIniSection->GetPortInfo( ptcsAddress, m_pParams->pData, 1 , m_pParams->bBypassNetProbe)) {
  294. if (GetLastError () == ERROR_DEVICE_NOT_FOUND) {
  295. // The IP address is incorrect, we should by pass network probe from now on
  296. //
  297. m_pParams->bBypassNetProbe = TRUE;
  298. }
  299. }
  300. }
  301. pIniSection->GetString( PORT_NAME_KEY, m_pParams->sztPortDesc, SIZEOF_IN_CHAR(m_pParams->sztPortDesc));
  302. }
  303. delete pIniSection;
  304. pIniSection = NULL;
  305. }
  306. break;
  307. case SUCCESS_DEVICE_UNKNOWN:
  308. dwDeviceType = SUCCESS_DEVICE_UNKNOWN;
  309. break;
  310. default:
  311. dwDeviceType = ERROR_DEVICE_NOT_FOUND;
  312. m_pParams->bBypassNetProbe = TRUE;
  313. break;
  314. }
  315. if ( hNewCursor )
  316. SetCursor(hOldCursor);
  317. // Set values in the outgoing structure
  318. lstrcpyn(m_pParams->pData->sztPortName, ptcsPortName, MAX_PORTNAME_LEN);
  319. lstrcpyn(m_pParams->pData->sztHostAddress, ptcsAddress, MAX_NETWORKNAME_LEN);
  320. m_pParams->dwDeviceType = dwDeviceType;
  321. } // OnNext
  322. //
  323. // FUNCTION: GetDeviceDescription()
  324. //
  325. // PURPOSE: Get the description of the user requested printer.
  326. //
  327. // Return Value Error Codes:
  328. // NO_ERROR
  329. // ERROR_DLL_NOT_FOUND
  330. //
  331. // Return Values in dwType:
  332. // ERROR_DEVICE_NOT_FOUND
  333. // SUCCESS_DEVICE_SINGLE_PORT
  334. // SUCCESS_DEVICE_MULTI_PORT
  335. // SUCCESS_DEVICE_UNKNOWN
  336. DWORD
  337. CGetAddrDlg::
  338. GetDeviceDescription(
  339. LPCTSTR pAddress,
  340. LPTSTR pszPortDesc,
  341. DWORD cBytes
  342. )
  343. {
  344. // Here is the essence of the code below without all the load
  345. // library stuff in addition:
  346. //
  347. // CTcpMibABC *pTcpMib = NULL;
  348. // pTcpMib = (CTcpMibABC *) GetTcpMibPtr();
  349. //
  350. // char HostName[MAX_NETWORKNAME_LEN];
  351. // UNICODE_TO_MBCS(HostName, MAX_NETWORKNAME_LEN, pAddress, -1);
  352. // *dwType = pTcpMib->GetDeviceType(HostName, pdwPortNum);
  353. //
  354. // return (NO_ERROR);
  355. //
  356. DWORD dwRet = ERROR_DEVICE_NOT_FOUND;
  357. CTcpMibABC *pTcpMib = NULL;
  358. FARPROC pfnGetTcpMibPtr = NULL;
  359. if ( !g_hTcpMibLib ) {
  360. goto Done;
  361. }
  362. pfnGetTcpMibPtr = ::GetProcAddress(g_hTcpMibLib, "GetTcpMibPtr");
  363. if ( !pfnGetTcpMibPtr ) {
  364. goto Done;
  365. }
  366. if ( pTcpMib = (CTcpMibABC *) pfnGetTcpMibPtr() ) {
  367. char HostName[MAX_NETWORKNAME_LEN] = "";
  368. UNICODE_TO_MBCS(HostName, MAX_NETWORKNAME_LEN, pAddress, -1);
  369. dwRet = pTcpMib->GetDeviceDescription(HostName,
  370. DEFAULT_SNMP_COMMUNITYA,
  371. DEFAULT_SNMP_DEVICE_INDEX,
  372. pszPortDesc,
  373. cBytes);
  374. }
  375. Done:
  376. return dwRet;
  377. } // GetDeviceType