简体   繁体   English

如何让进程设置它的亲和力?

[英]How to get process to set it's affinity?

I'm trying to get the process to set it's affinity using the below program.我正在尝试使用以下程序来设置它的亲和力。 But i want to set the affinity of chrome or any other process.但我想设置 chrome 或任何其他进程的亲和力。 How to do that?怎么做?

#include <windows.h>
#include <iostream>

using namespace std;
void main(){


    HANDLE process = GetCurrentProcess();
    DWORD_PTR processAffinityMask = 8;

    BOOL success = SetProcessAffinityMask(process, processAffinityMask);
    SetPriorityClass(GetCurrentProcess(), THREAD_PRIORITY_TIME_CRITICAL);

    cout << success << endl;

    system("pause");
}

To obtain the process ID of an arbitrarily-named process you can do:要获取任意命名的进程的进程 ID,您可以执行以下操作:

#include <Windows.h>
#include <string>
#include <tlhelp32.h>

int getPID(const std::string& process_name)
{
    PROCESSENTRY32 entry;
    entry.dwSize = sizeof(PROCESSENTRY32);
    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

    if (!Process32First(snapshot, &entry)) return 0;

    do
    {
        if (strcmp(entry.szExeFile, process_name.c_str()) == 0)
        {
            CloseHandle(snapshot);
            return entry.th32ProcessID;
        }
    } while (Process32Next(snapshot, &entry));

    CloseHandle(snapshot);
    return 0;
}

You can then obtain a handle to that process as follows:然后,您可以获得该进程的句柄,如下所示:

HANDLE hProcess = OpenProcess (PROCESS_SET_INFORMATION, FALSE, pid);

And finally you can pass hProcess to SetProcessAffinityMask and SetPriorityClass in the usual way.最后,您可以以通常的方式将hProcess传递给SetProcessAffinityMaskSetPriorityClass

I believe you need to be running elevated (ie as Administrator) for this to work - and do test that OpenProcess succeeded and report the result of calling GetLastError if not.我相信你需要运行提升(即作为管理员)才能工作 - 并测试OpenProcess成功并报告调用GetLastError的结果如果没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM