Microsoft Docs

SetProcessAffinityMask

Synopsis

BOOL SetProcessAffinityMask(
  HANDLE hProcess,
  DWORD_PTR dwProcessAffinityMask
);

Parameters

hProcess
Handle to the process whose affinity mask is to be set. The handle must have PROCESS_SET_INFORMATION access.
dwProcessAffinityMask
A bitmask that specifies the processors that the threads of the process are allowed to run on.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. Call GetLastError for more information.

Remarks

Example

#include <windows.h>
#include <stdio.h>

int main() {
    HANDLE h = GetCurrentProcess();
    DWORD_PTR mask = 0x00000003; // Use CPU 0 and 1
    if (SetProcessAffinityMask(h, mask)) {
        printf("Affinity set successfully.\n");
    } else {
        printf("Failed: %lu\n", GetLastError());
    }
    return 0;
}

See Also