[英]Setting process affinity to a specific process
I want to set the process affinity to a specific process.我想将进程关联设置为特定进程。
Like: I have a process called "word.exe" with PID: 2045 How I can set the process affinity to it?像:我有一个名为“word.exe”的进程,PID:2045 如何设置进程关联?
I searched online and I didn't find much.我在网上搜索,我没有找到太多。 I only found the GetCurrentProcess(), but it sets the process affinity only to the current process.我只找到了 GetCurrentProcess(),但它仅将进程关联性设置为当前进程。
int main()
{
DWORD processID = GetCurrentProcessId();
HANDLE process = GetCurrentProcess();
DWORD_PTR processAffinityMask = 1;
BOOL success = SetProcessAffinityMask(process, processAffinityMask);
SetPriorityClass(GetCurrentProcess(), THREAD_PRIORITY_TIME_CRITICAL);
cout << success << " " << processID << endl; //returns 1 if everything goes okay
}
EDIT What I meant is: there is a substitute of GetCurrentProcess() that instead of setting the affinity to the current process, sets the affinity to a specific process that I want?编辑我的意思是:有一个 GetCurrentProcess() 的替代品,它不是设置与当前进程的关联,而是将关联设置为我想要的特定进程?
can I change the GetCurrentProcces() with another function?我可以用另一个函数更改 GetCurrentProcces() 吗? Yes.是的。
#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
HANDLE GetProcessHandleByName(const std::wstring& processName)
{
HANDLE hProcess = NULL;
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(processInfo);
HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (processesSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
Process32First(processesSnapshot, &processInfo);
if (!processName.compare(processInfo.szExeFile))
{
CloseHandle(processesSnapshot);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processInfo.th32ProcessID);
return hProcess;
}
while (Process32Next(processesSnapshot, &processInfo))
{
if (!processName.compare(processInfo.szExeFile))
{
CloseHandle(processesSnapshot);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processInfo.th32ProcessID);
return hProcess;
}
}
CloseHandle(processesSnapshot);
return hProcess;
}
Usage:用法:
HANDLE hProcess = GetProcessHandleByName(L"word.exe");
BTW: In SetPriorityClass
, There is no parameter THREAD_PRIORITY_TIME_CRITICAL
in dwPriorityClass
, Maybe you want to use SetThreadPriority
.顺便说一句:在SetPriorityClass
, THREAD_PRIORITY_TIME_CRITICAL
中没有参数dwPriorityClass
,也许您想使用SetThreadPriority
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.