-3

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?

VikSn0w
  • 43
  • 4
  • Why do you set the current process to something and not pid:2045? – florgeng Jul 02 '19 at 13:33
  • The function used in your snippet has a detailed explanation on MSDN. What exactly is unclear? – SergeyA Jul 02 '19 at 13:35
  • 1
    Your code explicitly modifies the affinity mask for the current process. Why would you expect it to modify the affinity mask for any other process? – David Heffernan Jul 02 '19 at 14:05
  • The thing that is unclear is: how I can set the process affinity to a specific process and can I change the GetCurrentProcces() with another function? There is like a substitute that instead of setting the process affinity to the current process, sets the process affinity to a specific process? – VikSn0w Jul 02 '19 at 14:20
  • 2
    `OpenProcess` is your friend. You should have been able to see that from the documentation to `SetProcessAffinityMask`, where it documents the process handle. – David Heffernan Jul 02 '19 at 15:09

1 Answers1

1

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.

Drake Wu
  • 6,927
  • 1
  • 7
  • 30