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.

1058 lines
31 KiB

  1. /*++
  2. Copyright (C) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. GLOBALS.CPP
  5. Abstract:
  6. Utility methods for the Performance Logs and Alerts MMC snap-in.
  7. --*/
  8. #include "stdAfx.h"
  9. #include <pdhmsg.h> // For CreateSampleFileName
  10. #include <pdhp.h> // For CreateSampleFileName
  11. #include "smcfgmsg.h"
  12. #include "globals.h"
  13. USE_HANDLE_MACROS("SMLOGCFG(globals.cpp)");
  14. extern "C" {
  15. WCHAR GUIDSTR_TypeLibrary[] = {_T("{7478EF60-8C46-11d1-8D99-00A0C913CAD4}")};
  16. WCHAR GUIDSTR_ComponentData[] = {_T("{7478EF61-8C46-11d1-8D99-00A0C913CAD4}")};
  17. WCHAR GUIDSTR_Component[] = {_T("{7478EF62-8C46-11d1-8D99-00A0C913CAD4}")};
  18. WCHAR GUIDSTR_RootNode[] = {_T("{7478EF63-8C46-11d1-8D99-00A0C913CAD4}")};
  19. WCHAR GUIDSTR_MainNode[] = {_T("{7478EF64-8C46-11d1-8D99-00A0C913CAD4}")}; // Obsolete after Beta 3
  20. WCHAR GUIDSTR_SnapInExt[] = {_T("{7478EF65-8C46-11d1-8D99-00A0C913CAD4}")};
  21. WCHAR GUIDSTR_CounterMainNode[] = {_T("{7478EF66-8C46-11d1-8D99-00A0C913CAD4}")};
  22. WCHAR GUIDSTR_TraceMainNode[] = {_T("{7478EF67-8C46-11d1-8D99-00A0C913CAD4}")};
  23. WCHAR GUIDSTR_AlertMainNode[] = {_T("{7478EF68-8C46-11d1-8D99-00A0C913CAD4}")};
  24. WCHAR GUIDSTR_PerformanceAbout[] = {_T("{7478EF69-8C46-11d1-8D99-00A0C913CAD4}")};
  25. };
  26. HINSTANCE g_hinst; // Global instance handle
  27. CRITICAL_SECTION g_critsectInstallDefaultQueries;
  28. const COMBO_BOX_DATA_MAP TimeUnitCombo[] =
  29. {
  30. {SLQ_TT_UTYPE_SECONDS, IDS_SECONDS},
  31. {SLQ_TT_UTYPE_MINUTES, IDS_MINUTES},
  32. {SLQ_TT_UTYPE_HOURS, IDS_HOURS},
  33. {SLQ_TT_UTYPE_DAYS, IDS_DAYS}
  34. };
  35. const DWORD dwTimeUnitComboEntries = sizeof(TimeUnitCombo)/sizeof(TimeUnitCombo[0]);
  36. //---------------------------------------------------------------------------
  37. // Returns the current object based on the s_cfMmcMachineName clipboard format
  38. //
  39. CDataObject*
  40. ExtractOwnDataObject
  41. (
  42. LPDATAOBJECT lpDataObject // [in] IComponent pointer
  43. )
  44. {
  45. HGLOBAL hGlobal;
  46. HRESULT hr = S_OK;
  47. CDataObject* pDO = NULL;
  48. hr = ExtractFromDataObject( lpDataObject,
  49. CDataObject::s_cfInternal,
  50. sizeof(CDataObject **),
  51. &hGlobal
  52. );
  53. if( SUCCEEDED(hr) )
  54. {
  55. pDO = *(CDataObject **)(hGlobal);
  56. ASSERT( NULL != pDO );
  57. VERIFY ( NULL == GlobalFree(hGlobal) ); // Must return NULL
  58. }
  59. return pDO;
  60. } // end ExtractOwnDataObject()
  61. //---------------------------------------------------------------------------
  62. // Extracts data based on the passed-in clipboard format
  63. //
  64. HRESULT
  65. ExtractFromDataObject
  66. (
  67. LPDATAOBJECT lpDataObject, // [in] Points to data object
  68. UINT cfClipFormat, // [in] Clipboard format to use
  69. ULONG nByteCount, // [in] Number of bytes to allocate
  70. HGLOBAL *phGlobal // [out] Points to the data we want
  71. )
  72. {
  73. ASSERT( NULL != lpDataObject );
  74. HRESULT hr = S_OK;
  75. STGMEDIUM stgmedium = { TYMED_HGLOBAL, NULL };
  76. FORMATETC formatetc = { (USHORT)cfClipFormat, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  77. *phGlobal = NULL;
  78. do
  79. {
  80. // Allocate memory for the stream
  81. stgmedium.hGlobal = GlobalAlloc( GMEM_SHARE, nByteCount );
  82. if( !stgmedium.hGlobal )
  83. {
  84. hr = E_OUTOFMEMORY;
  85. LOCALTRACE( _T("Out of memory\n") );
  86. break;
  87. }
  88. // Attempt to get data from the object
  89. hr = lpDataObject->GetDataHere( &formatetc, &stgmedium );
  90. if (FAILED(hr))
  91. {
  92. break;
  93. }
  94. // stgmedium now has the data we need
  95. *phGlobal = stgmedium.hGlobal;
  96. stgmedium.hGlobal = NULL;
  97. } while (0);
  98. if (FAILED(hr) && stgmedium.hGlobal)
  99. {
  100. VERIFY ( NULL == GlobalFree(stgmedium.hGlobal)); // Must return NULL
  101. }
  102. return hr;
  103. } // end ExtractFromDataObject()
  104. //---------------------------------------------------------------------------
  105. //
  106. VOID DisplayError( LONG nErrorCode, LPWSTR wszDlgTitle )
  107. {
  108. LPVOID lpMsgBuf = NULL;
  109. ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  110. NULL,
  111. nErrorCode,
  112. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  113. (LPWSTR)&lpMsgBuf,
  114. 0,
  115. NULL
  116. );
  117. if (lpMsgBuf) {
  118. ::MessageBox( NULL, (LPWSTR)lpMsgBuf, wszDlgTitle,
  119. MB_OK|MB_ICONINFORMATION );
  120. LocalFree( lpMsgBuf );
  121. }
  122. } // end DisplayError()
  123. VOID DisplayError( LONG nErrorCode, UINT nTitleString )
  124. {
  125. CString strTitle;
  126. LPVOID lpMsgBuf = NULL;
  127. ResourceStateManager rsm;
  128. ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  129. NULL,
  130. nErrorCode,
  131. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  132. (LPWSTR)&lpMsgBuf,
  133. 0,
  134. NULL
  135. );
  136. strTitle.LoadString ( nTitleString );
  137. if (lpMsgBuf) {
  138. ::MessageBox( NULL, (LPWSTR)lpMsgBuf, (LPCWSTR)strTitle,
  139. MB_OK|MB_ICONINFORMATION );
  140. LocalFree( lpMsgBuf );
  141. }
  142. } // end DisplayError()
  143. //---------------------------------------------------------------------------
  144. // Debug only message box
  145. //
  146. int DebugMsg( LPWSTR wszMsg, LPWSTR wszTitle )
  147. {
  148. int nRetVal = 0;
  149. wszMsg;
  150. wszTitle;
  151. #ifdef _DEBUG
  152. nRetVal = ::MessageBox( NULL, wszMsg, wszTitle, MB_OK );
  153. #endif
  154. return nRetVal;
  155. }
  156. //---------------------------------------------------------------------------
  157. // Extracts data based on the passed-in clipboard format
  158. HRESULT ExtractObjectTypeGUID( IDataObject* piDataObject, GUID* pguidObjectType )
  159. {
  160. HGLOBAL hGlobal;
  161. HRESULT hr = S_OK;
  162. hr = ExtractFromDataObject( piDataObject,
  163. CDataObject::s_cfNodeType,
  164. sizeof(GUID),
  165. &hGlobal
  166. );
  167. if( SUCCEEDED(hr) )
  168. {
  169. *pguidObjectType = *(GUID*)(hGlobal);
  170. ASSERT( NULL != pguidObjectType );
  171. VERIFY ( NULL == GlobalFree(hGlobal) ); // Must return NULL
  172. }
  173. return hr;
  174. }
  175. HRESULT
  176. ExtractMachineName(
  177. IDataObject* piDataObject,
  178. CString& rstrMachineName )
  179. {
  180. HRESULT hr = S_OK;
  181. HGLOBAL hMachineName;
  182. hr = ExtractFromDataObject(piDataObject,
  183. CDataObject::s_cfMmcMachineName,
  184. sizeof(TCHAR) * (MAX_PATH + 1),
  185. &hMachineName);
  186. if( SUCCEEDED(hr) )
  187. {
  188. LPWSTR pszNewData = reinterpret_cast<LPWSTR>(hMachineName);
  189. if (NULL == pszNewData)
  190. {
  191. ASSERT(FALSE);
  192. hr = E_UNEXPECTED;
  193. } else {
  194. pszNewData[MAX_PATH] = _T('\0'); // just to be safe
  195. // USES_CONVERSION;
  196. // rstrMachineName = OLE2T(pszNewData);
  197. rstrMachineName = pszNewData;
  198. VERIFY ( NULL == GlobalFree(hMachineName) ); // Must return NULL
  199. }
  200. }
  201. return hr;
  202. }
  203. DWORD __stdcall
  204. CreateSampleFileName (
  205. const CString& rstrQueryName,
  206. const CString& rstrMachineName,
  207. const CString& rstrFolderName,
  208. const CString& rstrInputBaseName,
  209. const CString& rstrSqlName,
  210. DWORD dwSuffixValue,
  211. DWORD dwLogFileTypeValue,
  212. DWORD dwCurrentSerialNumber,
  213. CString& rstrReturnName)
  214. {
  215. DWORD dwStatus = ERROR_SUCCESS;
  216. PPDH_PLA_INFO pInfo = NULL;
  217. DWORD dwStrBufLen = 0;
  218. DWORD dwInfoSize = 0;
  219. DWORD dwFlags = 0;
  220. rstrReturnName.Empty();
  221. dwStatus = PdhPlaGetInfo(
  222. (LPTSTR)(LPCTSTR)rstrQueryName,
  223. (LPTSTR)(LPCTSTR)rstrMachineName,
  224. &dwInfoSize,
  225. pInfo );
  226. if( ERROR_SUCCESS == dwStatus && 0 != dwInfoSize ){
  227. pInfo = (PPDH_PLA_INFO)malloc(dwInfoSize);
  228. if( NULL != pInfo && (sizeof(PDH_PLA_INFO) <= dwInfoSize) ){
  229. ZeroMemory( pInfo, dwInfoSize );
  230. pInfo->dwMask = PLA_INFO_CREATE_FILENAME;
  231. dwStatus = PdhPlaGetInfo(
  232. (LPTSTR)(LPCTSTR)rstrQueryName,
  233. (LPTSTR)(LPCTSTR)rstrMachineName,
  234. &dwInfoSize,
  235. pInfo );
  236. pInfo->dwMask = PLA_INFO_CREATE_FILENAME;
  237. pInfo->dwFileFormat = dwLogFileTypeValue;
  238. pInfo->strBaseFileName = (LPTSTR)(LPCTSTR)rstrInputBaseName;
  239. pInfo->dwAutoNameFormat = dwSuffixValue;
  240. // PLA_INFO_FLAG_TYPE is counter log vs trace log vs alert
  241. pInfo->strDefaultDir = (LPTSTR)(LPCTSTR)rstrFolderName;
  242. pInfo->dwLogFileSerialNumber = dwCurrentSerialNumber;
  243. pInfo->strSqlName = (LPTSTR)(LPCTSTR)rstrSqlName;
  244. pInfo->dwLogFileSerialNumber = dwCurrentSerialNumber;
  245. // Create file name based on passed parameters only.
  246. dwFlags = PLA_FILENAME_CREATEONLY; // PLA_FILENAME_CURRENTLOG for latest run log
  247. dwStatus = PdhPlaGetLogFileName (
  248. (LPTSTR)(LPCTSTR)rstrQueryName,
  249. (LPTSTR)(LPCTSTR)rstrMachineName,
  250. pInfo,
  251. dwFlags,
  252. &dwStrBufLen,
  253. NULL );
  254. if ( ERROR_SUCCESS == dwStatus || PDH_INSUFFICIENT_BUFFER == dwStatus ) {
  255. dwStatus = PdhPlaGetLogFileName (
  256. (LPTSTR)(LPCTSTR)rstrQueryName,
  257. (LPTSTR)(LPCTSTR)rstrMachineName,
  258. pInfo,
  259. dwFlags,
  260. &dwStrBufLen,
  261. rstrReturnName.GetBufferSetLength ( dwStrBufLen ) );
  262. rstrReturnName.ReleaseBuffer();
  263. }
  264. }
  265. }
  266. if ( NULL != pInfo ) {
  267. free( pInfo );
  268. }
  269. return dwStatus;
  270. }
  271. DWORD __stdcall
  272. IsDirPathValid (
  273. CString& rstrPath,
  274. BOOL bLastNameIsDirectory,
  275. BOOL bCreateMissingDirs,
  276. BOOL& rbIsValid )
  277. /*++
  278. Routine Description:
  279. Creates the directory specified in szPath and any other "higher"
  280. directories in the specified path that don't exist.
  281. Arguments:
  282. IN CString rstrPath
  283. directory path to create (assumed to be a DOS path, not a UNC)
  284. IN BOOL bLastNameIsDirectory
  285. TRUE when the last name in the path is a Directory and not a File
  286. FALSE if the last name is a file
  287. IN BOOL bCreateMissingDirs
  288. TRUE will create any dirs in the path that are not found
  289. FALSE will only test for existence and not create any
  290. missing dirs.
  291. OUT BOOL rbIsValid
  292. TRUE if the directory path now exists
  293. FALSE if error (GetLastError to find out why)
  294. Return Value:
  295. DWSTATUS
  296. --*/
  297. {
  298. CString strLocalPath;
  299. LPTSTR szLocalPath;
  300. LPTSTR szEnd;
  301. LPSECURITY_ATTRIBUTES lpSA = NULL;
  302. DWORD dwAttr;
  303. TCHAR cBackslash = TEXT('\\');
  304. DWORD dwStatus = ERROR_SUCCESS;
  305. rbIsValid = FALSE;
  306. szLocalPath = strLocalPath.GetBufferSetLength ( MAX_PATH );
  307. if ( NULL == szLocalPath ) {
  308. dwStatus = ERROR_OUTOFMEMORY;
  309. } else {
  310. if (GetFullPathName (
  311. rstrPath,
  312. MAX_PATH,
  313. szLocalPath,
  314. NULL) > 0) {
  315. szEnd = &szLocalPath[3];
  316. if (*szEnd != 0) {
  317. // then there are sub dirs to create
  318. while (*szEnd != 0) {
  319. // go to next backslash
  320. while ((*szEnd != cBackslash) && (*szEnd != 0)) szEnd++;
  321. if (*szEnd == cBackslash) {
  322. // terminate path here and create directory
  323. *szEnd = 0;
  324. if (bCreateMissingDirs) {
  325. if (!CreateDirectory (szLocalPath, lpSA)) {
  326. // see what the error was and "adjust" it if necessary
  327. dwStatus = GetLastError();
  328. if ( ERROR_ALREADY_EXISTS == dwStatus ) {
  329. // this is OK
  330. dwStatus = ERROR_SUCCESS;
  331. rbIsValid = TRUE;
  332. } else {
  333. rbIsValid = FALSE;
  334. }
  335. } else {
  336. // directory created successfully so update count
  337. rbIsValid = TRUE;
  338. }
  339. } else {
  340. if ((dwAttr = GetFileAttributes(szLocalPath)) != 0xFFFFFFFF) {
  341. // make sure it's a dir
  342. if ((dwAttr & FILE_ATTRIBUTE_DIRECTORY) ==
  343. FILE_ATTRIBUTE_DIRECTORY) {
  344. rbIsValid = TRUE;
  345. } else {
  346. // if any dirs fail, then clear the return value
  347. rbIsValid = FALSE;
  348. }
  349. } else {
  350. // if any dirs fail, then clear the return value
  351. rbIsValid = FALSE;
  352. }
  353. }
  354. // replace backslash and go to next dir
  355. *szEnd++ = cBackslash;
  356. }
  357. }
  358. // create last dir in path now if it's a dir name and not a filename
  359. if (bLastNameIsDirectory) {
  360. if (bCreateMissingDirs) {
  361. if (!CreateDirectory (szLocalPath, lpSA)) {
  362. // see what the error was and "adjust" it if necessary
  363. dwStatus = GetLastError();
  364. if ( ERROR_ALREADY_EXISTS == dwStatus ) {
  365. // this is OK
  366. dwStatus = ERROR_SUCCESS;
  367. rbIsValid = TRUE;
  368. } else {
  369. rbIsValid = FALSE;
  370. }
  371. } else {
  372. // directory created successfully
  373. rbIsValid = TRUE;
  374. }
  375. } else {
  376. if ((dwAttr = GetFileAttributes(szLocalPath)) != 0xFFFFFFFF) {
  377. // make sure it's a dir
  378. if ((dwAttr & FILE_ATTRIBUTE_DIRECTORY) ==
  379. FILE_ATTRIBUTE_DIRECTORY) {
  380. rbIsValid = TRUE;
  381. } else {
  382. // if any dirs fail, then clear the return value
  383. rbIsValid = FALSE;
  384. }
  385. } else {
  386. // if any dirs fail, then clear the return value
  387. rbIsValid = FALSE;
  388. }
  389. }
  390. }
  391. } else {
  392. // else this is a root dir only so return success.
  393. dwStatus = ERROR_SUCCESS;
  394. rbIsValid = TRUE;
  395. }
  396. }
  397. strLocalPath.ReleaseBuffer();
  398. }
  399. return dwStatus;
  400. }
  401. DWORD __stdcall
  402. ProcessDirPath (
  403. CString& rstrPath,
  404. const CString& rstrLogName,
  405. CWnd* pwndParent,
  406. BOOL& rbIsValid,
  407. BOOL bOnFilesPage )
  408. {
  409. DWORD dwStatus = ERROR_SUCCESS;
  410. DWORD cchLen = 0;
  411. CString strExpanded;
  412. LPWSTR szExpanded;
  413. DWORD cchExpandedLen;
  414. ResourceStateManager rsm;
  415. // Parse all environment symbols
  416. cchLen = ExpandEnvironmentStrings ( rstrPath, NULL, 0 );
  417. if ( 0 < cchLen ) {
  418. MFC_TRY
  419. //
  420. // CString size does not include NULL.
  421. // cchLen includes NULL. Include NULL count for safety.
  422. //
  423. szExpanded = strExpanded.GetBuffer ( cchLen );
  424. MFC_CATCH_DWSTATUS;
  425. if ( ERROR_SUCCESS == dwStatus ) {
  426. cchExpandedLen = ExpandEnvironmentStrings (
  427. rstrPath,
  428. szExpanded,
  429. cchLen);
  430. if ( 0 == cchExpandedLen ) {
  431. dwStatus = GetLastError();
  432. }
  433. }
  434. strExpanded.ReleaseBuffer();
  435. } else {
  436. dwStatus = GetLastError();
  437. }
  438. dwStatus = IsDirPathValid (strExpanded, TRUE, FALSE, rbIsValid);
  439. if ( ERROR_SUCCESS != dwStatus ) {
  440. rbIsValid = FALSE;
  441. } else {
  442. if ( !rbIsValid ) {
  443. INT nMbReturn;
  444. CString strMessage;
  445. strMessage.Format ( IDS_FILE_DIR_NOT_FOUND, rstrPath );
  446. nMbReturn = pwndParent->MessageBox ( strMessage, rstrLogName, MB_YESNO | MB_ICONWARNING );
  447. if (nMbReturn == IDYES) {
  448. // create the dir(s)
  449. dwStatus = IsDirPathValid (strExpanded, TRUE, TRUE, rbIsValid);
  450. if (ERROR_SUCCESS != dwStatus || !rbIsValid ) {
  451. // unable to create the dir, display message
  452. if ( bOnFilesPage ) {
  453. strMessage.Format ( IDS_FILE_DIR_NOT_MADE, rstrPath );
  454. } else {
  455. strMessage.Format ( IDS_DIR_NOT_MADE, rstrPath );
  456. }
  457. nMbReturn = pwndParent->MessageBox ( strMessage, rstrLogName, MB_OK | MB_ICONERROR);
  458. rbIsValid = FALSE;
  459. }
  460. } else if ( IDNO == nMbReturn ) {
  461. // then abort and return to the dialog
  462. if ( bOnFilesPage ) {
  463. strMessage.LoadString ( IDS_FILE_DIR_CREATE_CANCEL );
  464. } else {
  465. strMessage.LoadString ( IDS_DIR_CREATE_CANCEL );
  466. }
  467. nMbReturn = pwndParent->MessageBox ( strMessage, rstrLogName, MB_OK | MB_ICONINFORMATION);
  468. rbIsValid = FALSE;
  469. }
  470. } // else the path is OK
  471. }
  472. return dwStatus;
  473. }
  474. DWORD __stdcall
  475. IsCommandFilePathValid (
  476. CString& rstrPath )
  477. {
  478. DWORD dwStatus = ERROR_SUCCESS;
  479. ResourceStateManager rsm;
  480. if ( !rstrPath.IsEmpty() ) {
  481. HANDLE hOpenFile;
  482. hOpenFile = CreateFile (
  483. rstrPath,
  484. GENERIC_READ,
  485. 0, // Not shared
  486. NULL, // Security attributes
  487. OPEN_EXISTING, //
  488. FILE_ATTRIBUTE_NORMAL,
  489. NULL );
  490. if ( ( NULL == hOpenFile )
  491. || INVALID_HANDLE_VALUE == hOpenFile ) {
  492. dwStatus = SMCFG_NO_COMMAND_FILE_FOUND;
  493. } else {
  494. CloseHandle(hOpenFile);
  495. }
  496. } else {
  497. dwStatus = SMCFG_NO_COMMAND_FILE_FOUND;
  498. }
  499. return dwStatus;
  500. }
  501. INT __stdcall
  502. BrowseCommandFilename (
  503. CWnd* pwndParent,
  504. CString& rstrFilename )
  505. {
  506. INT iReturn = IDCANCEL;
  507. OPENFILENAME ofn;
  508. CString strInitialDir;
  509. WCHAR szFileName[MAX_PATH];
  510. WCHAR szDrive[MAX_PATH];
  511. WCHAR szDir[MAX_PATH];
  512. WCHAR szExt[MAX_PATH];
  513. WCHAR szFileFilter[MAX_PATH];
  514. LPWSTR szNextFilter;
  515. CString strTemp;
  516. ResourceStateManager rsm;
  517. _wsplitpath((LPCWSTR)rstrFilename,
  518. szDrive, szDir, szFileName, szExt);
  519. strInitialDir = szDrive;
  520. strInitialDir += szDir;
  521. lstrcat (szFileName, szExt);
  522. ZeroMemory( &ofn, sizeof( OPENFILENAME ) );
  523. ofn.lStructSize = sizeof(ofn);
  524. ofn.hwndOwner = pwndParent->m_hWnd;
  525. ofn.hInstance = GetModuleHandle(NULL);
  526. // load the file filter MSZ
  527. szNextFilter = &szFileFilter[0];
  528. strTemp.LoadString ( IDS_BROWSE_CMD_FILE_FILTER1 );
  529. lstrcpyW (szNextFilter, (LPCWSTR)strTemp);
  530. szNextFilter += strTemp.GetLength();
  531. *szNextFilter++ = 0;
  532. strTemp.LoadString ( IDS_BROWSE_CMD_FILE_FILTER2 );
  533. lstrcpyW (szNextFilter, (LPCWSTR)strTemp);
  534. szNextFilter += strTemp.GetLength();
  535. *szNextFilter++ = 0;
  536. strTemp.LoadString ( IDS_BROWSE_CMD_FILE_FILTER3 );
  537. lstrcpyW (szNextFilter, (LPCWSTR)strTemp);
  538. szNextFilter += strTemp.GetLength();
  539. *szNextFilter++ = 0;
  540. strTemp.LoadString ( IDS_BROWSE_CMD_FILE_FILTER4 );
  541. lstrcpyW (szNextFilter, (LPCWSTR)strTemp);
  542. szNextFilter += strTemp.GetLength();
  543. *szNextFilter++ = 0;
  544. *szNextFilter++ = 0; // msz terminator
  545. ofn.lpstrFilter = szFileFilter;
  546. ofn.lpstrCustomFilter = NULL;
  547. ofn.nMaxCustFilter = 0;
  548. ofn.nFilterIndex = 1; // nFilterIndex is 1-based
  549. ofn.lpstrFile = szFileName;
  550. ofn.nMaxFile = MAX_PATH;
  551. ofn.lpstrFileTitle = NULL;
  552. ofn.nMaxFileTitle = 0;
  553. ofn.lpstrInitialDir = (LPCTSTR)strInitialDir;
  554. strTemp.LoadString( IDS_BROWSE_CMD_FILE_CAPTION );
  555. ofn.lpstrTitle = (LPCWSTR)strTemp;
  556. ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
  557. ofn.nFileOffset = 0;
  558. ofn.nFileExtension = 0;
  559. ofn.lpstrDefExt = NULL;
  560. ofn.lCustData = 0;
  561. ofn.lpfnHook = NULL;
  562. ofn.lpTemplateName = NULL;
  563. iReturn = GetOpenFileName (&ofn);
  564. if ( IDOK == iReturn ) {
  565. // Update the fields with the new information
  566. rstrFilename = szFileName;
  567. } // else ignore if they canceled out
  568. return iReturn;
  569. }
  570. DWORD __stdcall
  571. FormatSmLogCfgMessage (
  572. CString& rstrMessage,
  573. HINSTANCE hResourceHandle,
  574. UINT uiMessageId,
  575. ... )
  576. {
  577. DWORD dwStatus = ERROR_SUCCESS;
  578. LPTSTR lpszTemp = NULL;
  579. // format message into temporary buffer lpszTemp
  580. va_list argList;
  581. va_start(argList, uiMessageId);
  582. dwStatus = ::FormatMessage (
  583. FORMAT_MESSAGE_FROM_HMODULE
  584. | FORMAT_MESSAGE_ALLOCATE_BUFFER
  585. | FORMAT_MESSAGE_MAX_WIDTH_MASK,
  586. hResourceHandle,
  587. uiMessageId,
  588. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  589. (LPTSTR)&lpszTemp,
  590. 0,
  591. &argList );
  592. if ( 0 != dwStatus && NULL != lpszTemp ) {
  593. rstrMessage.GetBufferSetLength( lstrlen (lpszTemp) + 1 );
  594. rstrMessage.ReleaseBuffer();
  595. rstrMessage = lpszTemp;
  596. } else {
  597. dwStatus = GetLastError();
  598. }
  599. if ( NULL != lpszTemp ) {
  600. LocalFree( lpszTemp);
  601. lpszTemp = NULL;
  602. }
  603. va_end(argList);
  604. return dwStatus;
  605. }
  606. BOOL __stdcall
  607. FileRead (
  608. HANDLE hFile,
  609. void* lpMemory,
  610. DWORD nAmtToRead)
  611. {
  612. BOOL bSuccess ;
  613. DWORD nAmtRead ;
  614. bSuccess = ReadFile (hFile, lpMemory, nAmtToRead, &nAmtRead, NULL) ;
  615. return (bSuccess && (nAmtRead == nAmtToRead)) ;
  616. } // FileRead
  617. BOOL __stdcall
  618. FileWrite (
  619. HANDLE hFile,
  620. void* lpMemory,
  621. DWORD nAmtToWrite)
  622. {
  623. BOOL bSuccess = FALSE;
  624. DWORD nAmtWritten = 0;
  625. DWORD dwFileSizeLow, dwFileSizeHigh;
  626. LONGLONG llResultSize;
  627. dwFileSizeLow = GetFileSize (hFile, &dwFileSizeHigh);
  628. // limit file size to 2GB
  629. if (dwFileSizeHigh > 0) {
  630. SetLastError (ERROR_WRITE_FAULT);
  631. bSuccess = FALSE;
  632. } else {
  633. // note that the error return of this function is 0xFFFFFFFF
  634. // since that is > the file size limit, this will be interpreted
  635. // as an error (a size error) so it's accounted for in the following
  636. // test.
  637. llResultSize = dwFileSizeLow + nAmtToWrite;
  638. if (llResultSize >= 0x80000000) {
  639. SetLastError (ERROR_WRITE_FAULT);
  640. bSuccess = FALSE;
  641. } else {
  642. // write buffer to file
  643. bSuccess = WriteFile (hFile, lpMemory, nAmtToWrite, &nAmtWritten, NULL) ;
  644. if (bSuccess) bSuccess = (nAmtWritten == nAmtToWrite ? TRUE : FALSE);
  645. }
  646. }
  647. return (bSuccess) ;
  648. } // FileWrite
  649. static
  650. DWORD _stdcall
  651. CheckDuplicateInstances (
  652. PDH_COUNTER_PATH_ELEMENTS* pFirst,
  653. PDH_COUNTER_PATH_ELEMENTS* pSecond )
  654. {
  655. DWORD dwStatus = ERROR_SUCCESS;
  656. ASSERT ( 0 == lstrcmpi ( pFirst->szMachineName, pSecond->szMachineName ) );
  657. ASSERT ( 0 == lstrcmpi ( pFirst->szObjectName, pSecond->szObjectName ) );
  658. if ( 0 == lstrcmpi ( pFirst->szInstanceName, pSecond->szInstanceName ) ) {
  659. if ( 0 == lstrcmpi ( pFirst->szParentInstance, pSecond->szParentInstance ) ) {
  660. if ( pFirst->dwInstanceIndex == pSecond->dwInstanceIndex ) {
  661. dwStatus = SMCFG_DUPL_SINGLE_PATH;
  662. }
  663. }
  664. } else if ( 0 == lstrcmpi ( pFirst->szInstanceName, _T("*") ) ) {
  665. dwStatus = SMCFG_DUPL_FIRST_IS_WILD;
  666. } else if ( 0 == lstrcmpi ( pSecond->szInstanceName, _T("*") ) ) {
  667. dwStatus = SMCFG_DUPL_SECOND_IS_WILD;
  668. }
  669. return dwStatus;
  670. }
  671. //++
  672. // Description:
  673. // The function checks the relation between two counter paths
  674. //
  675. // Parameter:
  676. // pFirst - First counter path
  677. // pSecond - Second counter path
  678. //
  679. // Return:
  680. // ERROR_SUCCESS - The two counter paths are different
  681. // SMCFG_DUPL_FIRST_IS_WILD - The first counter path has wildcard name
  682. // SMCFG_DUPL_SECOND_IS_WILD - The second counter path has wildcard name
  683. // SMCFG_DUPL_SINGLE_PATH - The two counter paths are the same(may include
  684. // wildcard name)
  685. //
  686. //--
  687. DWORD _stdcall
  688. CheckDuplicateCounterPaths (
  689. PDH_COUNTER_PATH_ELEMENTS* pFirst,
  690. PDH_COUNTER_PATH_ELEMENTS* pSecond )
  691. {
  692. DWORD dwStatus = ERROR_SUCCESS;
  693. if ( 0 == lstrcmpi ( pFirst->szMachineName, pSecond->szMachineName ) ) {
  694. if ( 0 == lstrcmpi ( pFirst->szObjectName, pSecond->szObjectName ) ) {
  695. if ( 0 == lstrcmpi ( pFirst->szCounterName, pSecond->szCounterName ) ) {
  696. dwStatus = CheckDuplicateInstances ( pFirst, pSecond );
  697. } else if ( 0 == lstrcmpi ( pFirst->szCounterName, _T("*") )
  698. || 0 == lstrcmpi ( pSecond->szCounterName, _T("*") ) ) {
  699. // Wildcard counter.
  700. BOOL bIsDuplicate = ( ERROR_SUCCESS != CheckDuplicateInstances ( pFirst, pSecond ) );
  701. if ( bIsDuplicate ) {
  702. if ( 0 == lstrcmpi ( pFirst->szCounterName, _T("*") ) ) {
  703. dwStatus = SMCFG_DUPL_FIRST_IS_WILD;
  704. } else if ( 0 == lstrcmpi ( pSecond->szCounterName, _T("*") ) ) {
  705. dwStatus = SMCFG_DUPL_SECOND_IS_WILD;
  706. }
  707. }
  708. }
  709. }
  710. }
  711. return dwStatus;
  712. };
  713. // This routine extracts the filename portion from a given full-path filename
  714. LPTSTR _stdcall
  715. ExtractFileName (LPTSTR pFileSpec)
  716. {
  717. LPTSTR pFileName = NULL ;
  718. TCHAR DIRECTORY_DELIMITER1 = TEXT('\\') ;
  719. TCHAR DIRECTORY_DELIMITER2 = TEXT(':') ;
  720. if (pFileSpec)
  721. {
  722. pFileName = pFileSpec + lstrlen (pFileSpec) ;
  723. while (*pFileName != DIRECTORY_DELIMITER1 &&
  724. *pFileName != DIRECTORY_DELIMITER2)
  725. {
  726. if (pFileName == pFileSpec)
  727. {
  728. // done when no directory delimiter is found
  729. break ;
  730. }
  731. pFileName-- ;
  732. }
  733. if (*pFileName == DIRECTORY_DELIMITER1 ||
  734. *pFileName == DIRECTORY_DELIMITER2)
  735. {
  736. // directory delimiter found, point the
  737. // filename right after it
  738. pFileName++ ;
  739. }
  740. }
  741. return pFileName ;
  742. } // ExtractFileName
  743. //+--------------------------------------------------------------------------
  744. //
  745. // Function: InvokeWinHelp
  746. //
  747. // Synopsis: Helper (ahem) function to invoke winhelp.
  748. //
  749. // Arguments: [message] - WM_CONTEXTMENU or WM_HELP
  750. // [wParam] - depends on [message]
  751. // [wszHelpFileName] - filename with or without path
  752. // [adwControlIdToHelpIdMap] - see WinHelp API
  753. //
  754. // History: 06-10-1997 DavidMun Created
  755. //
  756. //---------------------------------------------------------------------------
  757. VOID
  758. InvokeWinHelp(
  759. UINT message,
  760. WPARAM wParam,
  761. LPARAM lParam,
  762. const CString& rstrHelpFileName,
  763. DWORD adwControlIdToHelpIdMap[])
  764. {
  765. //TRACE_FUNCTION(InvokeWinHelp);
  766. ASSERT ( !rstrHelpFileName.IsEmpty() );
  767. ASSERT ( adwControlIdToHelpIdMap );
  768. switch (message)
  769. {
  770. case WM_CONTEXTMENU: // Right mouse click - "What's This" context menu
  771. {
  772. ASSERT ( wParam );
  773. if ( 0 != GetDlgCtrlID ( (HWND) wParam ) ) {
  774. WinHelp(
  775. (HWND) wParam,
  776. rstrHelpFileName,
  777. HELP_CONTEXTMENU,
  778. (DWORD_PTR)adwControlIdToHelpIdMap);
  779. }
  780. }
  781. break;
  782. case WM_HELP: // Help from the "?" dialog
  783. {
  784. const LPHELPINFO pHelpInfo = (LPHELPINFO) lParam;
  785. if (pHelpInfo ) {
  786. if ( pHelpInfo->iContextType == HELPINFO_WINDOW ) {
  787. WinHelp(
  788. (HWND) pHelpInfo->hItemHandle,
  789. rstrHelpFileName,
  790. HELP_WM_HELP,
  791. (DWORD_PTR) adwControlIdToHelpIdMap);
  792. }
  793. }
  794. break;
  795. }
  796. default:
  797. //Dbg(DEB_ERROR, "Unexpected message %uL\n", message);
  798. break;
  799. }
  800. }
  801. BOOL
  802. FileNameIsValid ( CString* pstrFileName )
  803. {
  804. CString strfname;
  805. CString strModName;
  806. TCHAR BufferSrc[MAX_PATH];
  807. LPTSTR Src;
  808. BOOL bRetVal = TRUE;
  809. strfname = *pstrFileName;
  810. swprintf (BufferSrc,_T("%ws"), *pstrFileName);
  811. Src = BufferSrc;
  812. while(*Src != '\0'){
  813. if (*Src == '?' ||
  814. *Src == '\\' ||
  815. *Src == '*' ||
  816. *Src == '|' ||
  817. *Src == '<' ||
  818. *Src == '>' ||
  819. *Src == '/' ||
  820. *Src == ':' ||
  821. *Src == '\"'
  822. ){
  823. bRetVal = FALSE;
  824. break;
  825. }
  826. Src++;
  827. }
  828. return bRetVal;
  829. }
  830. DWORD
  831. FormatSystemMessage (
  832. DWORD dwMessageId,
  833. CString& rstrSystemMessage )
  834. {
  835. DWORD dwStatus = ERROR_SUCCESS;
  836. HINSTANCE hPdh = NULL;
  837. DWORD dwFlags = 0;
  838. LPTSTR pszMessage = NULL;
  839. DWORD dwChars;
  840. rstrSystemMessage.Empty();
  841. dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM;
  842. hPdh = LoadLibrary( _T("PDH.DLL") );
  843. if ( NULL != hPdh ) {
  844. dwFlags |= FORMAT_MESSAGE_FROM_HMODULE;
  845. }
  846. dwChars = FormatMessage (
  847. dwFlags,
  848. hPdh,
  849. dwMessageId,
  850. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  851. (LPTSTR)&pszMessage,
  852. 0,
  853. NULL );
  854. if ( NULL != hPdh ) {
  855. FreeLibrary( hPdh );
  856. }
  857. if ( 0 == dwChars ) {
  858. dwStatus = GetLastError();
  859. }
  860. MFC_TRY
  861. if ( NULL != pszMessage ) {
  862. if ( _T('\0') != pszMessage[0] ) {
  863. rstrSystemMessage = pszMessage;
  864. }
  865. }
  866. MFC_CATCH_DWSTATUS
  867. if ( rstrSystemMessage.IsEmpty() ) {
  868. MFC_TRY
  869. rstrSystemMessage.Format ( _T("0x%08lX"), dwMessageId );
  870. MFC_CATCH_DWSTATUS
  871. }
  872. LocalFree ( pszMessage );
  873. return dwStatus;
  874. }
  875. ResourceStateManager::ResourceStateManager ()
  876. : m_hResInstance ( NULL )
  877. {
  878. AFX_MODULE_STATE* pModuleState;
  879. HINSTANCE hNewResourceHandle;
  880. pModuleState = AfxGetModuleState();
  881. if ( NULL != pModuleState ) {
  882. m_hResInstance = pModuleState->m_hCurrentResourceHandle;
  883. hNewResourceHandle = (HINSTANCE)GetModuleHandleW (_CONFIG_DLL_NAME_W_);
  884. pModuleState->m_hCurrentResourceHandle = hNewResourceHandle;
  885. }
  886. }
  887. ResourceStateManager::~ResourceStateManager ()
  888. {
  889. AFX_MODULE_STATE* pModuleState;
  890. pModuleState = AfxGetModuleState();
  891. if ( NULL != pModuleState ) {
  892. pModuleState->m_hCurrentResourceHandle = m_hResInstance;
  893. }
  894. }