I'm using Java JNA in order to access a process' memory on Windows. My code looks like this:
static WinNT.HANDLE openProcessHandle(int processId)
{
val processAccessRights = PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_TERMINATE
| PROCESS_NAME_NATIVE | PROCESS_SUSPEND_RESUME | PROCESS_QUERY_INFORMATION | PROCESS_QUERY_LIMITED_INFORMATION;
val processHandle = Kernel32.INSTANCE.OpenProcess(processAccessRights, false, processId);
if (processHandle == null)
{
val lastError = Native.getLastError();
val formatMessageFromLastErrorCode = Kernel32Util.formatMessageFromLastErrorCode(lastError);
val message = "OpenProcess() failed to open process id "
+ processId + ": " + formatMessageFromLastErrorCode;
throw new IllegalStateException(message);
}
return processHandle;
}
I'm already not using PROCESS_ALL_ACCESS due to the problems it causes.
According to this answer, I also need to enable the debug privilege for my process. However, despite calling this code successfully (e.g. all return values indicate success) before OpenProcess(), some users still get the error message OpenProcess() failed to open process id xxxx: Access denied. My application is not running as administrator. Why does it work for me and most users without administrator rights but not all users? What exactly causes this inconsistency? I would prefer to understand and tackle this problem specifically rather than making all users run my software as administrator since my software generally doesn't need those extra rights.