Source code of Windows XP (NT5)
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.

523 lines
14 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2001 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // WizardUtils.cpp
  7. //
  8. // Maintained By:
  9. // David Potter (DavidP) 30-JAN-2001
  10. //
  11. //////////////////////////////////////////////////////////////////////////////
  12. #include "Pch.h"
  13. #include "WizardUtils.h"
  14. //////////////////////////////////////////////////////////////////////////////
  15. //++
  16. //
  17. // HrValidateDnsHostname
  18. //
  19. // Description:
  20. // Validate a hostname with DNS. If the name contains a period (.)
  21. // it will be validated as a full DNS hostname. Otherwise it will be
  22. // validated as a hostname label.
  23. //
  24. // Arguments:
  25. // hwndParentIn
  26. // pcwszHostnameIn
  27. // emvdhoOptionsIn -- mvdhoALLOW_FULL_NAME
  28. //
  29. // Return Values:
  30. // S_OK - Operation completed successfully
  31. // Other HRESULT values from DnsValidateName().
  32. //
  33. // Remarks:
  34. //
  35. //--
  36. //////////////////////////////////////////////////////////////////////////////
  37. HRESULT
  38. HrValidateDnsHostname(
  39. HWND hwndParentIn
  40. , LPCWSTR pcwszHostnameIn
  41. , EValidateDnsHostnameOptions emvdhoOptionsIn
  42. )
  43. {
  44. TraceFunc1( "pcwszHostnameIn = '%1!ws!", pcwszHostnameIn );
  45. HRESULT hr = S_OK;
  46. DNS_STATUS dnsStatus;
  47. int iRet;
  48. UINT idsStatus = 0;
  49. UINT idsSubStatus = 0;
  50. UINT nMsgBoxType;
  51. bool fAllowFullName = ( ( emvdhoOptionsIn & mvdhoALLOW_FULL_NAME ) == mvdhoALLOW_FULL_NAME );
  52. Assert( pcwszHostnameIn != NULL );
  53. if ( fAllowFullName )
  54. {
  55. dnsStatus = TW32( DnsValidateName( pcwszHostnameIn, DnsNameHostnameFull ) );
  56. }
  57. else
  58. {
  59. dnsStatus = TW32( DnsValidateName( pcwszHostnameIn, DnsNameHostnameLabel ) );
  60. }
  61. if ( dnsStatus != ERROR_SUCCESS )
  62. {
  63. // Format the error message string for the message box.
  64. switch ( dnsStatus )
  65. {
  66. case ERROR_INVALID_NAME:
  67. idsStatus = IDS_ERR_INVALID_DNS_NAME_TEXT;
  68. if ( fAllowFullName )
  69. {
  70. idsSubStatus = IDS_ERR_FULL_DNS_NAME_INFO_TEXT;
  71. }
  72. else
  73. {
  74. idsSubStatus = IDS_ERR_DNS_HOSTNAME_LABEL_INFO_TEXT;
  75. }
  76. nMsgBoxType = MB_OK | MB_ICONSTOP;
  77. break;
  78. case DNS_ERROR_NON_RFC_NAME:
  79. idsStatus = 0;
  80. idsSubStatus = IDS_ERR_NON_RFC_NAME_QUERY;
  81. nMsgBoxType = MB_YESNO | MB_ICONQUESTION;
  82. break;
  83. case DNS_ERROR_NUMERIC_NAME:
  84. idsStatus = IDS_ERR_INVALID_DNS_NAME_TEXT;
  85. if ( fAllowFullName )
  86. {
  87. idsSubStatus = IDS_ERR_FULL_DNS_NAME_NUMERIC;
  88. }
  89. else
  90. {
  91. idsSubStatus = IDS_ERR_DNS_HOSTNAME_LABEL_NUMERIC;
  92. }
  93. nMsgBoxType = MB_OK | MB_ICONSTOP;
  94. break;
  95. case DNS_ERROR_INVALID_NAME_CHAR:
  96. default:
  97. idsStatus = 0;
  98. idsSubStatus = IDS_ERR_DNS_NAME_INVALID_CHAR;
  99. nMsgBoxType = MB_OK | MB_ICONSTOP;
  100. break;
  101. }
  102. // Display the error message box.
  103. if ( idsStatus == 0 )
  104. {
  105. hr = THR( HrMessageBoxWithStatus(
  106. hwndParentIn
  107. , IDS_ERR_VALIDATING_NAME_TITLE
  108. , IDS_ERR_VALIDATING_NAME_TEXT
  109. , dnsStatus
  110. , idsSubStatus
  111. , nMsgBoxType
  112. , &iRet
  113. , pcwszHostnameIn
  114. ) );
  115. }
  116. else
  117. {
  118. hr = THR( HrMessageBoxWithStatusString(
  119. hwndParentIn
  120. , IDS_ERR_VALIDATING_NAME_TITLE
  121. , IDS_ERR_VALIDATING_NAME_TEXT
  122. , idsStatus
  123. , idsSubStatus
  124. , nMsgBoxType
  125. , &iRet
  126. , pcwszHostnameIn
  127. ) );
  128. }
  129. if ( FAILED( hr ) )
  130. {
  131. goto Cleanup;
  132. }
  133. if ( iRet == IDYES )
  134. {
  135. dnsStatus = ERROR_SUCCESS;
  136. }
  137. } // if: error in validation
  138. Cleanup:
  139. if ( dnsStatus != ERROR_SUCCESS )
  140. {
  141. hr = HRESULT_FROM_WIN32( dnsStatus );
  142. }
  143. HRETURN( hr );
  144. } //*** HrValidateDnsHostname()
  145. //////////////////////////////////////////////////////////////////////////////
  146. //++
  147. //
  148. // HrMessageBoxWithStatus
  149. //
  150. // Description:
  151. // Display an error message box.
  152. //
  153. // Arguments:
  154. // hwndParentIn
  155. // idsTitleIn
  156. // idsOperationIn
  157. // hrStatusIn
  158. // idsSubStatusIn
  159. // uTypeIn
  160. // pidReturnOut -- IDABORT on error or any return value from MessageBox()
  161. // ...
  162. //
  163. // Return Values:
  164. // Any return values from the MessageBox() Win32 API.
  165. //
  166. // Remarks:
  167. //
  168. //--
  169. //////////////////////////////////////////////////////////////////////////////
  170. HRESULT
  171. HrMessageBoxWithStatus(
  172. HWND hwndParentIn
  173. , UINT idsTitleIn
  174. , UINT idsOperationIn
  175. , HRESULT hrStatusIn
  176. , UINT idsSubStatusIn
  177. , UINT uTypeIn
  178. , int * pidReturnOut
  179. , ...
  180. )
  181. {
  182. TraceFunc( "" );
  183. HRESULT hr = S_OK;
  184. int idReturn = IDABORT; // Default in case of error.
  185. BSTR bstrTitle = NULL;
  186. BSTR bstrOperation = NULL;
  187. BSTR bstrStatus = NULL;
  188. BSTR bstrSubStatus = NULL;
  189. BSTR bstrFullText = NULL;
  190. va_list valist;
  191. va_start( valist, pidReturnOut );
  192. // Load the title string if one is specified.
  193. if ( idsTitleIn != 0 )
  194. {
  195. hr = THR( HrLoadStringIntoBSTR( g_hInstance, idsTitleIn, &bstrTitle ) );
  196. if ( FAILED( hr ) )
  197. {
  198. goto Cleanup;
  199. }
  200. }
  201. // Load the text string.
  202. hr = THR( HrFormatStringWithVAListIntoBSTR( g_hInstance, idsOperationIn, &bstrOperation, valist ) );
  203. if ( FAILED( hr ) )
  204. {
  205. goto Cleanup;
  206. }
  207. // Format the status.
  208. hr = THR( HrFormatErrorIntoBSTR( hrStatusIn, &bstrStatus ) );
  209. if ( FAILED( hr ) )
  210. {
  211. goto Cleanup;
  212. }
  213. // Load the substatus string if specified.
  214. if ( idsSubStatusIn != 0 )
  215. {
  216. hr = THR( HrLoadStringIntoBSTR( g_hInstance, idsSubStatusIn, &bstrSubStatus ) );
  217. if ( FAILED( hr ) )
  218. {
  219. goto Cleanup;
  220. }
  221. }
  222. // Format all the strings into a single string.
  223. if ( bstrSubStatus == NULL )
  224. {
  225. hr = THR( HrFormatStringIntoBSTR(
  226. L"%1!ws!\n\n%2!ws!"
  227. , &bstrFullText
  228. , bstrOperation
  229. , bstrStatus
  230. ) );
  231. }
  232. else
  233. {
  234. hr = THR( HrFormatStringIntoBSTR(
  235. L"%1!ws!\n\n%2!ws!\n\n%3!ws!"
  236. , &bstrFullText
  237. , bstrOperation
  238. , bstrStatus
  239. , bstrSubStatus
  240. ) );
  241. }
  242. if ( FAILED( hr ) )
  243. {
  244. goto Cleanup;
  245. }
  246. // Display the status.
  247. idReturn = MessageBox( hwndParentIn, bstrFullText, bstrTitle, uTypeIn );
  248. Cleanup:
  249. TraceSysFreeString( bstrTitle );
  250. TraceSysFreeString( bstrOperation );
  251. TraceSysFreeString( bstrStatus );
  252. TraceSysFreeString( bstrSubStatus );
  253. TraceSysFreeString( bstrFullText );
  254. va_end( valist );
  255. if ( pidReturnOut != NULL )
  256. {
  257. *pidReturnOut = idReturn;
  258. }
  259. HRETURN( hr );
  260. } //*** HrMessageBoxWithStatus( hrStatusIn )
  261. //////////////////////////////////////////////////////////////////////////////
  262. //++
  263. //
  264. // HrMessageBoxWithStatusString
  265. //
  266. // Description:
  267. // Display an error message box.
  268. //
  269. // Arguments:
  270. // hwndParentIn
  271. // idsTitleIn
  272. // idsOperationIn
  273. // idsStatusIn
  274. // idsSubStatusIn
  275. // uTypeIn
  276. // pidReturnOut -- IDABORT on error or any return value from MessageBox()
  277. // ...
  278. //
  279. // Return Values:
  280. // Any return values from the MessageBox() Win32 API.
  281. //
  282. // Remarks:
  283. //
  284. //--
  285. //////////////////////////////////////////////////////////////////////////////
  286. HRESULT
  287. HrMessageBoxWithStatusString(
  288. HWND hwndParentIn
  289. , UINT idsTitleIn
  290. , UINT idsOperationIn
  291. , UINT idsStatusIn
  292. , UINT idsSubStatusIn
  293. , UINT uTypeIn
  294. , int * pidReturnOut
  295. , ...
  296. )
  297. {
  298. TraceFunc( "" );
  299. HRESULT hr = S_OK;
  300. int idReturn = IDABORT; // Default in case of error.
  301. BSTR bstrTitle = NULL;
  302. BSTR bstrOperation = NULL;
  303. BSTR bstrStatus = NULL;
  304. BSTR bstrSubStatus = NULL;
  305. BSTR bstrFullText = NULL;
  306. va_list valist;
  307. va_start( valist, pidReturnOut );
  308. // Load the title string if one is specified.
  309. if ( idsTitleIn != 0 )
  310. {
  311. hr = THR( HrLoadStringIntoBSTR( g_hInstance, idsTitleIn, &bstrTitle ) );
  312. if ( FAILED( hr ) )
  313. {
  314. goto Cleanup;
  315. }
  316. }
  317. // Load the text string.
  318. hr = THR( HrFormatStringWithVAListIntoBSTR( g_hInstance, idsOperationIn, &bstrOperation, valist ) );
  319. if ( FAILED( hr ) )
  320. {
  321. goto Cleanup;
  322. }
  323. // Format the status.
  324. hr = THR( HrLoadStringIntoBSTR( g_hInstance, idsStatusIn, &bstrStatus ) );
  325. if ( FAILED( hr ) )
  326. {
  327. goto Cleanup;
  328. }
  329. // Load the substatus string if specified.
  330. if ( idsSubStatusIn != 0 )
  331. {
  332. hr = THR( HrLoadStringIntoBSTR( g_hInstance, idsSubStatusIn, &bstrSubStatus ) );
  333. if ( FAILED( hr ) )
  334. {
  335. goto Cleanup;
  336. }
  337. }
  338. // Format all the strings into a single string.
  339. if ( bstrSubStatus == NULL )
  340. {
  341. hr = THR( HrFormatStringIntoBSTR(
  342. L"%1!ws!\n\n%2!ws!"
  343. , &bstrFullText
  344. , bstrOperation
  345. , bstrStatus
  346. ) );
  347. }
  348. else
  349. {
  350. hr = THR( HrFormatStringIntoBSTR(
  351. L"%1!ws!\n\n%2!ws!\n\n%3!ws!"
  352. , &bstrFullText
  353. , bstrOperation
  354. , bstrStatus
  355. , bstrSubStatus
  356. ) );
  357. }
  358. if ( FAILED( hr ) )
  359. {
  360. goto Cleanup;
  361. }
  362. // Display the status.
  363. idReturn = MessageBox( hwndParentIn, bstrFullText, bstrTitle, uTypeIn );
  364. Cleanup:
  365. TraceSysFreeString( bstrTitle );
  366. TraceSysFreeString( bstrOperation );
  367. TraceSysFreeString( bstrStatus );
  368. TraceSysFreeString( bstrSubStatus );
  369. TraceSysFreeString( bstrFullText );
  370. va_end( valist );
  371. if ( pidReturnOut != NULL )
  372. {
  373. *pidReturnOut = idReturn;
  374. }
  375. HRETURN( hr );
  376. } //*** HrMessageBoxWithStatusString( idsStatusTextIn )
  377. //////////////////////////////////////////////////////////////////////////////
  378. //++
  379. //
  380. // HrViewLogFile
  381. //
  382. // Description:
  383. // View the log file.
  384. //
  385. // Arguments:
  386. // hwndParentIn
  387. //
  388. // Return Values:
  389. // S_OK - Operation completed successfully
  390. // Other HRESULT values from ShellExecute().
  391. //
  392. // Remarks:
  393. //
  394. //--
  395. //////////////////////////////////////////////////////////////////////////////
  396. HRESULT
  397. HrViewLogFile(
  398. HWND hwndParentIn
  399. )
  400. {
  401. TraceFunc( "" );
  402. static const WCHAR s_szVerb[] = L"open";
  403. static const WCHAR s_szLogFileName[] = L"%windir%\\system32\\LogFiles\\Cluster\\ClCfgSrv.log";
  404. HRESULT hr = S_OK;
  405. DWORD sc;
  406. DWORD cch;
  407. DWORD cchRet;
  408. LPWSTR pszFile = NULL;
  409. //
  410. // Expand environment variables in the file to open.
  411. //
  412. // Get the size of the output buffer.
  413. cch = 0;
  414. cchRet = ExpandEnvironmentStrings( s_szLogFileName, NULL, cch );
  415. if ( cchRet == 0 )
  416. {
  417. sc = TW32( GetLastError() );
  418. goto Win32Error;
  419. } // if: error getting length of the expansion string
  420. // Allocate the output buffer.
  421. cch = cchRet;
  422. pszFile = new WCHAR[ cch ];
  423. if ( pszFile == NULL )
  424. {
  425. sc = TW32( ERROR_OUTOFMEMORY );
  426. goto Win32Error;
  427. }
  428. // Expand the string into the output buffer.
  429. cchRet = ExpandEnvironmentStrings( s_szLogFileName, pszFile, cch );
  430. if ( cchRet == 0 )
  431. {
  432. sc = TW32( GetLastError() );
  433. goto Win32Error;
  434. }
  435. Assert( cchRet == cch );
  436. //
  437. // Execute the file.
  438. //
  439. sc = HandleToULong( ShellExecute(
  440. hwndParentIn // hwnd
  441. , s_szVerb // lpVerb
  442. , pszFile // lpFile
  443. , NULL // lpParameters
  444. , NULL // lpDirectory
  445. , SW_SHOWNORMAL // nShowCommand
  446. ) );
  447. if ( sc < 32 )
  448. {
  449. // Values less than 32 indicate an error occurred.
  450. TW32( sc );
  451. goto Win32Error;
  452. } // if: error executing the file
  453. goto Cleanup;
  454. Win32Error:
  455. THR( HrMessageBoxWithStatus(
  456. hwndParentIn
  457. , IDS_ERR_VIEW_LOG_TITLE
  458. , IDS_ERR_VIEW_LOG_TEXT
  459. , sc
  460. , 0 // idsSubStatusIn
  461. , ( MB_OK
  462. | MB_ICONEXCLAMATION )
  463. , NULL // pidReturnOut
  464. , s_szLogFileName
  465. ) );
  466. hr = HRESULT_FROM_WIN32( sc );
  467. goto Cleanup;
  468. Cleanup:
  469. HRETURN( hr );
  470. } //*** HrViewLogFile()