In Windows Vista/7/2008/2008R2, is it at all possible to launch a process in a user’s session from a service? Specifically, the local session would be most useful.
Everything I’ve been reading seems to say this isn’t possible, but I figured I’d ask here before giving up completely.
I’m coding in VB.NET, but will take suggestions in anything.
It is really possible. The main problem which you have is that Windows should be seen as a terminal server and a users session as a remote session. Your service should be able to start a process which run in the remote session belongs to the user.
By the way, if you write a service which run under Windows XP which is not added to a domain and the fast user switching is activated, you can have the same problems to start a process on running on the second (third and so on) logged users desktop.
I hope you have a user token, which you receive for example with respect of impersonation or you have a
dwSessionIdof session. If you don’t have it you can try use some WTS-function (Remote Desktop Services API http://msdn.microsoft.com/en-us/library/aa383464.aspx, for exampleWTSEnumerateProcessesorWTSGetActiveConsoleSessionId) or LSA-API to find out the corresponding users session (LsaEnumerateLogonSessionssee http://msdn.microsoft.com/en-us/library/aa378275.aspx andLsaGetLogonSessionDatasee http://msdn.microsoft.com/en-us/library/aa378290.aspx) orProcessIdToSessionId(see http://msdn.microsoft.com/en-us/library/aa382990.aspx).You can use
GetTokenInformationfunction with the parameterTokenSessionId(see http://msdn.microsoft.com/en-us/library/aa446671.aspx) to receive the session iddwSessionIdof the users session if you knows the users tokenhClient.This code only a schema.
EnablePrivilegeis a simple function usedAdjustTokenPrivilegesto enableSE_TCB_NAMEprivilege (see http://msdn.microsoft.com/en-us/library/aa446619.aspx as a template). It is important that the process from which you are start a process you have TCB privilege, but if your service run under the Local System you have enough permissions. By the way, following code fragment work with not only Local System account, but the account must haveSE_TCB_NAMEprivilege to be able to switch current terminal server session.One more remark. In the code above we start new process with the same account as the current process have (for example Local System). You change change a code to use another account for example the users token
hClient. It is only important to have aprimary token. If you have an impersonation token you can convert it to the primary token exactly like in the code above.In the
STARTUPINFOstructure used inCreateProcessAsUseryou should uselpDesktop =WinSta0\Default”.Depend on your requirements it could be also needed to use
CreateEnvironmentBlockto create an new environment block that you will be passing to the new process.I recommend you also to read How to ensure process window launched by Process.Start(ProcessStartInfo) has focus of all Forms? where I describe how to force that the process will be started in foreground on the users desktop.