c# STARTUPINFO标志,用于显示XP中从服务启动的进程的UI

本文关键字:服务 启动 进程 UI XP 标志 STARTUPINFO 用于 显示 | 更新日期: 2023-09-27 18:06:57

我正在从XP的windows服务启动一个进程,我只是启动这个进程,而不是试图与它交互。流程启动,但UI未显示。我相信我需要在STARTUPINFO中设置一些标志以使过程可见,并希望有人可以显示如何以及设置哪些标志。

    sPath = @"C:'Windows'notepad.exe";
    string Message = string.Empty;
    // Variables
    PROCESS_INFORMATION processInfo = new PROCESS_INFORMATION();
    STARTUPINFO startInfo = new STARTUPINFO();
    Boolean bResult = false;
    IntPtr hToken = IntPtr.Zero;
    try
    {
        // Logon user
        bResult = LogonUser(
            "Test",
            "VirtualXP-23639",
            "test",
            LogonType.LOGON32_LOGON_INTERACTIVE,
            LogonProvider.LOGON32_PROVIDER_DEFAULT,
            out hToken
        );
        if (!bResult) { throw new Exception("Logon error #" + Marshal.GetLastWin32Error()); }
        // Create process
        startInfo.cb = Marshal.SizeOf(startInfo);
        startInfo.lpDesktop = "winsta0''default";
        bResult = CreateProcessAsUser(
            hToken,
            null,
            sPath,
            IntPtr.Zero,
            IntPtr.Zero,
            false,
            0,
            IntPtr.Zero,
            null,
            ref startInfo,
            out processInfo
        );
        if (!bResult)
        {
            Message = "Failed to Create Process on Desktop/Console.  Code=" + Marshal.GetLastWin32Error().ToString();
            Logging.LogError(Ascension.CM.Common.Enums.ApplicationModuleEnums.Service, Message, "Ascension.CM.ServiceWorker.ProcessLauncher.XpLaunchDesktopProcess", null);
        }

    }
    finally
    {
        // Close all handles
        CloseHandle(hToken);
        CloseHandle(processInfo.hProcess);
        CloseHandle(processInfo.hThread);
    }
}

c# STARTUPINFO标志,用于显示XP中从服务启动的进程的UI

您至少需要允许服务与桌面交互,所以在服务中。Msc,点击你的服务,进入属性,然后登录,选择允许与桌面交互

我建议你在。net框架中使用Process类。

Process.Start("notepad.exe")

谢谢大家,但我已经找到了一个解决方案。

我最终使用WTSQueryUserToken来获得当前登录的用户,然后使用DuplicateTokenEx来获得一个令牌,我使用CreateProcessAsUser来启动该进程。

XP使用会话id 0, win7使用WTSGetActiveConsoleSessionId获取当前会话id。

不需要使用"Allow to interaction with Desktop"属性就可以了。

谢谢