SetProcessAffinityMask
Header: WinBase.h
Library: Kernel32.lib
Synopsis
BOOL WINAPI SetProcessAffinityMask(
_In_ HANDLE hProcess,
_In_ DWORD_PTR dwProcessAffinityMask
);
Parameters
| Name | Description |
|---|---|
hProcess | Handle to the process whose affinity mask is to be set. The handle must have PROCESS_SET_INFORMATION access. |
dwProcessAffinityMask | Bitmask that specifies the processors that the process is allowed to run on. Each bit set represents a logical processor. |
Return Value
Returns non‑zero if the function succeeds; otherwise returns zero. Use GetLastError for extended error information.
Remarks
- The affinity mask cannot contain processors that are not present in the system.
- If the mask is zero, the function fails with
ERROR_INVALID_PARAMETER. - Changing the affinity mask of a process may affect its performance and scheduling behavior.
- On systems with more than 64 logical processors, the affinity mask is limited to the lowest 64 processors.
Example
#include <windows.h>
#include <stdio.h>
int main(void)
{
HANDLE hProc = GetCurrentProcess();
DWORD_PTR mask = 0x0000000F; // Use the first four logical processors
if (!SetProcessAffinityMask(hProc, mask))
{
printf("Failed: %lu\n", GetLastError());
return 1;
}
printf("Affinity mask set successfully.\n");
return 0;
}