如何开始从窗口服务流程为当前登录的用户的会话流程、窗口、用户

2023-09-02 01:27:13 作者:有缘v有份

我要开始从Windows服务程序。该方案是一个用户界面的应用程序。而且该应用程序应该在特定的用户帐户启动。

I need to start a program from Windows Service. That program is a user UI application. Moreover that application should be started under specific user account.

的问题是,在会话#0运行一个窗口服务,但是一个登录用户的会话是1,2-等

The problem is that a Window Services run in session #0, but a logged in user sessions are 1,2 etc.

所以,问题是:如何开始从一个窗口服务流程以这样一种方式,它运行在当前登录用户的会话

So the question is: how to start a process from a window service in such a way that it run in currently logged in user's session?

我倒是强调说,问题不在于如何在特定的帐户启动一个进程(这是显而易见的 - 的Process.Start(新的ProcessStartInfo(){用户名= ..,密码= ..}) )。即使我安装我的窗户下,当前的用户帐户运行该服务将在会议#0运行反正。 设置允许服务与桌面交互没有帮助。

I'd emphasis on that the question is not about how to start a process under specific account (it's obvious - Process.Start(new ProcessStartInfo("..") { UserName=..,Password=..})). Even if I install my windows to run under current user account the service will run in session #0 anyway. Setting "Allow service to interact with desktop" doesn't help.

我的Windows服务是基于.NET的。

My windows service is .net-based.

更新: 首先,.NET无关这里,它实际上是纯粹的Win32的东西。 下面是我在做什么。下面code是在我的窗口服务(通过P / Inkove使用Win32函数的C#,我跳过导入签名,他们都在这里 - http://www.pinvoke.net/default.aspx/advapi32/CreateProcessWithLogonW.html):

UPDATE: first of all, .NET has nothing to do here, it's actually pure Win32 thing. Here's what I'm doing. The following code is in my windows service (C# using win32 function via P/Inkove, I skipped import signatures, they're all here - http://www.pinvoke.net/default.aspx/advapi32/CreateProcessWithLogonW.html):

    var startupInfo = new StartupInfo()
        {
            lpDesktop = "WinSta0\Default",
            cb = Marshal.SizeOf(typeof(StartupInfo)),
        };
    var processInfo = new ProcessInformation();
    string command = @"c:windowsNotepad.exe";
    string user = "Administrator";
    string password = "password";
    string currentDirectory = System.IO.Directory.GetCurrentDirectory();
    try
    {
        bool bRes = CreateProcessWithLogonW(user, null, password, 0,
            command, command, 0,
            Convert.ToUInt32(0),
            currentDirectory, ref startupInfo, out processInfo);
        if (!bRes)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
    }
    catch (Exception ex)
    {
        writeToEventLog(ex);
        return;
    }
    WaitForSingleObject(processInfo.hProcess, Convert.ToUInt32(0xFFFFFFF));
    UInt32 exitCode = Convert.ToUInt32(123456);
    GetExitCodeProcess(processInfo.hProcess, ref exitCode);
    writeToEventLog("Notepad has been started by WatchdogService. Exitcode: " + exitCode);

    CloseHandle(processInfo.hProcess);
    CloseHandle(processInfo.hThread);

在code呈上行记事本已经开始了WatchdogService退出code:+退出code。退出code是3221225794。 而且也没有任何新的记事本开始。 我在哪里错了?

The code goes to the line "Notepad has been started by WatchdogService. Exitcode: " + exitCode. Exitcode is 3221225794. And there's no any new notepad started. Where am I wrong?

推荐答案

我已经找到了解决方案 - http://blogs.msdn.com/b/winsdk/archive/2009/07/14/launching-an-interactive-process-from-windows-service-in-windows-vista-and-later.aspx - 这是关于Vista / 7上启动交互式会话一个新的进程,从窗口服务了不起的有益交

I've found the solution - http://blogs.msdn.com/b/winsdk/archive/2009/07/14/launching-an-interactive-process-from-windows-service-in-windows-vista-and-later.aspx - it's terrific helpful post about starting a new process in interactive session from windows service on Vista/7.