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?
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.
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?
can I change the GetCurrentProcces() with another function? 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
.