从进程ID获取进程句柄 [英] Get Process Handle From Process ID

查看:92
本文介绍了从进程ID获取进程句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Excel.exe等应用程序的所有实例的优先级设置为低于正常值。当我使用wscript执行此操作时,它非常慢。我可以通过运行

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

SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS );





如何在下面概括它?





How can I generalize it below?

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);
}

推荐答案

您可以尝试使用 PROCESS_SET_INFORMATION 作为第一个参数...:)
You could try to use PROCESS_SET_INFORMATION as the first parameter... :)


这是一个完整的解决方案。该函数将被称为

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);
    }
}


这篇关于从进程ID获取进程句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆