首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >启动windows服务并启动cmd

启动windows服务并启动cmd
EN

Stack Overflow用户
提问于 2010-11-11 02:36:58
回答 4查看 16.3K关注 0票数 16

需要启用Interactive desktp才能工作吗?启动EXE或cmd窗口的正确代码是什么?我仍然无法启动该服务,即使我已经启用了它与桌面交互。

我将使用聊天引擎,以便更容易作为windows服务进行管理。

我的代码有什么问题?

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading;

namespace MyNewService
{
    class Program : ServiceBase
    {
        static void Main(string[] args)
        {
        }

        public Program()
        {
            this.ServiceName = "Chatter";
        }

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);

            //TODO: place your start code here
            ThreadStart starter = new ThreadStart(bw_DoWork);
            Thread t = new Thread(starter);
            t.Start();

        }

        private void bw_DoWork()
        {
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo(@"C:\Windows\system32\cmd.exe");
            p.Start();
            p.WaitForExit();
            base.Stop();
        }

        protected override void OnStop()
        {
            base.OnStop();

            //TODO: clean up any variables and stop any threads
        }
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-11-11 02:42:42

我已经经历了这样做的所有痛苦。

在Windows7/Vista/2008下,如果不调用一些Win API,则无法从服务加载任何交互进程。=黑魔法

看看herehere

下面的代码做到了这一点,使用它的风险自负:

代码语言:javascript
复制
public static class ProcessAsCurrentUser
{

    /// <summary>
    /// Connection state of a session.
    /// </summary>
    public enum ConnectionState
    {
        /// <summary>
        /// A user is logged on to the session.
        /// </summary>
        Active,
        /// <summary>
        /// A client is connected to the session.
        /// </summary>
        Connected,
        /// <summary>
        /// The session is in the process of connecting to a client.
        /// </summary>
        ConnectQuery,
        /// <summary>
        /// This session is shadowing another session.
        /// </summary>
        Shadowing,
        /// <summary>
        /// The session is active, but the client has disconnected from it.
        /// </summary>
        Disconnected,
        /// <summary>
        /// The session is waiting for a client to connect.
        /// </summary>
        Idle,
        /// <summary>
        /// The session is listening for connections.
        /// </summary>
        Listening,
        /// <summary>
        /// The session is being reset.
        /// </summary>
        Reset,
        /// <summary>
        /// The session is down due to an error.
        /// </summary>
        Down,
        /// <summary>
        /// The session is initializing.
        /// </summary>
        Initializing
    }


    [StructLayout(LayoutKind.Sequential)]
    class SECURITY_ATTRIBUTES
    {
        public int nLength;
        public IntPtr lpSecurityDescriptor;
        public int bInheritHandle;
    }


    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    struct STARTUPINFO
    {
        public Int32 cb;
        public string lpReserved;
        public string lpDesktop;
        public string lpTitle;
        public Int32 dwX;
        public Int32 dwY;
        public Int32 dwXSize;
        public Int32 dwYSize;
        public Int32 dwXCountChars;
        public Int32 dwYCountChars;
        public Int32 dwFillAttribute;
        public Int32 dwFlags;
        public Int16 wShowWindow;
        public Int16 cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public int dwProcessId;
        public int dwThreadId;
    }

    enum LOGON_TYPE
    {
        LOGON32_LOGON_INTERACTIVE = 2,
        LOGON32_LOGON_NETWORK,
        LOGON32_LOGON_BATCH,
        LOGON32_LOGON_SERVICE,
        LOGON32_LOGON_UNLOCK = 7,
        LOGON32_LOGON_NETWORK_CLEARTEXT,
        LOGON32_LOGON_NEW_CREDENTIALS
    }

    enum LOGON_PROVIDER
    {
        LOGON32_PROVIDER_DEFAULT,
        LOGON32_PROVIDER_WINNT35,
        LOGON32_PROVIDER_WINNT40,
        LOGON32_PROVIDER_WINNT50
    }

    [Flags]
    enum CreateProcessFlags : uint
    {
        CREATE_BREAKAWAY_FROM_JOB = 0x01000000,
        CREATE_DEFAULT_ERROR_MODE = 0x04000000,
        CREATE_NEW_CONSOLE = 0x00000010,
        CREATE_NEW_PROCESS_GROUP = 0x00000200,
        CREATE_NO_WINDOW = 0x08000000,
        CREATE_PROTECTED_PROCESS = 0x00040000,
        CREATE_PRESERVE_CODE_AUTHZ_LEVEL = 0x02000000,
        CREATE_SEPARATE_WOW_VDM = 0x00000800,
        CREATE_SHARED_WOW_VDM = 0x00001000,
        CREATE_SUSPENDED = 0x00000004,
        CREATE_UNICODE_ENVIRONMENT = 0x00000400,
        DEBUG_ONLY_THIS_PROCESS = 0x00000002,
        DEBUG_PROCESS = 0x00000001,
        DETACHED_PROCESS = 0x00000008,
        EXTENDED_STARTUPINFO_PRESENT = 0x00080000,
        INHERIT_PARENT_AFFINITY = 0x00010000
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct WTS_SESSION_INFO
    {
        public int SessionID;
        [MarshalAs(UnmanagedType.LPTStr)]
        public string WinStationName;
        public ConnectionState State;
    }

    [DllImport("wtsapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern Int32 WTSEnumerateSessions(IntPtr hServer, int reserved, int version,
                                                    ref IntPtr sessionInfo, ref int count);


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

    [DllImport("wtsapi32.dll")]
    public static extern void WTSFreeMemory(IntPtr memory);

    [DllImport("kernel32.dll")]
    private static extern UInt32 WTSGetActiveConsoleSessionId();

    [DllImport("wtsapi32.dll", SetLastError = true)]
    static extern int WTSQueryUserToken(UInt32 sessionId, out IntPtr Token);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public extern static bool DuplicateTokenEx(
        IntPtr hExistingToken,
        uint dwDesiredAccess,
        IntPtr lpTokenAttributes,
        int ImpersonationLevel,
        int TokenType,
        out IntPtr phNewToken);

    private const int TokenImpersonation = 2;
    private const int SecurityIdentification = 1;
    private const int MAXIMUM_ALLOWED = 0x2000000;
    private const int TOKEN_DUPLICATE = 0x2;
    private const int TOKEN_QUERY = 0x00000008;

    /// <summary>
    /// Launches a process for the current logged on user if there are any.
    /// If none, return false as well as in case of 
    /// 
    /// ##### !!! BEWARE !!! ####  ------------------------------------------
    /// This code will only work when running in a windows service (where it is really needed)
    /// so in case you need to test it, it needs to run in the service. Reason
    /// is a security privileg which only services have (SE_??? something, cant remember)!
    /// </summary>
    /// <param name="processExe"></param>
    /// <returns></returns>
    public static bool CreateProcessAsCurrentUser(string processExe)
    {

        IntPtr duplicate = new IntPtr();
        STARTUPINFO info = new STARTUPINFO();
        PROCESS_INFORMATION procInfo = new PROCESS_INFORMATION();

        Debug.WriteLine(string.Format("CreateProcessAsCurrentUser. processExe: " + processExe));

        IntPtr p = GetCurrentUserToken();

        bool result = DuplicateTokenEx(p, MAXIMUM_ALLOWED | TOKEN_QUERY | TOKEN_DUPLICATE, IntPtr.Zero, SecurityIdentification, SecurityIdentification, out duplicate);
        Debug.WriteLine(string.Format("DuplicateTokenEx result: {0}", result));
        Debug.WriteLine(string.Format("duplicate: {0}", duplicate));


        if (result)
        {
            result = CreateProcessAsUser(duplicate, processExe, null,
                IntPtr.Zero, IntPtr.Zero, false, (UInt32)CreateProcessFlags.CREATE_NEW_CONSOLE, IntPtr.Zero, null,
                ref info, out procInfo);
            Debug.WriteLine(string.Format("CreateProcessAsUser result: {0}", result));

        }


        if (p.ToInt32() != 0)
        {
            Marshal.Release(p);
            Debug.WriteLine(string.Format("Released handle p: {0}", p));
        }


        if (duplicate.ToInt32() != 0)
        {
            Marshal.Release(duplicate);
            Debug.WriteLine(string.Format("Released handle duplicate: {0}", duplicate));
        }



        return result;
    }

    public static int GetCurrentSessionId()
    {
        uint sessionId = WTSGetActiveConsoleSessionId();
        Debug.WriteLine(string.Format("sessionId: {0}", sessionId));

        if (sessionId == 0xFFFFFFFF)
            return -1;
        else
            return (int)sessionId;
    }

    public static bool IsUserLoggedOn()
    {
        List<WTS_SESSION_INFO> wtsSessionInfos = ListSessions();
        Debug.WriteLine(string.Format("Number of sessions: {0}", wtsSessionInfos.Count));
        return wtsSessionInfos.Where(x => x.State == ConnectionState.Active).Count() > 0;
    }

    private static IntPtr GetCurrentUserToken()
    {
        List<WTS_SESSION_INFO> wtsSessionInfos = ListSessions();
        int sessionId = wtsSessionInfos.Where(x => x.State == ConnectionState.Active).FirstOrDefault().SessionID;
        //int sessionId = GetCurrentSessionId();

        Debug.WriteLine(string.Format("sessionId: {0}", sessionId));
        if (sessionId == int.MaxValue)
        {
            return IntPtr.Zero;
        }
        else
        {
            IntPtr p = new IntPtr();
            int result = WTSQueryUserToken((UInt32)sessionId, out p);
            Debug.WriteLine(string.Format("WTSQueryUserToken result: {0}", result));
            Debug.WriteLine(string.Format("WTSQueryUserToken p: {0}", p));

            return p;
        }
    }

    public static List<WTS_SESSION_INFO> ListSessions()
    {
        IntPtr server = IntPtr.Zero;
        List<WTS_SESSION_INFO> ret = new List<WTS_SESSION_INFO>();

        try
        {
            IntPtr ppSessionInfo = IntPtr.Zero;

            Int32 count = 0;
            Int32 retval = WTSEnumerateSessions(IntPtr.Zero, 0, 1, ref ppSessionInfo, ref count);
            Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));

            Int64 current = (int)ppSessionInfo;

            if (retval != 0)
            {
                for (int i = 0; i < count; i++)
                {
                    WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO));
                    current += dataSize;

                    ret.Add(si);
                }

                WTSFreeMemory(ppSessionInfo);
            }
        }
        catch (Exception exception)
        {
            Debug.WriteLine(exception.ToString());
        }

        return ret;
    }

}
票数 31
EN

Stack Overflow用户

发布于 2010-11-11 02:58:12

当作为服务运行时,你将不能启动任何需要与桌面交互的东西,或者会产生自己的窗口。

正如Aliostad所说,您需要调用CreateProcessAsUser的Win调用并模拟用户,才能使用它。这涉及到模拟已登录的用户,并使用他们的凭据将进程“提升”到进程隔离级别1(该级别允许您访问窗口系统和GPU等)。

我是在我写的一个应用程序中这样做的,它确实起作用了,但我同意Aliostad的观点,有一些黑魔法在发生,它通常很糟糕

话虽如此,只要它们不需要处于进程隔离级别1的东西(窗口、GPU等),您就可以从服务中派生工作进程。

默认情况下,cmd.exe会尝试创建一个窗口,这就是您的示例失败的原因。您可以设置以下ProcessStartInfo属性以使其正常工作。

CreateNoWindow WindowStyle

票数 5
EN

Stack Overflow用户

发布于 2013-05-07 16:02:02

我写了一个应用程序看门狗服务,它只是简单地重启一个应用程序(在我的例子中是一个控制台窗口应用程序)。

我发现了一个非常好的动手实验教程(用C++编写),我在http://msdn.microsoft.com/en-us/Windows7TrainingCourse_Win7Session0Isolation

  • I上尝试过,它适用于会话0隔离:
  1. C++
  2. in C#。经过几次测试后,它起作用了。只要我保持登录状态,而不是注销并再次登录,代码就会完美工作。我必须执行一些操作来捕获会话注销/登录。但是对于简单的登录和工作条件,看门狗可以正常工作。
  3. 是所需的PInvoke代码:

StructLayout(LayoutKind.Sequential) public struct STARTUPINFO { public int cb;public String lpReserved;public String lpDesktop;public String lpTitle;public uint dwX;public uint dwY;public uint dwXSize;public uint dwYSize;public uint dwXCountChars;public uint dwYCountChars;public uint dwFillAttribute;public uint dwFlags;public short wShowWindow;public short cbReserved2;public IntPtr lpReserved2;public IntPtr hStdInput;public;public;} StructLayout(LayoutKind.Sequential) public struct PROCESS_INFORMATION { public IntPtr hProcess;public IntPtr hThread;public uint dwProcessId;public uint dwThreadId;} public enum TOKEN_TYPE { TokenPrimary = 1,TokenImpersonation } public enum SECURITY_IMPERSONATION_LEVEL { SecurityAnonymous,SecurityIdentification,SecurityImpersonation,SecurityDelegation } StructLayout(LayoutKind.Sequential) public struct { public int;public;public int;} DllImport("kernel32.dll",EntryPoint = "CloseHandle",SetLastError = true,CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall) public extern static bool CloseHandle(IntPtr句柄);DllImport("kernel32.dll") public static extern uint WTSGetActiveConsoleSessionId();DllImport("wtsapi32.dll",SetLastError = true) public static extern bool WTSQueryUserToken(UInt32 sessionId,out IntPtr Token);DllImport("advapi32.dll",EntryPoint = "CreateProcessAsUser",SetLastError = true,CharSet = CharSet.Ansi,CallingConvention = CallingConvention.StdCall)公共外部静态布尔CreateProcessAsUser(IntPtr hToken,String lpApplicationName,String lpCommandLine,ref SECURITY_ATTRIBUTES lpProcessAttributes,ref SECURITY_ATTRIBUTES lpThreadAttributes,bool bInheritHandle,int dwCreationFlags,#EN20#,String,ref STARTUPINFO,out );DllImport("advapi32.dll",CharSet = CharSet.Auto,SetLastError = true)公共外部静态布尔DuplicateTokenEx( IntPtr hExistingToken,uint dwDesiredAccess,ref SECURITY_ATTRIBUTES lpTokenAttributes,SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,TOKEN_TYPE TokenType,out IntPtr Advapi32.dll是封装的方法:

private void CreateUserProcess() { bool ret;SECURITY_ATTRIBUTES sa = SECURITY_ATTRIBUTES();uint dwSessionID = WTSGetActiveConsoleSessionId();this.EventLog.WriteEntry("WTSGetActiveConsoleSessionId:“+ dwSessionID,EventLogEntryType.FailureAudit);IntPtr Token =新IntPtr();ret = WTSQueryUserToken((UInt32)dwSessionID,out Token);if (ret == false) { this.EventLog.WriteEntry("WTSQueryUserToken failed with”+ Marshal.GetLastWin32Error(),EventLogEntryType.FailureAudit);} const uint MAXIMUM_ALLOWED = 0x02000000;IntPtr DupedToken = IntPtr.Zero;ret = DuplicateTokenEx(Token,MAXIMUM_ALLOWED,ref sa,SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,TOKEN_TYPE.TokenPrimary,out DupedToken);if (ret == false) { this.EventLog.WriteEntry("DuplicateTokenEx failed“+ Marshal.GetLastWin32Error(),EventLogEntryType.FailureAudit);} else { this.EventLog.WriteEntry("DuplicateTokenEx SUCCESS",EventLogEntryType.SuccessAudit);} STARTUPINFO si = new STARTUPINFO();si.cb = Marshal.SizeOf(si);//si.lpDesktop = "";string commandLinePath;// commandLinePath示例:"c:\myapp.exe c:\myconfig.xml“。cmdLineArgs可以省略commandLinePath = AppPath +“”+ CmdLineArgs;PROCESS_INFORMATION pi = PROCESS_INFORMATION();//CreateProcessAsUser(hDuplicatedToken,NULL,lpszClientPath,NULL,NULL,FALSE,// 0,// null,NULL,&si,&pi) ret = CreateProcessAsUser(DupedToken,NULL,commandLinePath,ref sa,ref sa,false,0,(IntPtr)0,null,ref si,out pi);if (ret == false) { this.EventLog.WriteEntry("CreateProcessAsUser failed with“+ Marshal.GetLastWin32Error(),EventLogEntryType.FailureAudit);} else { this.EventLog.WriteEntry("CreateProcessAsUser SUCCESS.子PID为“+ pi.dwProcessId,EventLogEntryType.SuccessAudit);CloseHandle(pi.hProcess);CloseHandle(pi.hThread);} ret = CloseHandle(DupedToken);if (ret == false) { this.EventLog.WriteEntry("CloseHandle LastError:”+ Marshal.GetLastWin32Error(),EventLogEntryType.Error);} else { this.EventLog.WriteEntry("CloseHandle SUCCESS",EventLogEntryType.Information);} }

我希望它是有用的!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4147821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档