首页 > 系统相关 >windows 服务运行启动桌面程序

windows 服务运行启动桌面程序

时间:2024-10-24 12:01:01浏览次数:1  
标签:IntPtr process 桌面 windows 程序 int uint ref public

备注

普通程序直接启动即可,注意程序运行的当前目录,如果不好改源码可以通过cmd切换目录再执行。
部分情况下可以通过配置服务的登录属性打开允许服务与桌面交互即可,特殊情况下再使用本文的方法。

使用

使用方法

                UserProcess.PROCESS_INFORMATION pInfo = new UserProcess.PROCESS_INFORMATION();
                UserProcess.StartProcessAndBypassUAC("c:\\test.exe", string.Empty, out pInfo);

帮助类

    public class UserProcess
    {
        #region Structures
        [StructLayout(LayoutKind.Sequential)]
        public struct SECURITY_ATTRIBUTES
        {
            public int Length;
            public IntPtr lpSecurityDescriptor;
            public bool bInheritHandle;
        }
        [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 IntPtr hStdOutput;
            public IntPtr hStdError;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct PROCESS_INFORMATION
        {
            public IntPtr hProcess;
            public IntPtr hThread;
            public uint dwProcessId;
            public uint dwThreadId;
        }
        #endregion
        #region Enumerations
        enum TOKEN_TYPE : int
        {
            TokenPrimary = 1,
            TokenImpersonation = 2
        }
        enum SECURITY_IMPERSONATION_LEVEL : int
        {
            SecurityAnonymous = 0,
            SecurityIdentification = 1,
            SecurityImpersonation = 2,
            SecurityDelegation = 3,
        }
        enum WTSInfoClass
        {
            InitialProgram,
            ApplicationName,
            WorkingDirectory,
            OEMId,
            SessionId,
            UserName,
            WinStationName,
            DomainName,
            ConnectState,
            ClientBuildNumber,
            ClientName,
            ClientDirectory,
            ClientProductId,
            ClientHardwareId,
            ClientAddress,
            ClientDisplay,
            ClientProtocolType
        }
        #endregion
        #region Constants
        public const int TOKEN_DUPLICATE = 0x0002;
        public const uint MAXIMUM_ALLOWED = 0x2000000;
        public const int CREATE_NEW_CONSOLE = 0x00000010;
        public const int IDLE_PRIORITY_CLASS = 0x40;
        public const int NORMAL_PRIORITY_CLASS = 0x20;
        public const int HIGH_PRIORITY_CLASS = 0x80;
        public const int REALTIME_PRIORITY_CLASS = 0x100;
        #endregion
        #region Win32 API Imports
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool CloseHandle(IntPtr hSnapshot);
        [DllImport("kernel32.dll")]
        static extern uint WTSGetActiveConsoleSessionId();
        [DllImport("wtsapi32.dll", CharSet = CharSet.Unicode, SetLastError = true), SuppressUnmanagedCodeSecurityAttribute]
        static extern bool WTSQuerySessionInformation(System.IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);
        [DllImport("advapi32.dll", EntryPoint = "CreateProcessAsUser", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public extern static bool CreateProcessAsUser(IntPtr hToken, String lpApplicationName, String lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
            ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandle, int dwCreationFlags, IntPtr lpEnvironment,
            String lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
        [DllImport("kernel32.dll")]
        static extern bool ProcessIdToSessionId(uint dwProcessId, ref uint pSessionId);
        [DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx")]
        public extern static bool DuplicateTokenEx(IntPtr ExistingTokenHandle, uint dwDesiredAccess,
            ref SECURITY_ATTRIBUTES lpThreadAttributes, int TokenType,
            int ImpersonationLevel, ref IntPtr DuplicateTokenHandle);
        [DllImport("kernel32.dll")]
        static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
        [DllImport("advapi32", SetLastError = true), SuppressUnmanagedCodeSecurityAttribute]
        static extern bool OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);
        #endregion
        public static string GetCurrentActiveUser()
        {
            IntPtr hServer = IntPtr.Zero, state = IntPtr.Zero;
            uint bCount = 0;
            // obtain the currently active session id; every logged on user in the system has a unique session id  
            uint dwSessionId = WTSGetActiveConsoleSessionId();
            string domain = string.Empty, userName = string.Empty;
            if (WTSQuerySessionInformation(hServer, (int)dwSessionId, WTSInfoClass.DomainName, out state, out bCount))
            {
                domain = Marshal.PtrToStringAuto(state);
            }
            if (WTSQuerySessionInformation(hServer, (int)dwSessionId, WTSInfoClass.UserName, out state, out bCount))
            {
                userName = Marshal.PtrToStringAuto(state);
            }
            return string.Format("{0}\\{1}", domain, userName);
        }
        /// <summary>  
        /// Launches the given application with full admin rights, and in addition bypasses the Vista UAC prompt  
        /// </summary>  
        /// <param name="applicationName">The name of the application to launch</param>  
        /// <param name="procInfo">Process information regarding the launched application that gets returned to the caller</param>  
        /// <returns></returns>  
        public static bool StartProcessAndBypassUAC(String applicationName, String command, out PROCESS_INFORMATION procInfo)
        {
            uint winlogonPid = 0;
            IntPtr hUserTokenDup = IntPtr.Zero, hPToken = IntPtr.Zero, hProcess = IntPtr.Zero;
            procInfo = new PROCESS_INFORMATION();
            // obtain the currently active session id; every logged on user in the system has a unique session id  
            uint dwSessionId = WTSGetActiveConsoleSessionId();
            // obtain the process id of the winlogon process that is running within the currently active session  
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("winlogon");
            foreach (System.Diagnostics.Process p in processes)
            {
                if ((uint)p.SessionId == dwSessionId)
                {
                    winlogonPid = (uint)p.Id;
                }
            }
            // obtain a handle to the winlogon process  
            hProcess = OpenProcess(MAXIMUM_ALLOWED, false, winlogonPid);
            // obtain a handle to the access token of the winlogon process  
            if (!OpenProcessToken(hProcess, TOKEN_DUPLICATE, ref hPToken))
            {
                CloseHandle(hProcess);
                return false;
            }
            // Security attibute structure used in DuplicateTokenEx and CreateProcessAsUser  
            // I would prefer to not have to use a security attribute variable and to just   
            // simply pass null and inherit (by default) the security attributes  
            // of the existing token. However, in C# structures are value types and therefore  
            // cannot be assigned the null value.  
            SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
            sa.Length = Marshal.SizeOf(sa);
            // copy the access token of the winlogon process; the newly created token will be a primary token  
            if (!DuplicateTokenEx(hPToken, MAXIMUM_ALLOWED, ref sa, (int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification, (int)TOKEN_TYPE.TokenPrimary, ref hUserTokenDup))
            {
                CloseHandle(hProcess);
                CloseHandle(hPToken);
                return false;
            }
            // By default CreateProcessAsUser creates a process on a non-interactive window station, meaning  
            // the window station has a desktop that is invisible and the process is incapable of receiving  
            // user input. To remedy this we set the lpDesktop parameter to indicate we want to enable user   
            // interaction with the new process.  
            STARTUPINFO si = new STARTUPINFO();
            si.cb = (int)Marshal.SizeOf(si);
            si.lpDesktop = @"winsta0\default"; // interactive window station parameter; basically this indicates that the process created can display a GUI on the desktop  
                                               // flags that specify the priority and creation method of the process  
            int dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;
            // create a new process in the current user's logon session  
            bool result = CreateProcessAsUser(hUserTokenDup,        // client's access token  
                                            applicationName,        // file to execute  
                                            command,                // command line  
                                            ref sa,                 // pointer to process SECURITY_ATTRIBUTES  
                                            ref sa,                 // pointer to thread SECURITY_ATTRIBUTES  
                                            false,                  // handles are not inheritable  
                                            dwCreationFlags,        // creation flags  
                                            IntPtr.Zero,            // pointer to new environment block   
                                            null,                   // name of current directory   
                                            ref si,                 // pointer to STARTUPINFO structure  
                                            out procInfo            // receives information about new process  
                                            );
            // invalidate the handles  
            CloseHandle(hProcess);
            CloseHandle(hPToken);
            CloseHandle(hUserTokenDup);
            return result; // return the result  
        }
    }
}

[参考]
C# windows服务程序开机自启动exe程序

标签:IntPtr,process,桌面,windows,程序,int,uint,ref,public
From: https://www.cnblogs.com/ives/p/18499318

相关文章

  • 基于Android的的酒店管理APP小程序实现(源码+lw+部署文档+讲解等)
    项目整体介绍基于安卓Android的酒店管理APP和小程序可以为酒店提供更便捷的管理方式和更好的客户服务体验。一、背景随着移动互联网的发展,越来越多的人习惯使用手机进行各种操作,包括酒店预订、入住登记、服务请求等。因此,开发一款基于安卓Android的酒店管理APP......
  • 在Windows中,可以使用PowerShell来迁移打印机设置,以下是用于导出和导入打印机配置的Pow
    在Windows中,可以使用PowerShell来迁移打印机设置,以下是用于导出和导入打印机配置的PowerShell代码示例。导出打印机设置使用以下命令导出当前打印机设置到一个文件:powershellCopyCodeGet-Printer|Export-Clixml-Path"C:\path\to\exported_printers.xml"导入打印机设置......
  • Windows 调试工具课程——在软件万种死法中调试出原因
     参考:https://blog.lindexi.com/post/Windows-%E8%B0%83%E8%AF%95%E5%B7%A5%E5%85%B7%E8%AF%BE%E7%A8%8B.html 本文是我在集团内部上的课程记录而成的博客内容。在本次课程里面将和大家介绍一些在Windows上常用的调试工具,以及调查问题的常见套路。适合于伙伴们入门Windows......
  • Springboot汽车租赁管理系统f3fla(程序+源码+数据库+调试部署+开发环境)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表用户,商家,门店信息,汽车类型,汽车信息,保险信息,保险类别,汽车租赁,还车信息,统计信息开题报告内容一、研究背景随着城市化进程的加速和人们对出行便捷性需求......
  • Springboot汽车维修服务管理9041t(程序+源码+数据库+调试部署+开发环境)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表用户,车辆登记,员工,供应商,维修厂,项目分类,项目信息,项目预约,车辆预检,车辆维修,质量检查,增值服务,订单支付,商品分类,商品信息,出库领料,部门,部门调度,入职......
  • Windows、macOS和Linux系统安装Python的指南
    在不同的操作系统上安装Python通常涉及几个简单的步骤。以下是在Windows、macOS和Linux系统上安装Python的指南:Windows系统安装Python:下载Python安装程序:访问Python官方网站 python.org。选择适合Windows的Python版本(确保下载最新稳定版)。运行安装程序:双击下载的.exe文......
  • 人工智能驱动的编程新时代:从自动化到智能化的技术变革(附代码)--Happy1024程序员节
    PS:尊敬的程序员同仁们,值此1024程序员节的到来,向每一位在代码世界中辛勤耕耘的小伙伴们致以诚挚的祝福和敬意!我们的努力与创新,犹如一束光芒,照亮了数字时代的每一个角落。正是我们用智慧与汗水,构建了生活中不可或缺的科技基础。从解决复杂问题到实现日常便利,程序员的每一行代码......
  • 群控系统服务端开发模式-程序草图设计
    本系统采用PHP开发,采用thinkphp6作为php应用框架。一、总控制    1、Base.php        a、是否是登录后的验证父类,也就是需要登录才能操作的控制都需要继承他;        b、继承BaseController.php;    2、Emptys.php  ......
  • 1024程序员节:为何不放假?—— 一场关于社会地位与资源分配的反思
    目录一场关于社会地位与资源分配的反思IT从业者的社会贡献对比其他行业医生教师公务员程序员的现状社会地位工作环境呼吁改变企业责任社会认知个人觉醒结语一场关于社会地位与资源分配的反思在这个数字化时代,信息技术从业者(IT从业者)无疑是推动社会进步的重要力......
  • 【文件加密系统】华企盾DSC服务程序启动失败解决办法
    问题原因:1.sa账户密码错误导致连接数据数据库失败无法启动DSC服务解决方法:用windows身份验证进入数据库更改sa用户密码:安全性>登录名>sa>右键属性>更改密码※如果显示请输入秘钥更改,使用更改完密码的sa账户登录数据库,在数据库>DSEDB>表>dbo.FileEncryptKey_TABLE_>右键编......