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.

427 lines
10 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1998-2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: IPUI.cpp
  6. * Content: Winsock service provider IP UI functions
  7. *
  8. *
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 10/15/1999 jtk Dervied from ComPortUI.cpp
  13. ***************************************************************************/
  14. #include "dnwsocki.h"
  15. #ifndef DPNBUILD_NOSPUI
  16. //**********************************************************************
  17. // Constant definitions
  18. //**********************************************************************
  19. //
  20. // expected return from IP dialog
  21. //
  22. static const INT_PTR g_iExpectedIPDialogReturn = 0x12345678;
  23. //**********************************************************************
  24. // Macro definitions
  25. //**********************************************************************
  26. //**********************************************************************
  27. // Structure definitions
  28. //**********************************************************************
  29. //**********************************************************************
  30. // Variable definitions
  31. //**********************************************************************
  32. //**********************************************************************
  33. // Function prototypes
  34. //**********************************************************************
  35. static INT_PTR CALLBACK SettingsDialogProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam );
  36. static HRESULT SetIPHostName( const HWND hDlg, const CEndpoint *const pEndpoint );
  37. static HRESULT GetDialogData( const HWND hDlg, CEndpoint *const pEndpoint );
  38. //**********************************************************************
  39. // Function definitions
  40. //**********************************************************************
  41. //**********************************************************************
  42. // ------------------------------
  43. // DisplayIPHostNameDialog - dialog for comport settings
  44. //
  45. // Entry: Pointer to endpoint
  46. //
  47. // Exit: Error code
  48. // ------------------------------
  49. #undef DPF_MODNAME
  50. #define DPF_MODNAME "DisplayIPHostNameSettingsDialog"
  51. void DisplayIPHostNameSettingsDialog( void *const pContext )
  52. {
  53. CEndpoint *pEndpoint;
  54. INT_PTR iDlgReturn;
  55. DNASSERT( pContext != NULL );
  56. //
  57. // intialize
  58. //
  59. pEndpoint = static_cast<CEndpoint*>( pContext );
  60. DBG_CASSERT( sizeof( pEndpoint ) == sizeof( LPARAM ) );
  61. DPFX(DPFPREP, 5, "Starting IP settings dialog for endpoint 0x%p.", pEndpoint );
  62. SetLastError( ERROR_SUCCESS );
  63. iDlgReturn = DialogBoxParam( g_hDLLInstance, // handle of module for resources
  64. MAKEINTRESOURCE( IDD_IP_SETTINGS ), // resource for dialog
  65. NULL, // parent window (none)
  66. SettingsDialogProc, // dialog message proc
  67. reinterpret_cast<LPARAM>( pEndpoint ) // startup parameter
  68. );
  69. if ( iDlgReturn != g_iExpectedIPDialogReturn )
  70. {
  71. DWORD dwError;
  72. dwError = GetLastError();
  73. DPFX(DPFPREP, 0, "Failed to start IP settings dialog!" );
  74. DisplayErrorCode( 0, dwError );
  75. pEndpoint->SettingsDialogComplete( DPNERR_OUTOFMEMORY );
  76. }
  77. return;
  78. }
  79. //**********************************************************************
  80. //**********************************************************************
  81. // ------------------------------
  82. // SetopIPHostNameSettingsDialog - stop dialog dialog for serial settings
  83. //
  84. // Entry: Handle of dialog
  85. //
  86. // Exit: Nothing
  87. // ------------------------------
  88. #undef DPF_MODNAME
  89. #define DPF_MODNAME "StopIPHostNameSettingsDialog"
  90. void StopIPHostNameSettingsDialog( const HWND hDlg )
  91. {
  92. DNASSERT( hDlg != NULL );
  93. if ( PostMessage( hDlg, WM_COMMAND, MAKEWPARAM( IDCANCEL, NULL ), NULL ) == 0 )
  94. {
  95. DWORD dwError;
  96. dwError = GetLastError();
  97. DPFX(DPFPREP, 0, "Failed to stop dialog!" );
  98. DisplayErrorCode( 0, dwError );
  99. DNASSERT( FALSE );
  100. }
  101. }
  102. //**********************************************************************
  103. //**********************************************************************
  104. // ------------------------------
  105. // SettingsDialogProc - dialog proc serial settings
  106. //
  107. // Entry: Window handle
  108. // Message
  109. // Message LPARAM
  110. // Message WPARAM
  111. //
  112. // Exit: Error code
  113. // ------------------------------
  114. #undef DPF_MODNAME
  115. #define DPF_MODNAME "SettingsDialogProc"
  116. static INT_PTR CALLBACK SettingsDialogProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
  117. {
  118. CEndpoint *pEndpoint;
  119. HRESULT hr;
  120. //
  121. // initialize
  122. //
  123. hr = DPN_OK;
  124. pEndpoint = NULL;
  125. //
  126. // Get the dialog context. Note that the dialog context will be NULL
  127. // until the WM_INITDIALOG message is processed so the endpoint may note be
  128. // availble yet.
  129. //
  130. DBG_CASSERT( sizeof( pEndpoint ) == sizeof( LPARAM ) );
  131. pEndpoint = reinterpret_cast<CEndpoint*>( GetWindowLongPtr( hDlg, GWLP_USERDATA ) );
  132. switch ( uMsg )
  133. {
  134. //
  135. // initialize dialog
  136. //
  137. case WM_INITDIALOG:
  138. {
  139. //
  140. // since this is the first dialog message, the default code to set
  141. // pEndpoint isn't getting valid data
  142. //
  143. DBG_CASSERT( sizeof( pEndpoint ) == sizeof( lParam ) );
  144. pEndpoint = reinterpret_cast<CEndpoint*>( lParam );
  145. pEndpoint->Lock();
  146. if (pEndpoint->GetState() == ENDPOINT_STATE_DISCONNECTING)
  147. {
  148. hr = DPNERR_USERCANCEL;
  149. goto Failure;
  150. }
  151. else
  152. {
  153. pEndpoint->SetActiveDialogHandle( hDlg );
  154. }
  155. pEndpoint->Unlock();
  156. //
  157. // SetWindowLongPtr() returns NULL in case of error. It's possible that
  158. // the old value from SetWindowLongPtr() was really NULL in which case it's not
  159. // an error. To be safe, clear any residual error code before calling
  160. // SetWindowLongPtr().
  161. //
  162. SetLastError( 0 );
  163. if ( SetWindowLongPtr( hDlg, GWLP_USERDATA, lParam ) == NULL )
  164. {
  165. DWORD dwError;
  166. dwError = GetLastError();
  167. if ( dwError != ERROR_SUCCESS )
  168. {
  169. DPFX(DPFPREP, 0, "Problem setting user data for window!" );
  170. DisplayErrorCode( 0, dwError );
  171. hr = DPNERR_GENERIC;
  172. goto Failure;
  173. }
  174. }
  175. //
  176. // set dialog parameters
  177. //
  178. if ( ( hr = SetIPHostName( hDlg, pEndpoint ) ) != DPN_OK )
  179. {
  180. DPFX(DPFPREP, 0, "Problem setting device in WM_INITDIALOG!" );
  181. DisplayDNError( 0, hr );
  182. goto Failure;
  183. }
  184. return TRUE;
  185. break;
  186. }
  187. //
  188. // a control did something
  189. //
  190. case WM_COMMAND:
  191. {
  192. //
  193. // what was the control?
  194. //
  195. switch ( LOWORD( wParam ) )
  196. {
  197. case IDOK:
  198. {
  199. if ( ( hr = GetDialogData( hDlg, pEndpoint ) ) != DPN_OK )
  200. {
  201. DPFX(DPFPREP, 0, "Problem getting UI data!" );
  202. DisplayDNError( 0, hr );
  203. goto Failure;
  204. }
  205. //
  206. // pass any error code on to 'DialogComplete'
  207. //
  208. EndDialog( hDlg, g_iExpectedIPDialogReturn );
  209. pEndpoint->SettingsDialogComplete( hr );
  210. break;
  211. }
  212. case IDCANCEL:
  213. {
  214. EndDialog( hDlg, g_iExpectedIPDialogReturn );
  215. pEndpoint->SettingsDialogComplete( DPNERR_USERCANCEL );
  216. break;
  217. }
  218. default:
  219. {
  220. break;
  221. }
  222. }
  223. break;
  224. }
  225. // window is closing
  226. case WM_CLOSE:
  227. {
  228. break;
  229. }
  230. }
  231. Exit:
  232. return FALSE;
  233. Failure:
  234. DNASSERT( pEndpoint != NULL );
  235. DNASSERT( hr != DPN_OK );
  236. pEndpoint->SettingsDialogComplete( hr );
  237. EndDialog( hDlg, g_iExpectedIPDialogReturn );
  238. goto Exit;
  239. }
  240. //**********************************************************************
  241. //**********************************************************************
  242. // ------------------------------
  243. // SetIPHostName - set hostname field
  244. //
  245. // Entry: Window handle
  246. // Pointer to endpoint
  247. //
  248. // Exit: Error code
  249. // ------------------------------
  250. #undef DPF_MODNAME
  251. #define DPF_MODNAME "SetIPHostName"
  252. static HRESULT SetIPHostName( const HWND hDlg, const CEndpoint *const pEndpoint )
  253. {
  254. HRESULT hr;
  255. HWND hEditControl;
  256. //
  257. // initialize
  258. //
  259. hr = DPN_OK;
  260. hEditControl = GetDlgItem( hDlg, IDC_EDIT_IP_HOSTNAME );
  261. if ( hEditControl == NULL )
  262. {
  263. DWORD dwErrorCode;
  264. hr = DPNERR_GENERIC;
  265. dwErrorCode = GetLastError();
  266. DPFX(DPFPREP, 0, "Problem getting handle of hostname edit control!" );
  267. DisplayErrorCode( 0, dwErrorCode );
  268. goto Failure;
  269. }
  270. //
  271. // set edit field limit (this message does not have a return result)
  272. //
  273. SendMessage( hEditControl, EM_LIMITTEXT, TEMP_HOSTNAME_LENGTH, 0 );
  274. //
  275. // add string to dialog
  276. //
  277. if ( SetWindowText( hEditControl, TEXT("") ) == FALSE )
  278. {
  279. DWORD dwErrorCode;
  280. hr = DPNERR_OUTOFMEMORY;
  281. dwErrorCode = GetLastError();
  282. DPFX(DPFPREP, 0, "Problem setting IP hostname in dialog!" );
  283. DisplayErrorCode( 0, dwErrorCode );
  284. goto Failure;
  285. }
  286. Failure:
  287. return hr;
  288. }
  289. //**********************************************************************
  290. //**********************************************************************
  291. // ------------------------------
  292. // GetDialogData - set endpoint data from serial dialog
  293. //
  294. // Entry: Window handle
  295. // Pointer to endpoint
  296. //
  297. // Exit: Error code
  298. // ------------------------------
  299. #undef DPF_MODNAME
  300. #define DPF_MODNAME "GetDialogData"
  301. static HRESULT GetDialogData( HWND hDlg, CEndpoint *pEndpoint )
  302. {
  303. HRESULT hr;
  304. UINT_PTR uHostNameLength;
  305. TCHAR HostName[ TEMP_HOSTNAME_LENGTH ];
  306. HWND hEditControl;
  307. //
  308. // initialize
  309. //
  310. hr = DPN_OK;
  311. //
  312. // get control ID and then the host name
  313. //
  314. hEditControl = GetDlgItem( hDlg, IDC_EDIT_IP_HOSTNAME );
  315. if ( hEditControl == NULL )
  316. {
  317. DWORD dwErrorCode;
  318. DNASSERT( FALSE );
  319. hr = DPNERR_OUTOFMEMORY;
  320. dwErrorCode = GetLastError();
  321. DPFX(DPFPREP, 0, "Failed to get control handle when attempting to read IP hostname!" );
  322. DisplayDNError( 0, dwErrorCode );
  323. goto Failure;
  324. }
  325. //
  326. // Clear the error since Japanese Windows 9x does not seem to set it properly.
  327. //
  328. SetLastError(0);
  329. uHostNameLength = GetWindowText( hEditControl, HostName, LENGTHOF( HostName ) );
  330. if ( uHostNameLength == 0 )
  331. {
  332. DWORD dwErrorCode;
  333. //
  334. // zero, possible empty name or error
  335. //
  336. dwErrorCode = GetLastError();
  337. if ( dwErrorCode != ERROR_SUCCESS )
  338. {
  339. hr = DPNERR_OUTOFMEMORY;
  340. DPFX(DPFPREP, 0, "Failed to read hostname from dialog!" );
  341. DisplayErrorCode( 0, dwErrorCode );
  342. goto Failure;
  343. }
  344. }
  345. pEndpoint->SetTempHostName( HostName, uHostNameLength );
  346. Failure:
  347. return hr;
  348. }
  349. //**********************************************************************
  350. #endif // !DPNBUILD_NOSPUI