I have a .net Windows Service running as Local System. I have another .net process that needs to wait for the service to terminate. That process does not know the service name so it can't query the service control manager. It does know the service process Id. I can modify sources of both the windows service and the other process.
When I do:
process = Process.GetProcessById(processId);
process.WaitForExit();
from the other process, I'm getting:
System.ComponentModel.Win32Exception: Access is denied
Stack trace:
at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.WaitForExit(Int32 milliseconds)
GetProcessHandle calls OpenProcess. Apparently the target process should allow SYNCHRONIZE bit for the above to work, which in theory can be set by SetSecurityInfo from the Windows Service. However it does not appear that there is an easy way of doing this in .NET short of calling several pinvokes to elevate, enable privilege and finally change the security.
Am I overlooking a simple way of waiting on another (system) process from a user process?