xref: /reactos/base/applications/taskmgr/taskmgr.c (revision 2d53e953)
1 /*
2  * PROJECT:     ReactOS Task Manager
3  * LICENSE:     LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4  * PURPOSE:     Application Entry-point.
5  * COPYRIGHT:   Copyright 1999-2001 Brian Palmer <brianp@reactos.org>
6  *              Copyright 2005 Klemens Friedl <frik85@reactos.at>
7  */
8 
9 #include "precomp.h"
10 
11 #include "perfpage.h"
12 #include "about.h"
13 #include "affinity.h"
14 #include "debug.h"
15 #include "priority.h"
16 
17 #define STATUS_WINDOW   2001
18 
19 /* Global Variables: */
20 HINSTANCE hInst;                 /* current instance */
21 
22 HWND hMainWnd;                   /* Main Window */
23 HWND hStatusWnd;                 /* Status Bar Window */
24 HWND hTabWnd;                    /* Tab Control Window */
25 
26 HMENU hWindowMenu = NULL;
27 
28 int  nMinimumWidth;              /* Minimum width of the dialog (OnSize()'s cx) */
29 int  nMinimumHeight;             /* Minimum height of the dialog (OnSize()'s cy) */
30 
31 int  nOldWidth;                  /* Holds the previous client area width */
32 int  nOldHeight;                 /* Holds the previous client area height */
33 
34 BOOL bInMenuLoop = FALSE;        /* Tells us if we are in the menu loop */
35 BOOL bWasKeyboardInput = FALSE;  /* TabChange by Keyboard or Mouse ? */
36 
37 TASKMANAGER_SETTINGS TaskManagerSettings;
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 // Taken from WinSpy++ 1.7
41 // http://www.catch22.net/software/winspy
42 // Copyright (c) 2002 by J Brown
43 //
44 
45 //
46 //	Copied from uxtheme.h
47 //  If you have this new header, then delete these and
48 //  #include <uxtheme.h> instead!
49 //
50 #define ETDT_DISABLE        0x00000001
51 #define ETDT_ENABLE         0x00000002
52 #define ETDT_USETABTEXTURE  0x00000004
53 #define ETDT_ENABLETAB      (ETDT_ENABLE  | ETDT_USETABTEXTURE)
54 
55 //
56 typedef HRESULT (WINAPI * ETDTProc) (HWND, DWORD);
57 
58 //
59 //	Try to call EnableThemeDialogTexture, if uxtheme.dll is present
60 //
61 BOOL EnableDialogTheme(HWND hwnd)
62 {
63     HMODULE hUXTheme;
64     ETDTProc fnEnableThemeDialogTexture;
65 
66     hUXTheme = LoadLibraryA("uxtheme.dll");
67 
68     if(hUXTheme)
69     {
70         fnEnableThemeDialogTexture =
71             (ETDTProc)GetProcAddress(hUXTheme, "EnableThemeDialogTexture");
72 
73         if(fnEnableThemeDialogTexture)
74         {
75             fnEnableThemeDialogTexture(hwnd, ETDT_ENABLETAB);
76 
77             FreeLibrary(hUXTheme);
78             return TRUE;
79         }
80         else
81         {
82             // Failed to locate API!
83             FreeLibrary(hUXTheme);
84             return FALSE;
85         }
86     }
87     else
88     {
89         // Not running under XP? Just fail gracefully
90         return FALSE;
91     }
92 }
93 
94 int APIENTRY wWinMain(HINSTANCE hInstance,
95                       HINSTANCE hPrevInstance,
96                       LPWSTR    lpCmdLine,
97                       int       nCmdShow)
98 {
99     HANDLE hProcess;
100     HANDLE hToken;
101     TOKEN_PRIVILEGES tkp;
102     HANDLE hMutex;
103 
104     /* check wether we're already running or not */
105     hMutex = CreateMutexW(NULL, TRUE, L"taskmgrros");
106     if (hMutex && GetLastError() == ERROR_ALREADY_EXISTS)
107     {
108         /* Restore existing taskmanager and bring window to front */
109         /* Relies on the fact that the application title string and window title are the same */
110         HWND hTaskMgr;
111         TCHAR szTaskmgr[128];
112 
113         LoadString(hInst, IDS_APP_TITLE, szTaskmgr, sizeof(szTaskmgr)/sizeof(TCHAR));
114         hTaskMgr = FindWindow(NULL, szTaskmgr);
115 
116         if (hTaskMgr != NULL)
117         {
118             SendMessage(hTaskMgr, WM_SYSCOMMAND, SC_RESTORE, 0);
119             SetForegroundWindow(hTaskMgr);
120         }
121 
122         CloseHandle(hMutex);
123         return 0;
124     }
125     else if (!hMutex)
126     {
127         return 1;
128     }
129 
130     /* Initialize global variables */
131     hInst = hInstance;
132 
133     /* Change our priority class to HIGH */
134     hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
135     SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
136     CloseHandle(hProcess);
137 
138     /* Now lets get the SE_DEBUG_NAME privilege
139      * so that we can debug processes
140      */
141 
142     /* Get a token for this process.  */
143     if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
144     {
145         /* Get the LUID for the debug privilege.  */
146         if (LookupPrivilegeValueW(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid))
147         {
148             tkp.PrivilegeCount = 1;  /* one privilege to set */
149             tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
150 
151             /* Get the debug privilege for this process. */
152             AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
153         }
154         CloseHandle(hToken);
155     }
156 
157     /* Load our settings from the registry */
158     LoadSettings();
159 
160     /* Initialize perf data */
161     if (!PerfDataInitialize())
162     {
163         return -1;
164     }
165 
166     /*
167      * Set our shutdown parameters: we want to shutdown the very last,
168      * without displaying any end task dialog if needed.
169      */
170     SetProcessShutdownParameters(1, SHUTDOWN_NORETRY);
171 
172     DialogBoxW(hInst, (LPCWSTR)IDD_TASKMGR_DIALOG, NULL, TaskManagerWndProc);
173 
174     /* Save our settings to the registry */
175     SaveSettings();
176     PerfDataUninitialize();
177     CloseHandle(hMutex);
178     if (hWindowMenu)
179         DestroyMenu(hWindowMenu);
180     return 0;
181 }
182 
183 /* Message handler for dialog box. */
184 INT_PTR CALLBACK
185 TaskManagerWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
186 {
187 #if 0
188     HDC              hdc;
189     PAINTSTRUCT      ps;
190     RECT             rc;
191 #endif
192     LPRECT           pRC;
193     LPNMHDR          pnmh;
194     WINDOWPLACEMENT  wp;
195 
196     switch (message) {
197     case WM_INITDIALOG:
198         // For now, the Help dialog menu item is disabled because of lacking of HTML Help support
199         EnableMenuItem(GetMenu(hDlg), ID_HELP_TOPICS, MF_BYCOMMAND | MF_GRAYED);
200         hMainWnd = hDlg;
201         return OnCreate(hDlg);
202 
203     case WM_COMMAND:
204         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
205             EndDialog(hDlg, LOWORD(wParam));
206             return TRUE;
207         }
208         /* Process menu commands */
209         switch (LOWORD(wParam))
210         {
211         case ID_FILE_NEW:
212             TaskManager_OnFileNew();
213             break;
214         case ID_OPTIONS_ALWAYSONTOP:
215             TaskManager_OnOptionsAlwaysOnTop();
216             break;
217         case ID_OPTIONS_MINIMIZEONUSE:
218             TaskManager_OnOptionsMinimizeOnUse();
219             break;
220         case ID_OPTIONS_HIDEWHENMINIMIZED:
221             TaskManager_OnOptionsHideWhenMinimized();
222             break;
223         case ID_OPTIONS_SHOW16BITTASKS:
224             TaskManager_OnOptionsShow16BitTasks();
225             break;
226         case ID_RESTORE:
227             TaskManager_OnRestoreMainWindow();
228             break;
229         case ID_VIEW_LARGE:
230         case ID_VIEW_SMALL:
231         case ID_VIEW_DETAILS:
232             ApplicationPage_OnView(LOWORD(wParam));
233             break;
234         case ID_VIEW_SHOWKERNELTIMES:
235             PerformancePage_OnViewShowKernelTimes();
236             break;
237         case ID_VIEW_CPUHISTORY_ONEGRAPHALL:
238             PerformancePage_OnViewCPUHistoryOneGraphAll();
239             break;
240         case ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU:
241             PerformancePage_OnViewCPUHistoryOneGraphPerCPU();
242             break;
243         case ID_VIEW_UPDATESPEED_HIGH:
244         case ID_VIEW_UPDATESPEED_NORMAL:
245         case ID_VIEW_UPDATESPEED_LOW:
246         case ID_VIEW_UPDATESPEED_PAUSED:
247             TaskManager_OnViewUpdateSpeed(LOWORD(wParam));
248             break;
249         case ID_VIEW_SELECTCOLUMNS:
250             ProcessPage_OnViewSelectColumns();
251             break;
252         case ID_VIEW_REFRESH:
253             PostMessageW(hDlg, WM_TIMER, 0, 0);
254             break;
255         case ID_WINDOWS_TILEHORIZONTALLY:
256             ApplicationPage_OnWindowsTile(MDITILE_HORIZONTAL);
257             break;
258         case ID_WINDOWS_TILEVERTICALLY:
259             ApplicationPage_OnWindowsTile(MDITILE_VERTICAL);
260             break;
261         case ID_WINDOWS_MINIMIZE:
262             ApplicationPage_OnWindowsMinimize();
263             break;
264         case ID_WINDOWS_MAXIMIZE:
265             ApplicationPage_OnWindowsMaximize();
266             break;
267         case ID_WINDOWS_CASCADE:
268             ApplicationPage_OnWindowsCascade();
269             break;
270         case ID_WINDOWS_BRINGTOFRONT:
271             ApplicationPage_OnWindowsBringToFront();
272             break;
273         case ID_APPLICATION_PAGE_SWITCHTO:
274             ApplicationPage_OnSwitchTo();
275             break;
276         case ID_APPLICATION_PAGE_ENDTASK:
277             ApplicationPage_OnEndTask();
278             break;
279         case ID_APPLICATION_PAGE_GOTOPROCESS:
280             ApplicationPage_OnGotoProcess();
281             break;
282         case ID_PROCESS_PAGE_ENDPROCESS:
283             ProcessPage_OnEndProcess();
284             break;
285         case ID_PROCESS_PAGE_ENDPROCESSTREE:
286             ProcessPage_OnEndProcessTree();
287             break;
288         case ID_PROCESS_PAGE_DEBUG:
289             ProcessPage_OnDebug();
290             break;
291         case ID_PROCESS_PAGE_SETAFFINITY:
292             ProcessPage_OnSetAffinity();
293             break;
294         case ID_PROCESS_PAGE_SETPRIORITY_REALTIME:
295             DoSetPriority(REALTIME_PRIORITY_CLASS);
296             break;
297         case ID_PROCESS_PAGE_SETPRIORITY_HIGH:
298             DoSetPriority(HIGH_PRIORITY_CLASS);
299             break;
300         case ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL:
301             DoSetPriority(ABOVE_NORMAL_PRIORITY_CLASS);
302             break;
303         case ID_PROCESS_PAGE_SETPRIORITY_NORMAL:
304             DoSetPriority(NORMAL_PRIORITY_CLASS);
305             break;
306         case ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL:
307             DoSetPriority(BELOW_NORMAL_PRIORITY_CLASS);
308             break;
309         case ID_PROCESS_PAGE_SETPRIORITY_LOW:
310             DoSetPriority(IDLE_PRIORITY_CLASS);
311             break;
312         case ID_PROCESS_PAGE_PROPERTIES:
313             ProcessPage_OnProperties();
314             break;
315         case ID_PROCESS_PAGE_OPENFILELOCATION:
316             ProcessPage_OnOpenFileLocation();
317             break;
318 
319 /* ShutDown items */
320         case ID_SHUTDOWN_STANDBY:
321             ShutDown_StandBy();
322             break;
323         case ID_SHUTDOWN_HIBERNATE:
324             ShutDown_Hibernate();
325             break;
326         case ID_SHUTDOWN_POWEROFF:
327             ShutDown_PowerOff();
328             break;
329         case ID_SHUTDOWN_REBOOT:
330             ShutDown_Reboot();
331             break;
332         case ID_SHUTDOWN_LOGOFF:
333             ShutDown_LogOffUser();
334             break;
335         case ID_SHUTDOWN_SWITCHUSER:
336             ShutDown_SwitchUser();
337             break;
338         case ID_SHUTDOWN_LOCKCOMPUTER:
339             ShutDown_LockComputer();
340             break;
341         case ID_SHUTDOWN_DISCONNECT:
342             ShutDown_Disconnect();
343             break;
344         case ID_SHUTDOWN_EJECT_COMPUTER:
345             ShutDown_EjectComputer();
346             break;
347 
348         case ID_HELP_ABOUT:
349             OnAbout();
350             break;
351         case ID_FILE_EXIT:
352             EndDialog(hDlg, IDOK);
353             break;
354         }
355         break;
356 
357     case WM_ONTRAYICON:
358         switch(lParam)
359         {
360         case WM_RBUTTONDOWN:
361             {
362             POINT pt;
363             BOOL OnTop;
364             HMENU hMenu, hPopupMenu;
365 
366             GetCursorPos(&pt);
367 
368             OnTop = ((GetWindowLongPtrW(hMainWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0);
369 
370             hMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_TRAY_POPUP));
371             hPopupMenu = GetSubMenu(hMenu, 0);
372 
373             if(IsWindowVisible(hMainWnd))
374             {
375               DeleteMenu(hPopupMenu, ID_RESTORE, MF_BYCOMMAND);
376             }
377             else
378             {
379               SetMenuDefaultItem(hPopupMenu, ID_RESTORE, FALSE);
380             }
381 
382             if(OnTop)
383             {
384               CheckMenuItem(hPopupMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND | MF_CHECKED);
385             } else
386             {
387               CheckMenuItem(hPopupMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND | MF_UNCHECKED);
388             }
389 
390             SetForegroundWindow(hMainWnd);
391             TrackPopupMenuEx(hPopupMenu, 0, pt.x, pt.y, hMainWnd, NULL);
392 
393             DestroyMenu(hMenu);
394             break;
395             }
396         case WM_LBUTTONDBLCLK:
397             TaskManager_OnRestoreMainWindow();
398             break;
399         }
400         break;
401 
402     case WM_NOTIFY:
403         pnmh = (LPNMHDR)lParam;
404         if ((pnmh->hwndFrom == hTabWnd) &&
405             (pnmh->idFrom == IDC_TAB))
406         {
407             switch (pnmh->code)
408             {
409                 case TCN_SELCHANGE:
410                     TaskManager_OnTabWndSelChange();
411                     break;
412                 case TCN_KEYDOWN:
413                     bWasKeyboardInput = TRUE;
414                     break;
415                 case NM_CLICK:
416                     bWasKeyboardInput = FALSE;
417                 break;
418             }
419         }
420         break;
421 
422     case WM_SIZING:
423         /* Make sure the user is sizing the dialog */
424         /* in an acceptable range */
425         pRC = (LPRECT)lParam;
426         if ((wParam == WMSZ_LEFT) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_BOTTOMLEFT)) {
427             /* If the width is too small enlarge it to the minimum */
428             if (nMinimumWidth > (pRC->right - pRC->left))
429                 pRC->left = pRC->right - nMinimumWidth;
430         } else {
431             /* If the width is too small enlarge it to the minimum */
432             if (nMinimumWidth > (pRC->right - pRC->left))
433                 pRC->right = pRC->left + nMinimumWidth;
434         }
435         if ((wParam == WMSZ_TOP) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_TOPRIGHT)) {
436             /* If the height is too small enlarge it to the minimum */
437             if (nMinimumHeight > (pRC->bottom - pRC->top))
438                 pRC->top = pRC->bottom - nMinimumHeight;
439         } else {
440             /* If the height is too small enlarge it to the minimum */
441             if (nMinimumHeight > (pRC->bottom - pRC->top))
442                 pRC->bottom = pRC->top + nMinimumHeight;
443         }
444         return TRUE;
445         break;
446 
447     case WM_SIZE:
448         /* Handle the window sizing in it's own function */
449         OnSize(wParam, LOWORD(lParam), HIWORD(lParam));
450         break;
451 
452     case WM_MOVE:
453         /* Handle the window moving in it's own function */
454         OnMove(wParam, LOWORD(lParam), HIWORD(lParam));
455         break;
456 
457     case WM_DESTROY:
458         ShowWindow(hDlg, SW_HIDE);
459         TrayIcon_RemoveIcon();
460         wp.length = sizeof(WINDOWPLACEMENT);
461         GetWindowPlacement(hDlg, &wp);
462         TaskManagerSettings.Left = wp.rcNormalPosition.left;
463         TaskManagerSettings.Top = wp.rcNormalPosition.top;
464         TaskManagerSettings.Right = wp.rcNormalPosition.right;
465         TaskManagerSettings.Bottom = wp.rcNormalPosition.bottom;
466         if (IsZoomed(hDlg) || (wp.flags & WPF_RESTORETOMAXIMIZED))
467             TaskManagerSettings.Maximized = TRUE;
468         else
469             TaskManagerSettings.Maximized = FALSE;
470         /* Get rid of the allocated command line cache, if any */
471         PerfDataDeallocCommandLineCache();
472         if (hWindowMenu)
473             DestroyMenu(hWindowMenu);
474         return DefWindowProcW(hDlg, message, wParam, lParam);
475 
476     case WM_TIMER:
477         /* Refresh the performance data */
478         PerfDataRefresh();
479         RefreshApplicationPage();
480         RefreshProcessPage();
481         RefreshPerformancePage();
482         TrayIcon_UpdateIcon();
483         break;
484 
485     case WM_INITMENUPOPUP:
486         /* Do not disable the status bar if we opened the system menu */
487         if (!HIWORD(lParam))
488             TaskManager_DisableStatusBar(hDlg);
489         else
490             TaskManager_EnableStatusBar(hDlg);
491         break;
492     case WM_ENTERMENULOOP:
493         bInMenuLoop = TRUE;
494         break;
495     case WM_EXITMENULOOP:
496         bInMenuLoop = FALSE;
497         TaskManager_EnableStatusBar(hDlg);
498         break;
499     case WM_MENUSELECT:
500         if (!(HIWORD(wParam) & MF_SYSMENU))
501             TaskManager_OnMenuSelect(hDlg, LOWORD(wParam), HIWORD(wParam), (HMENU)lParam);
502         break;
503     case WM_SYSCOLORCHANGE:
504         /* Forward WM_SYSCOLORCHANGE to common controls */
505         SendMessage(hApplicationPageListCtrl, WM_SYSCOLORCHANGE, 0, 0);
506         SendMessage(hProcessPageListCtrl, WM_SYSCOLORCHANGE, 0, 0);
507         SendMessage(hProcessPageHeaderCtrl, WM_SYSCOLORCHANGE, 0, 0);
508         break;
509     }
510 
511     return 0;
512 }
513 
514 void FillSolidRect(HDC hDC, LPCRECT lpRect, COLORREF clr)
515 {
516     SetBkColor(hDC, clr);
517     ExtTextOutW(hDC, 0, 0, ETO_OPAQUE, lpRect, NULL, 0, NULL);
518 }
519 
520 static void SetUpdateSpeed(HWND hWnd)
521 {
522     /* Setup update speed (pause=fall down) */
523     switch (TaskManagerSettings.UpdateSpeed) {
524     case ID_VIEW_UPDATESPEED_HIGH:
525         SetTimer(hWnd, 1, 500, NULL);
526         break;
527     case ID_VIEW_UPDATESPEED_NORMAL:
528         SetTimer(hWnd, 1, 2000, NULL);
529         break;
530     case ID_VIEW_UPDATESPEED_LOW:
531         SetTimer(hWnd, 1, 4000, NULL);
532         break;
533     }
534 }
535 
536 BOOL OnCreate(HWND hWnd)
537 {
538     HMENU   hMenu;
539     HMENU   hEditMenu;
540     HMENU   hViewMenu;
541     HMENU   hShutMenu;
542     HMENU   hUpdateSpeedMenu;
543     HMENU   hCPUHistoryMenu;
544     int     nActivePage;
545     int     nParts[3];
546     RECT    rc;
547     WCHAR   szTemp[256];
548     WCHAR   szLogOffItem[MAX_PATH];
549     LPWSTR  lpUserName;
550     TCITEM  item;
551     DWORD   len = 0;
552 
553     SendMessageW(hMainWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW(hInst, MAKEINTRESOURCEW(IDI_TASKMANAGER)));
554 
555     /* Initialize the Windows Common Controls DLL */
556     InitCommonControls();
557 
558     /* Get the minimum window sizes */
559     GetWindowRect(hWnd, &rc);
560     nMinimumWidth = (rc.right - rc.left);
561     nMinimumHeight = (rc.bottom - rc.top);
562 
563     /* Create the status bar */
564     hStatusWnd = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS, L"", hWnd, STATUS_WINDOW);
565     if(!hStatusWnd)
566         return FALSE;
567 
568     /* Create the status bar panes */
569     nParts[0] = STATUS_SIZE1;
570     nParts[1] = STATUS_SIZE2;
571     nParts[2] = STATUS_SIZE3;
572     SendMessageW(hStatusWnd, SB_SETPARTS, 3, (LPARAM) (LPINT) nParts);
573 
574     /* Create tab pages */
575     hTabWnd = GetDlgItem(hWnd, IDC_TAB);
576 #if 1
577     hApplicationPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_APPLICATION_PAGE), hWnd, ApplicationPageWndProc); EnableDialogTheme(hApplicationPage);
578     hProcessPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PROCESS_PAGE), hWnd, ProcessPageWndProc); EnableDialogTheme(hProcessPage);
579     hPerformancePage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PERFORMANCE_PAGE), hWnd, PerformancePageWndProc); EnableDialogTheme(hPerformancePage);
580 #else
581     hApplicationPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_APPLICATION_PAGE), hTabWnd, ApplicationPageWndProc); EnableDialogTheme(hApplicationPage);
582     hProcessPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PROCESS_PAGE), hTabWnd, ProcessPageWndProc); EnableDialogTheme(hProcessPage);
583     hPerformancePage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PERFORMANCE_PAGE), hTabWnd, PerformancePageWndProc); EnableDialogTheme(hPerformancePage);
584 #endif
585 
586     /* Insert tabs */
587     LoadStringW(hInst, IDS_TAB_APPS, szTemp, 256);
588     memset(&item, 0, sizeof(TCITEM));
589     item.mask = TCIF_TEXT;
590     item.pszText = szTemp;
591     (void)TabCtrl_InsertItem(hTabWnd, 0, &item);
592     LoadStringW(hInst, IDS_TAB_PROCESSES, szTemp, 256);
593     memset(&item, 0, sizeof(TCITEM));
594     item.mask = TCIF_TEXT;
595     item.pszText = szTemp;
596     (void)TabCtrl_InsertItem(hTabWnd, 1, &item);
597     LoadStringW(hInst, IDS_TAB_PERFORMANCE, szTemp, 256);
598     memset(&item, 0, sizeof(TCITEM));
599     item.mask = TCIF_TEXT;
600     item.pszText = szTemp;
601     (void)TabCtrl_InsertItem(hTabWnd, 2, &item);
602 
603     /* Size everything correctly */
604     GetClientRect(hWnd, &rc);
605     nOldWidth = rc.right;
606     nOldHeight = rc.bottom;
607     /* nOldStartX = rc.left; */
608     /*nOldStartY = rc.top;  */
609 
610 #define PAGE_OFFSET_LEFT    17
611 #define PAGE_OFFSET_TOP     72
612 #define PAGE_OFFSET_WIDTH   (PAGE_OFFSET_LEFT*2)
613 #define PAGE_OFFSET_HEIGHT  (PAGE_OFFSET_TOP+32)
614 
615     if ((TaskManagerSettings.Left != 0) ||
616         (TaskManagerSettings.Top != 0) ||
617         (TaskManagerSettings.Right != 0) ||
618         (TaskManagerSettings.Bottom != 0))
619     {
620         MoveWindow(hWnd, TaskManagerSettings.Left, TaskManagerSettings.Top, TaskManagerSettings.Right - TaskManagerSettings.Left, TaskManagerSettings.Bottom - TaskManagerSettings.Top, TRUE);
621 #ifdef __GNUC__TEST__
622         MoveWindow(hApplicationPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
623         MoveWindow(hProcessPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
624         MoveWindow(hPerformancePage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
625 #endif
626     }
627     if (TaskManagerSettings.Maximized)
628         ShowWindow(hWnd, SW_MAXIMIZE);
629 
630     /* Set the always on top style */
631     hMenu = GetMenu(hWnd);
632     hEditMenu = GetSubMenu(hMenu, 1);
633     hViewMenu = GetSubMenu(hMenu, 2);
634     hShutMenu = GetSubMenu(hMenu, 4);
635     hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
636     hCPUHistoryMenu  = GetSubMenu(hViewMenu, 7);
637 
638     /* Check or uncheck the always on top menu item */
639     if (TaskManagerSettings.AlwaysOnTop) {
640         CheckMenuItem(hEditMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND|MF_CHECKED);
641         SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
642     } else {
643         CheckMenuItem(hEditMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND|MF_UNCHECKED);
644         SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
645     }
646 
647     /* Check or uncheck the minimize on use menu item */
648     if (TaskManagerSettings.MinimizeOnUse)
649         CheckMenuItem(hEditMenu, ID_OPTIONS_MINIMIZEONUSE, MF_BYCOMMAND|MF_CHECKED);
650     else
651         CheckMenuItem(hEditMenu, ID_OPTIONS_MINIMIZEONUSE, MF_BYCOMMAND|MF_UNCHECKED);
652 
653     /* Check or uncheck the hide when minimized menu item */
654     if (TaskManagerSettings.HideWhenMinimized)
655         CheckMenuItem(hEditMenu, ID_OPTIONS_HIDEWHENMINIMIZED, MF_BYCOMMAND|MF_CHECKED);
656     else
657         CheckMenuItem(hEditMenu, ID_OPTIONS_HIDEWHENMINIMIZED, MF_BYCOMMAND|MF_UNCHECKED);
658 
659     /* Check or uncheck the show 16-bit tasks menu item */
660     if (TaskManagerSettings.Show16BitTasks)
661         CheckMenuItem(hEditMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_CHECKED);
662     else
663         CheckMenuItem(hEditMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_UNCHECKED);
664 
665     /* Set the view mode */
666     CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, TaskManagerSettings.ViewMode, MF_BYCOMMAND);
667 
668     if (TaskManagerSettings.ShowKernelTimes)
669         CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
670     else
671         CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
672 
673     CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, TaskManagerSettings.UpdateSpeed, MF_BYCOMMAND);
674 
675     if (TaskManagerSettings.CPUHistory_OneGraphPerCPU)
676         CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
677     else
678         CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
679 
680     nActivePage = TaskManagerSettings.ActiveTabPage;
681     TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 0);
682     TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 1);
683     TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 2);
684     TabCtrl_SetCurFocus/*Sel*/(hTabWnd, nActivePage);
685 
686     /* Set the username in the "Log Off %s" item of the Shutdown menu */
687 
688     /* 1- Get the menu item text and store it temporarily */
689     GetMenuStringW(hShutMenu, ID_SHUTDOWN_LOGOFF, szTemp, 256, MF_BYCOMMAND);
690 
691     /* 2- Retrieve the username length first, then allocate a buffer for it and call it again */
692     if (!GetUserNameW(NULL, &len) && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
693     {
694         lpUserName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR));
695         if (lpUserName && GetUserNameW(lpUserName, &len))
696         {
697             _snwprintf(szLogOffItem, sizeof(szLogOffItem)/sizeof(szLogOffItem[0]), szTemp, lpUserName);
698             szLogOffItem[sizeof(szLogOffItem)/sizeof(szLogOffItem[0]) - 1] = UNICODE_NULL;
699         }
700         else
701         {
702             _snwprintf(szLogOffItem, sizeof(szLogOffItem)/sizeof(szLogOffItem[0]), szTemp, L"n/a");
703         }
704 
705         if (lpUserName) HeapFree(GetProcessHeap(), 0, lpUserName);
706     }
707     else
708     {
709         _snwprintf(szLogOffItem, sizeof(szLogOffItem)/sizeof(szLogOffItem[0]), szTemp, L"n/a");
710     }
711 
712     /* 3- Set the menu item text to its formatted counterpart */
713     ModifyMenuW(hShutMenu, ID_SHUTDOWN_LOGOFF, MF_BYCOMMAND | MF_STRING, ID_SHUTDOWN_LOGOFF, szLogOffItem);
714 
715     /* Setup update speed */
716     SetUpdateSpeed(hWnd);
717 
718     /*
719      * Refresh the performance data
720      * Sample it twice so we can establish
721      * the delta values & cpu usage
722      */
723     PerfDataRefresh();
724     PerfDataRefresh();
725 
726     RefreshApplicationPage();
727     RefreshProcessPage();
728     RefreshPerformancePage();
729 
730     TrayIcon_AddIcon();
731 
732     return TRUE;
733 }
734 
735 /* OnMove()
736  * This function handles all the moving events for the application
737  * It moves every child window that needs moving
738  */
739 void OnMove( WPARAM nType, int cx, int cy )
740 {
741 #ifdef __GNUC__TEST__
742     MoveWindow(hApplicationPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
743     MoveWindow(hProcessPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
744     MoveWindow(hPerformancePage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
745 #endif
746 }
747 
748 /* OnSize()
749  * This function handles all the sizing events for the application
750  * It re-sizes every window, and child window that needs re-sizing
751  */
752 void OnSize( WPARAM nType, int cx, int cy )
753 {
754     int   nParts[3];
755     int   nXDifference;
756     int   nYDifference;
757     RECT  rc;
758 
759     if (nType == SIZE_MINIMIZED)
760     {
761         if(TaskManagerSettings.HideWhenMinimized)
762         {
763           ShowWindow(hMainWnd, SW_HIDE);
764         }
765         return;
766     }
767 
768     nXDifference = cx - nOldWidth;
769     nYDifference = cy - nOldHeight;
770     nOldWidth = cx;
771     nOldHeight = cy;
772 
773     /* Update the status bar size */
774     GetWindowRect(hStatusWnd, &rc);
775     SendMessageW(hStatusWnd, WM_SIZE, nType, MAKELPARAM(cx,rc.bottom - rc.top));
776 
777     /* Update the status bar pane sizes */
778     nParts[0] = bInMenuLoop ? -1 : STATUS_SIZE1;
779     nParts[1] = STATUS_SIZE2;
780     nParts[2] = cx;
781     SendMessageW(hStatusWnd, SB_SETPARTS, bInMenuLoop ? 1 : 3, (LPARAM) (LPINT) nParts);
782 
783     /* Resize the tab control */
784     GetWindowRect(hTabWnd, &rc);
785     cx = (rc.right - rc.left) + nXDifference;
786     cy = (rc.bottom - rc.top) + nYDifference;
787     SetWindowPos(hTabWnd, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
788 
789     /* Resize the application page */
790     GetWindowRect(hApplicationPage, &rc);
791     cx = (rc.right - rc.left) + nXDifference;
792     cy = (rc.bottom - rc.top) + nYDifference;
793     SetWindowPos(hApplicationPage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
794 
795     /* Resize the process page */
796     GetWindowRect(hProcessPage, &rc);
797     cx = (rc.right - rc.left) + nXDifference;
798     cy = (rc.bottom - rc.top) + nYDifference;
799     SetWindowPos(hProcessPage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
800 
801     /* Resize the performance page */
802     GetWindowRect(hPerformancePage, &rc);
803     cx = (rc.right - rc.left) + nXDifference;
804     cy = (rc.bottom - rc.top) + nYDifference;
805     SetWindowPos(hPerformancePage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
806 }
807 
808 void LoadSettings(void)
809 {
810     HKEY   hKey;
811     WCHAR  szSubKey[] = L"Software\\ReactOS\\TaskManager";
812     int    i;
813     DWORD  dwSize;
814 
815     /* Window size & position settings */
816     TaskManagerSettings.Maximized = FALSE;
817     TaskManagerSettings.Left = 0;
818     TaskManagerSettings.Top = 0;
819     TaskManagerSettings.Right = 0;
820     TaskManagerSettings.Bottom = 0;
821 
822     /* Tab settings */
823     TaskManagerSettings.ActiveTabPage = 0;
824 
825     /* Options menu settings */
826     TaskManagerSettings.AlwaysOnTop = TRUE;
827     TaskManagerSettings.MinimizeOnUse = TRUE;
828     TaskManagerSettings.HideWhenMinimized = FALSE;
829     TaskManagerSettings.Show16BitTasks = TRUE;
830 
831     /* Update speed settings */
832     TaskManagerSettings.UpdateSpeed = ID_VIEW_UPDATESPEED_NORMAL;
833 
834     /* Applications page settings */
835     TaskManagerSettings.ViewMode = ID_VIEW_DETAILS;
836 
837     /* Processes page settings */
838     TaskManagerSettings.ShowProcessesFromAllUsers = FALSE; /* It's the default */
839 
840     for (i = 0; i < COLUMN_NMAX; i++) {
841         TaskManagerSettings.Columns[i] = ColumnPresets[i].bDefaults;
842         TaskManagerSettings.ColumnOrderArray[i] = i;
843         TaskManagerSettings.ColumnSizeArray[i] = ColumnPresets[i].size;
844     }
845 
846     TaskManagerSettings.SortColumn = COLUMN_IMAGENAME;
847     TaskManagerSettings.SortAscending = TRUE;
848 
849     /* Performance page settings */
850     TaskManagerSettings.CPUHistory_OneGraphPerCPU = TRUE;
851     TaskManagerSettings.ShowKernelTimes = FALSE;
852 
853     /* Open the key */
854     if (RegOpenKeyExW(HKEY_CURRENT_USER, szSubKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
855         return;
856     /* Read the settings */
857     dwSize = sizeof(TASKMANAGER_SETTINGS);
858     RegQueryValueExW(hKey, L"Preferences", NULL, NULL, (LPBYTE)&TaskManagerSettings, &dwSize);
859 
860     /*
861      * ATM, the 'ImageName' column is always visible
862      * (and grayed in configuration dialog)
863      * This will avoid troubles if the registry gets corrupted.
864      */
865     TaskManagerSettings.Column_ImageName = TRUE;
866 
867     /* Close the key */
868     RegCloseKey(hKey);
869 }
870 
871 void SaveSettings(void)
872 {
873     HKEY hKey;
874     WCHAR szSubKey[] = L"Software\\ReactOS\\TaskManager";
875 
876     /* Open (or create) the key */
877     if (RegCreateKeyExW(HKEY_CURRENT_USER, szSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS)
878         return;
879     /* Save the settings */
880     RegSetValueExW(hKey, L"Preferences", 0, REG_BINARY, (LPBYTE)&TaskManagerSettings, sizeof(TASKMANAGER_SETTINGS));
881     /* Close the key */
882     RegCloseKey(hKey);
883 }
884 
885 void TaskManager_OnRestoreMainWindow(void)
886 {
887     //HMENU hMenu, hOptionsMenu;
888     BOOL OnTop;
889 
890     //hMenu = GetMenu(hMainWnd);
891     //hOptionsMenu = GetSubMenu(hMenu, OPTIONS_MENU_INDEX);
892     OnTop = ((GetWindowLongPtrW(hMainWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0);
893 
894     OpenIcon(hMainWnd);
895     SetForegroundWindow(hMainWnd);
896     SetWindowPos(hMainWnd, (OnTop ? HWND_TOPMOST : HWND_TOP), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
897 }
898 
899 void TaskManager_DisableStatusBar(HWND hWnd)
900 {
901     int nParts;
902 
903     /* Update the status bar pane sizes */
904     nParts = -1;
905     SendMessageW(hStatusWnd, SB_SETPARTS, 1, (LPARAM) (LPINT)&nParts);
906     SendMessageW(hStatusWnd, SB_SETTEXT, (WPARAM)0, (LPARAM)L"");
907 }
908 
909 void TaskManager_EnableStatusBar(HWND hWnd)
910 {
911     RECT   rc;
912     int    nParts[3];
913 
914     /* Update the status bar pane sizes */
915     GetClientRect(hWnd, &rc);
916     nParts[0] = STATUS_SIZE1;
917     nParts[1] = STATUS_SIZE2;
918     nParts[2] = rc.right;
919     SendMessageW(hStatusWnd, SB_SETPARTS, 3, (LPARAM) (LPINT) nParts);
920 
921     /* trigger update of status bar columns and performance page asynchronously */
922     RefreshPerformancePage();
923 }
924 
925 void TaskManager_OnMenuSelect(HWND hWnd, UINT nItemID, UINT nFlags, HMENU hSysMenu)
926 {
927     WCHAR  str[100];
928 
929     wcscpy(str, L"");
930     if (LoadStringW(hInst, nItemID, str, 100)) {
931         /* load appropriate string */
932         LPWSTR lpsz = str;
933         /* first newline terminates actual string */
934         lpsz = wcschr(lpsz, '\n');
935         if (lpsz != NULL)
936             *lpsz = '\0';
937     }
938     SendMessageW(hStatusWnd, SB_SETTEXT, 0, (LPARAM)str);
939 }
940 
941 void TaskManager_OnViewUpdateSpeed(DWORD dwSpeed)
942 {
943     HMENU  hMenu;
944     HMENU  hViewMenu;
945     HMENU  hUpdateSpeedMenu;
946 
947     hMenu = GetMenu(hMainWnd);
948     hViewMenu = GetSubMenu(hMenu, 2);
949     hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
950 
951     TaskManagerSettings.UpdateSpeed = dwSpeed;
952     CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, dwSpeed, MF_BYCOMMAND);
953 
954     KillTimer(hMainWnd, 1);
955 
956     SetUpdateSpeed(hMainWnd);
957 }
958 
959 void TaskManager_OnTabWndSelChange(void)
960 {
961     int    i;
962     HMENU  hMenu;
963     HMENU  hOptionsMenu;
964     HMENU  hViewMenu;
965     HMENU  hSubMenu;
966     WCHAR  szTemp[256];
967     SYSTEM_INFO sysInfo;
968 
969     hMenu = GetMenu(hMainWnd);
970     hViewMenu = GetSubMenu(hMenu, 2);
971     hOptionsMenu = GetSubMenu(hMenu, 1);
972     TaskManagerSettings.ActiveTabPage = TabCtrl_GetCurSel(hTabWnd);
973     for (i = GetMenuItemCount(hViewMenu) - 1; i > 2; i--) {
974         hSubMenu = GetSubMenu(hViewMenu, i);
975         if (hSubMenu)
976             DestroyMenu(hSubMenu);
977         RemoveMenu(hViewMenu, i, MF_BYPOSITION);
978     }
979     RemoveMenu(hOptionsMenu, 3, MF_BYPOSITION);
980     if (hWindowMenu)
981         DestroyMenu(hWindowMenu);
982     switch (TaskManagerSettings.ActiveTabPage) {
983     case 0:
984         ShowWindow(hApplicationPage, SW_SHOW);
985         ShowWindow(hProcessPage, SW_HIDE);
986         ShowWindow(hPerformancePage, SW_HIDE);
987         BringWindowToTop(hApplicationPage);
988 
989         LoadStringW(hInst, IDS_MENU_LARGEICONS, szTemp, 256);
990         AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_LARGE, szTemp);
991 
992         LoadStringW(hInst, IDS_MENU_SMALLICONS, szTemp, 256);
993         AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SMALL, szTemp);
994 
995         LoadStringW(hInst, IDS_MENU_DETAILS, szTemp, 256);
996         AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_DETAILS, szTemp);
997 
998         if (GetMenuItemCount(hMenu) <= 5) {
999             hWindowMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_WINDOWSMENU));
1000 
1001             LoadStringW(hInst, IDS_MENU_WINDOWS, szTemp, 256);
1002             InsertMenuW(hMenu, 3, MF_BYPOSITION|MF_POPUP, (UINT_PTR) hWindowMenu, szTemp);
1003 
1004             DrawMenuBar(hMainWnd);
1005         }
1006         CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, TaskManagerSettings.ViewMode, MF_BYCOMMAND);
1007 
1008         /*
1009          * Give the application list control focus
1010          */
1011         if (!bWasKeyboardInput)
1012             SetFocus(hApplicationPageListCtrl);
1013         break;
1014 
1015     case 1:
1016         ShowWindow(hApplicationPage, SW_HIDE);
1017         ShowWindow(hProcessPage, SW_SHOW);
1018         ShowWindow(hPerformancePage, SW_HIDE);
1019         BringWindowToTop(hProcessPage);
1020 
1021         LoadStringW(hInst, IDS_MENU_SELECTCOLUMNS, szTemp, 256);
1022         AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SELECTCOLUMNS, szTemp);
1023 
1024         LoadStringW(hInst, IDS_MENU_16BITTASK, szTemp, 256);
1025         AppendMenuW(hOptionsMenu, MF_STRING, ID_OPTIONS_SHOW16BITTASKS, szTemp);
1026 
1027         if (TaskManagerSettings.Show16BitTasks)
1028             CheckMenuItem(hOptionsMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_CHECKED);
1029         if (GetMenuItemCount(hMenu) > 5)
1030         {
1031             DeleteMenu(hMenu, 3, MF_BYPOSITION);
1032             DrawMenuBar(hMainWnd);
1033         }
1034         /*
1035          * Give the process list control focus
1036          */
1037         if (!bWasKeyboardInput)
1038             SetFocus(hProcessPageListCtrl);
1039         break;
1040 
1041     case 2:
1042         ShowWindow(hApplicationPage, SW_HIDE);
1043         ShowWindow(hProcessPage, SW_HIDE);
1044         ShowWindow(hPerformancePage, SW_SHOW);
1045         BringWindowToTop(hPerformancePage);
1046         if (GetMenuItemCount(hMenu) > 5) {
1047             DeleteMenu(hMenu, 3, MF_BYPOSITION);
1048             DrawMenuBar(hMainWnd);
1049         }
1050 
1051         GetSystemInfo(&sysInfo);
1052 
1053         /* Hide CPU graph options on single CPU systems */
1054         if (sysInfo.dwNumberOfProcessors > 1)
1055         {
1056             hSubMenu = CreatePopupMenu();
1057 
1058             LoadStringW(hInst, IDS_MENU_ONEGRAPHALLCPUS, szTemp, 256);
1059             AppendMenuW(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHALL, szTemp);
1060 
1061             LoadStringW(hInst, IDS_MENU_ONEGRAPHPERCPU, szTemp, 256);
1062             AppendMenuW(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, szTemp);
1063 
1064             LoadStringW(hInst, IDS_MENU_CPUHISTORY, szTemp, 256);
1065             AppendMenuW(hViewMenu, MF_STRING|MF_POPUP, (UINT_PTR) hSubMenu, szTemp);
1066 
1067             if (TaskManagerSettings.CPUHistory_OneGraphPerCPU)
1068                 CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
1069             else
1070                 CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
1071         }
1072 
1073         LoadStringW(hInst, IDS_MENU_SHOWKERNELTIMES, szTemp, 256);
1074         AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SHOWKERNELTIMES, szTemp);
1075 
1076         if (TaskManagerSettings.ShowKernelTimes)
1077             CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
1078         else
1079             CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
1080 
1081         /*
1082          * Give the tab control focus
1083          */
1084         if (!bWasKeyboardInput)
1085             SetFocus(hTabWnd);
1086         break;
1087     }
1088 }
1089 
1090 VOID ShowWin32Error(DWORD dwError)
1091 {
1092     LPWSTR lpMessageBuffer;
1093 
1094     if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
1095                        NULL,
1096                        dwError,
1097                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1098                        (LPWSTR)&lpMessageBuffer,
1099                        0, NULL) != 0)
1100     {
1101         MessageBoxW(hMainWnd, lpMessageBuffer, NULL, MB_OK | MB_ICONERROR);
1102         if (lpMessageBuffer) LocalFree(lpMessageBuffer);
1103     }
1104 }
1105 
1106 LPWSTR GetLastErrorText(LPWSTR lpszBuf, DWORD dwSize)
1107 {
1108     DWORD  dwRet;
1109     LPWSTR lpszTemp = NULL;
1110 
1111     dwRet = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
1112                            NULL,
1113                            GetLastError(),
1114                            LANG_NEUTRAL,
1115                            (LPWSTR)&lpszTemp,
1116                            0,
1117                            NULL );
1118 
1119     /* supplied buffer is not long enough */
1120     if (!dwRet || ( (long)dwSize < (long)dwRet+14)) {
1121         lpszBuf[0] = L'\0';
1122     } else {
1123         lpszTemp[lstrlenW(lpszTemp)-2] = L'\0';  /*remove cr and newline character */
1124         wsprintfW(lpszBuf, L"%s (0x%x)", lpszTemp, (int)GetLastError());
1125     }
1126     if (lpszTemp) {
1127         LocalFree((HLOCAL)lpszTemp);
1128     }
1129     return lpszBuf;
1130 }
1131 
1132 DWORD EndLocalThread(HANDLE *hThread, DWORD dwThread)
1133 {
1134     DWORD dwExitCodeThread = 0;
1135 
1136     if (*hThread != NULL) {
1137         PostThreadMessage(dwThread,WM_QUIT,0,0);
1138         for (;;) {
1139             MSG msg;
1140 
1141             if (WAIT_OBJECT_0 == WaitForSingleObject(*hThread, 500))
1142                 break;
1143             while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1144                 TranslateMessage(&msg);
1145                 DispatchMessage(&msg);
1146             }
1147         }
1148         GetExitCodeThread(*hThread, &dwExitCodeThread);
1149         CloseHandle(*hThread);
1150         *hThread = NULL;
1151     }
1152     return dwExitCodeThread;
1153 }
1154 
1155