Search code examples
c++windowsopenprocess

OpenProcess / SetProcessAffinityMask on process started by my service


In my manager.exe, I'm trying to change the CPU usage of my worker.exe, which is started by my service.exe. Service.exe is running under the System Account, while manager.exe is running under the logged in user's account.

When I call OpenProcess in manager.exe with worker.exe's PID, I get NULL for procHandle:

HANDLE procHandle = OpenProcess(PROCESS_SET_INFORMATION, 0, pid);

Using GetLastError() I see that I got an Access Denied error.

Any ways around this? Can I somehow modify worker.exe to grant other processes full control over itself?


Solution

  • You shouldn't have to call OpenProcess.

    The service should already have a full-permission handle to the worker from when it called CreateProcessAsUser or CreateProcessWithLogonW. Use DuplicateHandle to make a version of that handle suitable for use by the manager process, and then have the service send that handle to the manager. The service already has a handle to the manager, right? It will need that for DuplicateHandle.

    Or have the manager ask the service to change the worker process.