欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > c# 服务中启动exe窗体程序

c# 服务中启动exe窗体程序

2025/2/22 16:30:37 来源:https://blog.csdn.net/wangyue4/article/details/144886778  浏览:    关键词:c# 服务中启动exe窗体程序

Windows服务默认在会话0(Session 0)中运行,这是一个隔离的环境,旨在防止服务与应用程序和用户会话交互,从而提高系统的稳定性和安全性。由于这个原因,直接从服务启动的GUI应用程序将不会显示,因为它们没有与用户桌面交互的能力。

为了在用户会话中启动一个GUI应用程序,你可以使用CreateProcessAsUser函数。这个函数允许你在指定用户的安全上下文中创建一个新进程。以下是使用CreateProcessAsUser函数在特定用户会话中启动进程的步骤和示例代码:

步骤:

  1. 获取目标用户的安全令牌。
  2. 使用CreateProcessAsUser函数在新用户的会话中创建进程。

以下是一个C#示例,演示如何使用CreateProcessAsUser

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;public partial class YourService : ServiceBase
{[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);[DllImport("advapi32.dll", SetLastError = true)]private static extern bool CreateProcessAsUser(IntPtr hToken, String lpApplicationName, String lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, String lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInfo);[DllImport("kernel32.dll", SetLastError = true)][return: MarshalAs(UnmanagedType.Bool)]private static extern bool CloseHandle(IntPtr hObject);// 定义其他需要的结构体和常量...protected override void OnStart(string[] args){string applicationName = "PathToYourExe.exe";string username = "YourUsername";string domain = "YourDomain";string password = "YourPassword";IntPtr tokenHandle = IntPtr.Zero;IntPtr dupTokenHandle = IntPtr.Zero;PROCESS_INFORMATION procInfo = new PROCESS_INFORMATION();STARTUPINFO startupInfo = new STARTUPINFO();// LogonUser获取用户令牌bool loggedOn = LogonUser(username, domain, password, 2, 0, out tokenHandle);if (loggedOn){// 准备启动信息startupInfo.cb = Marshal.SizeOf(typeof(STARTUPINFO));// 复制令牌以获取必要的权限if (!DuplicateToken(tokenHandle, 2, ref dupTokenHandle)){// 处理错误}// 创建进程bool result = CreateProcessAsUser(dupTokenHandle, null, applicationName, ref SECURITY_ATTRIBUTES.Null, ref SECURITY_ATTRIBUTES.Null, false, 0, IntPtr.Zero, null, ref startupInfo, out procInfo);if (!result){// 处理错误}// 释放句柄CloseHandle(tokenHandle);CloseHandle(dupTokenHandle);CloseHandle(procInfo.hThread);CloseHandle(procInfo.hProcess);}else{// 处理LogonUser失败}}// 其他服务方法...
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词