priority.c 3.39 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
#include "wine/unicode.h"
32 33 34
#include "taskmgr.h"
#include "perfdata.h"

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

43 44 45 46 47 48 49
    WCHAR    wszWarnMsg[255];
    WCHAR    wszWarnTitle[255];
    WCHAR    wszUnable2Change[255];

    LoadStringW(hInst, IDS_PRIORITY_CHANGE_MESSAGE, wszWarnMsg, sizeof(wszWarnMsg)/sizeof(WCHAR));
    LoadStringW(hInst, IDS_WARNING_TITLE, wszWarnTitle, sizeof(wszWarnTitle)/sizeof(WCHAR));
    LoadStringW(hInst, IDS_PRIORITY_UNABLE2CHANGE, wszUnable2Change, sizeof(wszUnable2Change)/sizeof(WCHAR));
50

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

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

        if (lvitem.state & LVIS_SELECTED)
            break;
    }

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

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

    hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);

    if (!hProcess)
    {
77 78
        GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR));
        MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Change, MB_OK|MB_ICONSTOP);
79 80 81
        return;
    }

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

    CloseHandle(hProcess);
}

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

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

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

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

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

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