How to Start a Process in Session 1 from a Windows 7 Service

自作多情 提交于 2019-12-10 18:25:18

问题


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:

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool LogonUser(
    string principal,
    string authority,
    string password,
    UInt32 logonType,
    UInt32 logonProvider,
    out    IntPtr token);

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool CreateProcessAsUser(
    IntPtr hToken,
    string lpApplicationName,
    string lpCommandLine,
    IntPtr lpProcessAttributes,
    IntPtr lpThreadAttributes,
    bool bInheritHandles,
    int dwCreationFlags,
    IntPtr lpEnvironment,
    string lpCurrentDirectory,
    ref STARTUPINFO lpStartupInfo,
    ref PROCESS_INFORMATION lpProcessInformation);

IntPtr token;
LogonUser("UserName", ".", "Password", 
    LogonTypes.Interactive,LogonProviders.Default, out token)

<code to impersonate user>
string hd = Environment.ExpandEnvironmentVariables("%USERPROFILE%");

IntPtr envBlock = IntPtr.Zero;
CreateProcessAsUser(token, "PathToMenu.exe",
    NORMAL_PRIORITY_CLASS |CREATE_UNICODE_ENVIRONMENT,
    "WinSta0\\Default", hd, envBlock, "Menu");

Can anyone tell me what I'm doing wrong?


回答1:


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.



来源:https://stackoverflow.com/questions/7824170/how-to-start-a-process-in-session-1-from-a-windows-7-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!