Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Windows 7中从Windows服务启动交互式进程_C#_Windows_Visual Studio_Winapi_Service - Fatal编程技术网

C# 在Windows 7中从Windows服务启动交互式进程

C# 在Windows 7中从Windows服务启动交互式进程,c#,windows,visual-studio,winapi,service,C#,Windows,Visual Studio,Winapi,Service,我正在尝试从Windows服务启动一个交互式进程。 正如您所知,这在Windows 7中很棘手,因为服务在另一个会话中运行,但我知道这是可能的: userToken为0。如果我将sessionId硬编码为1,则userToken不是0,但进程不会启动(服务不会抛出任何错误) 我的服务不会启动任何进程。如果我使用createprocess,它会启动进程,但它不在交互式桌面上 编辑: 我设法用这段代码启动了交互式程序。我将lpDesktop修改为string.empty 现在仍然存在的问题是: -

我正在尝试从Windows服务启动一个交互式进程。 正如您所知,这在Windows 7中很棘手,因为服务在另一个会话中运行,但我知道这是可能的:

userToken为0。如果我将sessionId硬编码为1,则userToken不是0,但进程不会启动(服务不会抛出任何错误)

我的服务不会启动任何进程。如果我使用createprocess,它会启动进程,但它不在交互式桌面上

编辑:

我设法用这段代码启动了交互式程序。我将lpDesktop修改为string.empty

现在仍然存在的问题是: -如何在winlogon上启动它?或者更好…在系统方面。
当我在会话0(服务所在地)上启动程序时,程序以系统帐户运行(有点明显),但在会话1上运行时,程序以用户帐户运行。

不清楚为什么您决定硬编码会话号而不是使用WTSGetActiveConsoleSessionId()。并跳过所需的错误检查。而且不要记录你的pinvoke声明。这些懒惰的快捷方式当然会产生无法诊断的问题和无法回答的问题。如果您希望在与服务(即系统)相同的安全上下文中启动新流程,则不应使用WTSQueryUserToken。相反,复制您自己的令牌并使用SetTokenInformation更改会话ID。请注意,在交互式会话中启动高权限进程是一个非常危险的想法。
        IntPtr hToken2 = IntPtr.Zero;
       //   WTSQueryUserToken(WTSGetActiveConsoleSessionId(), out userToken);
        WTSQueryUserToken(1, out userToken);
        IntPtr newToken = IntPtr.Zero;
        SECURITY_ATTRIBUTES tokenAttributes = new SECURITY_ATTRIBUTES();
        tokenAttributes.nLength = Marshal.SizeOf(tokenAttributes);
        SECURITY_ATTRIBUTES threadAttributes = new SECURITY_ATTRIBUTES();
        threadAttributes.nLength = Marshal.SizeOf(threadAttributes);
       if (!DuplicateTokenEx(userToken, 0x10000000, ref tokenAttributes, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation,
            TOKEN_TYPE.TokenImpersonation, out newToken))
        {
            throw new Exception("ERROR: DuplicateTokenEx returned false - " + Marshal.GetLastWin32Error());
        }
        TOKEN_PRIVILEGES tokPrivs = new TOKEN_PRIVILEGES();
        tokPrivs.PrivilegeCount = 1;
        LUID seDebugNameValue = new LUID();
        if (!LookupPrivilegeValue(null, SE_DEBUG_NAME, out seDebugNameValue))
        {
            throw new Exception("ERROR: LookupPrivilegeValue returned false - " + Marshal.GetLastWin32Error());
        }
        tokPrivs.Privileges = new LUID_AND_ATTRIBUTES[1];
        tokPrivs.Privileges[0].Luid = seDebugNameValue;
        tokPrivs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

        if (!AdjustTokenPrivileges(newToken, false, ref tokPrivs, 0, IntPtr.Zero, IntPtr.Zero))
        {
            throw new Exception("ERROR: AdjustTokenPrivileges returned false - " + Marshal.GetLastWin32Error());
        }
        PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
        STARTUPINFO si = new STARTUPINFO();
        si.cb = Marshal.SizeOf(si);

       // si.lpDesktop = "Winsta0\\Winlogon";
        si.lpDesktop = string.Empty;
        if (!CreateProcessAsUser(newToken, "calc.exe", null, ref tokenAttributes, ref threadAttributes,
            true, (int)CreateProcessFlags.CREATE_NEW_CONSOLE | (int)CreateProcessFlags.INHERIT_CALLER_PRIORITY, IntPtr.Zero,
            "C:\\Windows\\System32", ref si, out pi))
        {
            throw new Exception ("ERROR: CreateProcessAsUser returned false - " + Marshal.GetLastWin32Error());
        }