问题解决了,必须把方法分享出来造福大家!!!
在WinXP和Win2003环境中,安装服务后,右键单击服务“属性”-“登录”选项卡-选择“本地系统帐户”并勾选“允许服务与桌面交互”即可.
在Win7及以后的环境中,由于微软加强了权限管理,将此功能禁用,需要引用第三方dll,
Cjwdev.WindowsApi.dll下载路径:链接:http://pan.baidu.com/s/1c2xfJNE 密码:w4nf
使用方法:
public void AppStart(string appPath) { try { string appStartPath = appPath; IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsolesessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser( userTokenHandle, appStartPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) ApiDefinitions.CloseHandle(userTokenHandle); int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { } }—— —— —— —— 分界线 —— —— —— —— —— ——
最近想写一个进程守护程序并把它做成服务,结果发现在服务中启动带窗体的应用程序,只有进程看不到窗体。
找了很多文章,最后选择了一个答案:
网上也有叫session0穿透的,具体的大家可以找找。
(我单独写的程序启动和服务都没有问题,但是当而二者放到一起就出现问题了)
下面我把我的程序启动代码粘出来给大家看一下,并把我找的几篇好一点的文章分享给大家。
static void Main(string[] args) { string appName = "QQ";//the path of the exe file string appPath = @"C:/Program Files (x86)/Tencent/QQ/Bin/QQ.exe";//the path of the exe file bool runFlag = false; Process[] myProcesses = Process.GetProcesses(); foreach (Process myProcess in myProcesses) { if (myProcess.ProcessName.CompareTo(appName) == 0) { runFlag = true; } } if (!runFlag) //如果程序没有启动 { Process proc = new Process(); proc.StartInfo.FileName = appName; proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(appPath); proc.Start(); } else if (runFlag) //如果程序已经启动 { Process[] myPro = Process.GetProcessesByName(appName); myPro[0].Kill(); //删除进程 Process proc = new Process(); proc.StartInfo.FileName = appName; proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(appPath); proc.Start(); } }C#写windows服务:http://www.cnblogs.com/knowledgesea/p/3616127.htmlhttp://bbs.csdn.net/topics/380030333
C#做服务使用Process启动外部程序没窗体?:http://bbs.csdn.net/topics/380030333:
穿透Session 0 隔离(一):http://www.cnblogs.com/gnielee/archive/2010/04/07/session0-isolation-part1.html
PS:在win10下或者windows 2012R2及以上知道怎么用windows服务做守护进程的去那个告诉我一声,不胜感激!!!
新闻热点
疑难解答