[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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM