How to make windows service interactivity with desktop application
For some scenario we would wish our windows service that running under background, but communicate with desktop application for any purpose, for instance pop-up window for notice user that alarm something or…
Unluckily, windows OS (since Vista) was default disabled and disallowed this behavior for windows service to interactive with desktop application. So we need do a little tricky on it.
As per investigated, if we used Process.Start to invoke separate process in windows service was invalid due to the service process (system level) always stay in session 0. User process should be outside the session 0. This might case isolation problem for interactivity between user and service application.
After research more article for the session 0 isolation that point out use native Win32 Api (CreateProcessAsUser
) might be workaround for the issue. So we took an experiment on it. Let’s start created process handler that using this approach:
The key point was to created process using CreateProcessAsUser, first we need create token from the user logged’s session. Through this method, we could finally communicated our windows service and desktop application that escaped from session 0 isolation:)
Note-1, if you got error message: privilege not held by client, please make sure your service was logged on as Local System account for grant the desired permission
Note-2 this approach not working under remote desktop, if you need deal with remote desktop environment, maybe you could try this reference.
Reference
- https://www.pinvoke.net/default.aspx/advapi32.createprocessasuser
- https://stackoverflow.com/questions/25726172/launch-process-from-session-0-isolation
- http://www.piaoyi.org/c-sharp/C-window-service-winform.html
- http://www.cnblogs.com/therock/articles/2261371.html
- https://stackoverflow.com/questions/16789301/wtsqueryusertoken-gives-error-privilege-not-held