曲径通幽论坛

标题: GetPriorityClass()/SetPriorityClass() -- 获取/设置指定进程优先级类 [打印本页]

作者: beyes    时间: 2012-3-4 04:06
标题: GetPriorityClass()/SetPriorityClass() -- 获取/设置指定进程优先级类
GetPriorityClass() 和 SetPriorityClass() 函数原型分别如下:
[C++] 纯文本查看 复制代码
DWORD WINAPI GetPriorityClass(
  __in  HANDLE hProcess
);
BOOL WINAPI SetPriorityClass(
  __in  HANDLE hProcess,
  __in  DWORD dwPriorityClass
);

GetPriorityClass() 和 SetPriorityClass() 函数分别用来获取和设置指定进程的优先级类,前者只有一个参数,即指定进程的句柄;后者的第 1 个参数 hProcess 表示要设置进程的句柄,第 2 个参数 dwPriorityClass 是要设置的优先级类。

优先级类有:
ABOVE_NORMAL_PRIORITY_CLASS : 在正常优先级之上
BELOW_NORMAL_PRIORITY_CLASS : 在正常优先级之下
HIGH_PRIORITY_CLASS : 高优先级
IDLE_PRIORITY_CLASS : 空闲优先级
NORMAL_PRIORITY_CLASS : 正常优先级
REALTIME_PRIORITY_CLASS : 实时优先级

测试代码:
[C++] 纯文本查看 复制代码
#include "stdafx.h"
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    printf ("My PID is %d\n", GetCurrentProcessId());
    printf ("my Handle is %d\n", GetCurrentProcess());
    HANDLE hMyselfHandle = OpenProcess (PROCESS_ALL_ACCESS, TRUE, GetCurrentProcessId());

    printf ("Get my PID through My real handle: %d\n", GetProcessId(hMyselfHandle));

    printf ("Priority: 0x%.8x\n", GetPriorityClass(hMyselfHandle));


    SetPriorityClass(hMyselfHandle, HIGH_PRIORITY_CLASS);

    printf ("Priority: 0x%.8x\n", GetPriorityClass(hMyselfHandle));
    return 0;
}

运行输出:
My PID is 1092
my Handle is -1
Get my PID through My real handle: 1092
Priority: 0x00000020
Priority: 0x00000080
上面程序中,OpenProcess() 函数为当前进程创建了一个真实的句柄,GetCurrentProcess() 函数返回当前进程的一个虚句柄。

优先级类值 0x00000020 代表 NORMAL_PRIORITY_CLASS ; 被修改后变为高优先级类,即 HIGH_PRIORITY_CLASS  对应值为 0x00000080 。




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2