priority.c 3.32 KB
Newer Older
1 2 3 4 5 6
/*
 *  ReactOS Task Manager
 *
 *  priority.c
 *
 *  Copyright (C) 1999 - 2001  Brian Palmer  <brianp@reactos.org>
7
 *  Copyright (C) 2008  Vladimir Pankratov
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22
 */
23 24 25 26

#include <stdio.h>
#include <stdlib.h>

27 28 29
#include <windows.h>
#include <commctrl.h>
#include <winnt.h>
30

31 32 33
#include "taskmgr.h"
#include "perfdata.h"

34
static void DoSetPriority(DWORD priority)
35
{
36
    LVITEMW          lvitem;
37
    ULONG            Index, Count;
38
    DWORD            dwProcessId;
39 40 41
    HANDLE           hProcess;
    WCHAR            wstrErrorText[256];

42 43 44 45
    WCHAR    wszWarnMsg[255];
    WCHAR    wszWarnTitle[255];
    WCHAR    wszUnable2Change[255];

46 47 48
    LoadStringW(hInst, IDS_PRIORITY_CHANGE_MESSAGE, wszWarnMsg, ARRAY_SIZE(wszWarnMsg));
    LoadStringW(hInst, IDS_WARNING_TITLE, wszWarnTitle, ARRAY_SIZE(wszWarnTitle));
    LoadStringW(hInst, IDS_PRIORITY_UNABLE2CHANGE, wszUnable2Change, ARRAY_SIZE(wszUnable2Change));
49

50 51
    Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0);
    for (Index=0; Index<Count; Index++)
52 53 54 55
    {
        lvitem.mask = LVIF_STATE;
        lvitem.stateMask = LVIS_SELECTED;
        lvitem.iItem = Index;
56
        lvitem.iSubItem = 0;
57

58
        SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM)&lvitem);
59 60 61 62 63

        if (lvitem.state & LVIS_SELECTED)
            break;
    }

64
    Count = SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0);
65
    dwProcessId = PerfDataGetProcessId(Index);
66
    if ((Count != 1) || (dwProcessId == 0))
67 68
        return;

69
    if (MessageBoxW(hMainWnd, wszWarnMsg, wszWarnTitle, MB_YESNO|MB_ICONWARNING) != IDYES)
70 71 72 73 74 75
        return;

    hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);

    if (!hProcess)
    {
76
        GetLastErrorText(wstrErrorText, ARRAY_SIZE(wstrErrorText));
77
        MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Change, MB_OK|MB_ICONSTOP);
78 79 80
        return;
    }

81
    if (!SetPriorityClass(hProcess, priority))
82
    {
83
        GetLastErrorText(wstrErrorText, ARRAY_SIZE(wstrErrorText));
84
        MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Change, MB_OK|MB_ICONSTOP);
85 86 87 88 89
    }

    CloseHandle(hProcess);
}

90
void ProcessPage_OnSetPriorityRealTime(void)
91
{
92 93
    DoSetPriority(REALTIME_PRIORITY_CLASS);
}
94

95 96 97
void ProcessPage_OnSetPriorityHigh(void)
{
    DoSetPriority(HIGH_PRIORITY_CLASS);
98 99 100 101
}

void ProcessPage_OnSetPriorityAboveNormal(void)
{
102
    DoSetPriority(ABOVE_NORMAL_PRIORITY_CLASS);
103 104 105 106
}

void ProcessPage_OnSetPriorityNormal(void)
{
107
    DoSetPriority(NORMAL_PRIORITY_CLASS);
108 109 110 111
}

void ProcessPage_OnSetPriorityBelowNormal(void)
{
112
    DoSetPriority(BELOW_NORMAL_PRIORITY_CLASS);
113 114 115 116
}

void ProcessPage_OnSetPriorityLow(void)
{
117
    DoSetPriority(IDLE_PRIORITY_CLASS);
118
}