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.

426 lines
13 KiB

  1. //==========================================================================//
  2. // Includes //
  3. //==========================================================================//
  4. #include <string.h> // strupr
  5. #include <stdio.h> // for sprintf.
  6. #include "setedit.h"
  7. #include "init.h" // external declarations for this file
  8. #include "command.h" // for ViewChart
  9. #include "grafdata.h" // for QuerySaveChart
  10. #include "graph.h" // for GraphInitializeApplication
  11. #include "legend.h" // for LegendInitializeApplication
  12. #include "perfdata.h" // for PerfDataInitializeInstance
  13. #include "perfmops.h" // for OpenFileHandler, for now
  14. #include "status.h" // for StatusInitializeApplication
  15. #include "registry.h" // for Load/SaveMainWindowPlacement
  16. #include "toolbar.h" // for ToolbarInitializeApplication
  17. #include "utils.h"
  18. #include "fileopen.h" // for FileOpen
  19. #include "pmemory.h" // for MemoryFree
  20. extern TCHAR DefaultLangId[] ;
  21. extern TCHAR EnglishLangId[] ;
  22. static LPSTR lpszCommandLine;
  23. //==========================================================================//
  24. // Constants //
  25. //==========================================================================//
  26. #define szPerfmonMainClass TEXT("PerfmonMainClass")
  27. HHOOK lpMsgFilterProc ;
  28. //==========================================================================//
  29. // Local Functions //
  30. //==========================================================================//
  31. static
  32. LONG
  33. EnablePrivilege (
  34. IN LPTSTR szPrivName
  35. )
  36. {
  37. LUID SePrivNameValue;
  38. TOKEN_PRIVILEGES tkp;
  39. HANDLE hToken = NULL;
  40. /* Retrieve a handle of the access token. */
  41. if (!OpenProcessToken(GetCurrentProcess(),
  42. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  43. &hToken)) {
  44. goto Exit_Point;
  45. }
  46. /*
  47. * Enable the privilege by name and get the ID
  48. */
  49. if (!LookupPrivilegeValue((LPTSTR) NULL,
  50. szPrivName,
  51. &SePrivNameValue)) {
  52. goto Exit_Point;
  53. }
  54. tkp.PrivilegeCount = 1;
  55. tkp.Privileges[0].Luid = SePrivNameValue;
  56. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  57. AdjustTokenPrivileges(hToken,
  58. FALSE,
  59. &tkp,
  60. sizeof(TOKEN_PRIVILEGES),
  61. (PTOKEN_PRIVILEGES) NULL,
  62. (PDWORD) NULL);
  63. /* The return value of AdjustTokenPrivileges be texted. */
  64. Exit_Point:
  65. if (hToken != NULL) CloseHandle (hToken);
  66. return GetLastError();
  67. }
  68. void GetScalesFonts (void)
  69. {
  70. LOGFONT lf ;
  71. memset (&lf, 0, sizeof (lf)) ;
  72. lstrcpy (lf.lfFaceName, szScalesFontFace) ;
  73. lf.lfHeight = iScalesFontHeight ;
  74. lf.lfWeight = FW_REGULAR ;
  75. hFontScales = CreateFontIndirect (&lf) ;
  76. lf.lfWeight = FW_BOLD ;
  77. hFontScalesBold = CreateFontIndirect (&lf) ;
  78. }
  79. BOOL InitializeSystemValues (void)
  80. /*
  81. Effect: Read and store in variables the various system values,
  82. such as the width and height of the screen and icons,
  83. the width of scroll bars, etc.
  84. Called By: PerfmonInitialize only.
  85. Returns: Whether this function was successful in getting all
  86. needed system values.
  87. */
  88. { // InitializeSystemValues
  89. xScreenWidth = GetSystemMetrics (SM_CXSCREEN) ;
  90. yScreenHeight = GetSystemMetrics (SM_CYSCREEN) ;
  91. xBorderWidth = GetSystemMetrics (SM_CXBORDER) ;
  92. yBorderHeight = GetSystemMetrics (SM_CYBORDER) ;
  93. xScrollWidth = GetSystemMetrics (SM_CXVSCROLL) ;
  94. yScrollHeight = GetSystemMetrics (SM_CYHSCROLL) ;
  95. xScrollThumbWidth = GetSystemMetrics (SM_CXHTHUMB) ;
  96. yScrollThumbHeight = GetSystemMetrics (SM_CYVTHUMB) ;
  97. xDlgBorderWidth = GetSystemMetrics (SM_CXDLGFRAME) ;
  98. yDlgBorderHeight = GetSystemMetrics (SM_CYDLGFRAME) ;
  99. MinimumSize = yScrollHeight +
  100. GetSystemMetrics (SM_CYMENU) +
  101. GetSystemMetrics (SM_CYCAPTION) ;
  102. //================================================================//
  103. // create all the brushes and pens for performance improvement //
  104. //================================================================//
  105. CreatePerfmonSystemObjects () ;
  106. hWhitePen = CreatePen (PS_SOLID, 3, crWhite) ;
  107. return (TRUE) ;
  108. } // InitializeSystemValues
  109. BOOL InitializeApplication (void)
  110. /*
  111. Effect: Perform all initializations required for the FIRST
  112. instance of the Perfmon application. In particular,
  113. register all of Perfmon's window classes.
  114. Note: There is no background brush set for the MainWindow
  115. class so that the main window is never erased. The
  116. client area of MainWindow is always covered by one
  117. of the view windows. If we erase it, it would just
  118. flicker needlessly.
  119. Called By: PerfmonInitialize only.
  120. Returns: Whether this function was successful in initializing.
  121. */
  122. { // InitializeApplication
  123. BOOL bSuccess ;
  124. WNDCLASS wc ;
  125. TCHAR LocalHelpFileName [ShortTextLen] ;
  126. LPTSTR pFileName ;
  127. hIcon = LoadIcon (hInstance, idIcon) ;
  128. //=============================//
  129. // Register Main window class //
  130. //=============================//
  131. wc.style = CS_DBLCLKS | CS_BYTEALIGNCLIENT;
  132. wc.lpfnWndProc = (WNDPROC) MainWndProc;
  133. wc.hInstance = hInstance;
  134. wc.cbClsExtra = 0 ;
  135. wc.cbWndExtra = 0;
  136. wc.hIcon = hIcon ;
  137. wc.hCursor = LoadCursor(NULL, IDI_APPLICATION);
  138. wc.hbrBackground = NULL ; // see note above
  139. wc.lpszMenuName = idMenuChart ;
  140. wc.lpszClassName = szPerfmonMainClass ;
  141. bSuccess = RegisterClass (&wc) ;
  142. //=============================//
  143. // Register Abstract "Systems" //
  144. //=============================//
  145. hbLightGray = GetStockObject (LTGRAY_BRUSH) ;
  146. if (bSuccess)
  147. bSuccess = StatusInitializeApplication () ;
  148. if (bSuccess)
  149. bSuccess = GraphInitializeApplication () ;
  150. return (bSuccess) ;
  151. } // InitializeApplication
  152. BOOL InitializeInstance (int nCmdShow, LPCSTR lpszCmdLine)
  153. /*
  154. Effect: Perform all initializations required for EACH instance
  155. of the Perfmon application. In particular, create all
  156. of Perfmon's initial windows, and perform any other
  157. initializations except registering classes (done in
  158. InitializeApplication).
  159. Called By: PerfmonInitialize only.
  160. Note: This function has multiple return points.
  161. Returns: Whether this function was successful in initalizing.
  162. */
  163. { // InitializeInstance
  164. DWORD ComputerNameLength;
  165. TCHAR szApplication [WindowCaptionLen] ;
  166. // enable privileges needed to profile system
  167. // if this fails, that's ok for now.
  168. EnablePrivilege (SE_SYSTEM_PROFILE_NAME); // to access perfdata
  169. EnablePrivilege (SE_INC_BASE_PRIORITY_NAME); // to raise priority
  170. //=============================//
  171. // Set Priority high //
  172. //=============================//
  173. SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS) ;
  174. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST) ;
  175. //=============================//
  176. // Load Resources //
  177. //=============================//
  178. GetScalesFonts () ;
  179. hMenuChart = LoadMenu (hInstance, idMenuChart) ;
  180. hAccelerators = LoadAccelerators (hInstance, idAccelerators) ;
  181. //=============================//
  182. // Initialize Systems //
  183. //=============================//
  184. iLanguage = GetUserDefaultLangID() ;
  185. iEnglishLanguage = MAKELANGID (LANG_ENGLISH, LANG_NEUTRAL) ;
  186. TSPRINTF (DefaultLangId, TEXT("%03x"), iLanguage) ;
  187. TSPRINTF (EnglishLangId, TEXT("%03x"), iEnglishLanguage) ;
  188. // GetComputerName returns the name without the "\\" prefix. We add
  189. // the prefix before even calling the routine. This is so that all our
  190. // computer names have the prefix and are therefore compatible with
  191. // I_SetSystemFocus (see perfmops.c).
  192. ComputerNameLength = MAX_COMPUTERNAME_LENGTH + 1;
  193. lstrcpy (LocalComputerName, szComputerPrefix) ;
  194. GetComputerName (LocalComputerName + lstrlen (szComputerPrefix),
  195. &ComputerNameLength);
  196. PerfDataInitializeInstance () ;
  197. //=============================//
  198. // Create Window //
  199. //=============================//
  200. StringLoad (IDS_APPNAME, szApplication) ;
  201. hWndMain = CreateWindow (szPerfmonMainClass,
  202. szApplication,
  203. WS_OVERLAPPEDWINDOW | WS_BORDER,
  204. CW_USEDEFAULT, CW_USEDEFAULT,
  205. CW_USEDEFAULT, CW_USEDEFAULT,
  206. NULL,
  207. NULL,
  208. NULL,
  209. NULL);
  210. if (!hWndMain)
  211. return (FALSE) ;
  212. ViewChart (hWndMain) ;
  213. LoadMainWindowPlacement (hWndMain) ;
  214. return (TRUE) ;
  215. } // InitializeInstance
  216. //==========================================================================//
  217. // Exported Functions //
  218. //==========================================================================//
  219. BOOL PerfmonInitialize (HINSTANCE hCurrentInstance,
  220. HINSTANCE hPrevInstance,
  221. LPCSTR lpszCmdLine,
  222. int nCmdShow)
  223. /*
  224. Effect: Performa all initializations required when Perfmon is
  225. started. In particular, initialize all "systems", register
  226. all window classes, create needed windows, read in and
  227. process font and Perfmon lists.
  228. Called By: WinMain only, at the start of the application.
  229. Assert: There are no other instances of Perfmon currently
  230. executing.
  231. Returns: Whether initialization was successful. If this function
  232. returns FALSE, Perfmon should exit immediately.
  233. Internals: The bSuccess variable is used to conditionalize each
  234. successive initialization step.
  235. */
  236. { // PerfmonInitialize
  237. BOOL bSuccess ;
  238. TCHAR szFilePath [FilePathLen + 1] ;
  239. LPTSTR pFileNameStart ;
  240. HANDLE hFindFile ;
  241. WIN32_FIND_DATA FindFileInfo ;
  242. CHAR QuoteChar ;
  243. LPSTR pCmdLine ;
  244. int NameOffset ;
  245. hInstance = hCurrentInstance ;
  246. bSuccess = InitializeSystemValues () ;
  247. if (bSuccess && !hPrevInstance)
  248. bSuccess = InitializeApplication () ;
  249. if (bSuccess)
  250. bSuccess = InitializeInstance (nCmdShow, lpszCmdLine) ;
  251. if (bSuccess)
  252. {
  253. if (strempty (lpszCmdLine))
  254. StringLoad (IDS_DEFAULTPATH, szFilePath) ;
  255. else
  256. {
  257. // check for single or double quote
  258. QuoteChar = *lpszCmdLine ;
  259. if (QuoteChar == '\'' || QuoteChar == '\"')
  260. {
  261. lpszCmdLine++ ;
  262. // remove the matching QuoteChar if found
  263. pCmdLine = (LPSTR) lpszCmdLine ;
  264. while (*pCmdLine != '\0')
  265. {
  266. if (*pCmdLine == QuoteChar)
  267. {
  268. *pCmdLine = '\0' ;
  269. break ;
  270. }
  271. else
  272. {
  273. pCmdLine++ ;
  274. }
  275. }
  276. }
  277. // convert the LPSTR to LPTSTR
  278. mbstowcs (szFilePath, lpszCmdLine, strlen(lpszCmdLine) + 1) ;
  279. pFileNameStart = ExtractFileName (szFilePath) ;
  280. NameOffset = (int)(pFileNameStart - szFilePath) ;
  281. // convert short filename to long NTFS filename if necessary
  282. hFindFile = FindFirstFile (szFilePath, &FindFileInfo) ;
  283. if (hFindFile && hFindFile != INVALID_HANDLE_VALUE)
  284. {
  285. // append the file name back to the path name
  286. lstrcpy (&szFilePath[NameOffset], FindFileInfo.cFileName) ;
  287. FindClose (hFindFile) ;
  288. }
  289. }
  290. // OpenFileHandler (hWndMain, szFilePath) ;
  291. FileOpen (hWndMain, (int)0, (LPTSTR)szFilePath) ;
  292. PrepareMenu (GetMenu (hWndMain));
  293. }
  294. return (bSuccess) ;
  295. } // PerfmonInitialize
  296. void PerfmonClose (HWND hWndMain)
  297. {
  298. // reset all views - will free all systems as well
  299. ResetGraphView (hWndGraph) ;
  300. // close the local machine
  301. if (bCloseLocalMachine)
  302. {
  303. RegCloseKey (HKEY_PERFORMANCE_DATA) ;
  304. }
  305. // free all the filenames
  306. if (pChartFullFileName)
  307. {
  308. MemoryFree (pChartFullFileName) ;
  309. pChartFullFileName = NULL ;
  310. }
  311. // free all the GDI resources
  312. DeletePen (hWhitePen) ;
  313. DeletePerfmonSystemObjects () ;
  314. // SaveMainWindowPlacement (hWndMain) ;
  315. DestroyWindow (hWndMain) ;
  316. } // PerfmonClose
  317.