I tried that but it didn't help.
I found my mistake. It was in the _WinAPI_OpenProcess call. My key learnings:
1. The handle you need for _WinAPI_GetProcessAffinityMask or _WinAPI_SetProcessAffinityMask is the "process" handle not the PID or the Window Handle. Use _WinAPI_OpenProcess to get the Process Handle.
2. _WinAPI_OPenProcess needs a constant in the first variable for "$iAccess". I was only passing "$PROCESS_QUERY_INFORMATION". This is why the GetProcessAffinity was working. I also need to set "$PROCESS_SET_INFORMATION".
3. I updated my code for OpenProcess to this:
$hProc = _WinAPI_OpenProcess($PROCESS_QUERY_INFORMATION+$PROCESS_SET_INFORMATION, True, $sPID, True)
4. I could have also just used this:
$hProc = _WinAPI_OpenProcess($PROCESS_ALL_ACCESS, True, $sPID, True)
5. I actually didn't really have an issue with "Access", I was just not making the correct call in the first variable to OpenProcess. So this works fine for me in terms of "Notepad.exe" which does not require any elevated privileges.
$hProc = _WinAPI_OpenProcess($PROCESS_ALL_ACCESS, False, $sPID)
Here is the updated working code for others that run into this problem.
#include <Constants.au3>
#include <WinAPIProc.au3>
#include <WinAPIError.au3>
; Launch Notepad
Local $sPID = Run("notepad.exe")
Local $hProc
; https://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_OpenProcess.htm
; Returns a handle of an existing process object
; Success: the process handle to the object.
; Failure: sets the @error flag to non-zero.
$hProc = _WinAPI_OpenProcess($PROCESS_ALL_ACCESS, False, $sPID)
Msgbox(0, "Open Process", "Success = " & $hProc & @CRLF & "Error Return: " & _WinAPI_GetLastError() & @CRLF & "Error message = " & _WinAPI_GetLastErrorMessage())
; https://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_GetProcessAffinityMask.htm
; Obtains the affinity masks for the process and the system
;Success: Array with the following format:
; $aMask[0] - True on success, otherwise False
; $aMask[1] - Process affinity mask
; $aMask[2] - System affinity mask
;Failure: Sets the @error flag to non-zero, call _WinAPI_GetLastError() to get extended error information
Local $aRet = _WinAPI_GetProcessAffinityMask($hProc)
MsgBox(0, "Get Process", "Success:" & $aRet [0] & @CRLF & "Proc Mask:" & $aRet [1] & @CRLF & "Sys Mask:" & $aRet [2] & @CRLF & "Error Return: " & _WinAPI_GetLastError() & @CRLF & "Error Message: " & _WinAPI_GetLastErrorMessage())
; https://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_SetProcessAffinityMask.htm
; Sets a processor affinity mask for the threads of a specified process
; Success: True
; Failure: False, call _WinAPI_GetLastError() to get extended error information
Local $hSetAffinity = _WinAPI_SetProcessAffinityMask($hProc, 0x03)
Msgbox(0, "Set affinity", "Success = "&$hSetaffinity & @CRLF & "Error Return: " & _WinAPI_GetLastError() & @CRLF & "Error message = " & _WinAPI_GetLastErrorMessage())
; Closes an open object handle
_WinAPI_CloseHandle($hProc)
; Close Notepad
WinClose("Untitled - Notepad")
Exit