[C#]做服务使用Process启动外部程序没窗体

这几天会到一个需要,要时时侦测文件生成,并上传到Server上,侦测文件生成使用的FileSystemWatch。但是时时运行遇到了问题,程序可能会人为退出或者意外终止,使用一个进程监控程序的监程,也有意外关掉的情况,想来想去,还是Windows服务比较可靠,只要开机就在运行,而且服务是系统高级应用,一般人不知道关闭。

一切都差多的时候,发现Windows服务调用外部程序(Process),程序竟然没有界面,只有进程,试了好多方法,原因是Win10为了安全,拿掉之前Win7&XP的交互式Windows服务,所以Win10下直接调用是不行的,在网上找到有大神写的Cjwdev.WindowsApi.dll,真的可以

在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服务做守护进程的去那个告诉我一声,不胜感激!!!

MSDN:

C#通过Windows服务启动其他窗体程序  https://q.cnblogs.com/q/77666/

 

c#用服务实现开机自动启动程序 

交互式 Windows 服务 (CSCreateProcessAsUserFromService)

猜你喜欢

转载自www.cnblogs.com/masonlu/p/9726541.html