I have a service running in Windows 7. In Windows 7 all services run in Session 0. From that service I want to create an interactive user session (in a session other than Session 0) and start an application in that session. My problem is that when I call LogonUser to start an interactive user session and then use CreateProcessAsUser to start the application the application ends up running in Session 0.
All of my code is C#.
Here is the relevant code:
static extern bool LogonUser(
out IntPtr token);
static extern bool CreateProcessAsUser(
ref STARTUPINFO lpStartupInfo,
ref PROCESS_INFORMATION lpProcessInformation);
LogonUser("UserName", ".", "Password",
LogonTypes.Interactive,LogonProviders.Default, out token)
<code to impersonate user>
string hd = Environment.ExpandEnvironmentVariables("%USERPROFILE%");
IntPtr envBlock = IntPtr.Zero;
"WinSta0\\Default", hd, envBlock, "Menu");
Can anyone tell me what I'm doing wrong?
Tons of things can go wrong when trying to launch a process from a service in Vista/7. I would recommend that you start with this article and adapt it to your needs. I can tell you that I've used and modified the code in the article quite a bit, and it works. I'm sorry I can't show it to you because the modified code belongs to my company.