Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to set the priority of all instances of an application such as Excel.exe to below normal. When I do this with wscript, it's very slow. I can do it for an open excel by running
MIDL
SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS );


How can I generalize it below?

C#
char * pProcessName="Excel.exe";
if (hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE)
     ;
else {
    proc32.dwSize=sizeof(PROCESSENTRY32);
    while((Process32Next(hSnap, &proc32)) == TRUE) {
        if(_stricmp(proc32.szExeFile, pProcessName) == 0) {
            HANDLE h=OpenProcess(PROCESS_ALL_ACCESS, TRUE, proc32.th32ProcessID); //this line is incorrect I think
            SetPriorityClass(h, BELOW_NORMAL_PRIORITY_CLASS );
            CloseHandle(h);
        }
    }
    CloseHandle(hSnap);
}
Posted
Updated 11-Jan-18 3:00am

You could try to use PROCESS_SET_INFORMATION as the first parameter... :)
 
Share this answer
 
Comments
T2102 10-Sep-10 5:02am    
Thanks, that solved my problem
Here is a complete solution. The function will be called as
SetProcessProirity(L"excel.exe",BELOW_NORMAL_PRIORITY_CLASS)


Here is the function I created:

void SetProcessProirity(LPWSTR ProcessName, int Priority)
{
    PROCESSENTRY32 proc32;
    HANDLE hSnap;
    if (hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
    if (hSnap == INVALID_HANDLE_VALUE)
    {

    }
    else
    {
        proc32.dwSize = sizeof(PROCESSENTRY32);
        while ((Process32Next(hSnap, &proc32)) == TRUE)
        {
            if (_wcsicmp(proc32.szExeFile, ProcessName) == 0)
            {
                HANDLE h = OpenProcess(PROCESS_ALL_ACCESS | PROCESS_SET_INFORMATION ,TRUE, proc32.th32ProcessID);
                SetPriorityClass(h, BELOW_NORMAL_PRIORITY_CLASS);
                CloseHandle(h);
            }
        }
        CloseHandle(hSnap);
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900