SetProcessAffinityMask

Header: WinBase.h

Library: Kernel32.lib

Synopsis

BOOL WINAPI SetProcessAffinityMask(
  _In_ HANDLE hProcess,
  _In_ DWORD_PTR dwProcessAffinityMask
);

Parameters

NameDescription
hProcessHandle to the process whose affinity mask is to be set. The handle must have PROCESS_SET_INFORMATION access.
dwProcessAffinityMaskBitmask 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

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;
}

See Also