using Cjwdev.WindowsApi;
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Timers;

namespace MWS.Daemon
{
    public partial class Service1 : ServiceBase
    {
        private string exePath;
        private int timerInterval;
        private string exeName;
        private string workPath;

        public Service1()
        {
            InitializeComponent();
        }


        protected override void OnStart(string[] args)
        {

            exePath = ConfigurationManager.AppSettings["exePath"].Trim();
            timerInterval = Convert.ToInt32(ConfigurationManager.AppSettings["timerInterval"].Trim());
            exeName = Path.GetFileName(exePath).Replace(".exe", "");
            workPath = Path.GetDirectoryName(exePath);

            System.Timers.Timer timer;
            timer = new System.Timers.Timer();
            timer.Interval = timerInterval;//设置计时器事件间隔执行时间
            timer.Elapsed += new System.Timers.ElapsedEventHandler(Circulation);
            timer.Enabled = true;

        }

        private void Circulation(object sender, ElapsedEventArgs e)
        {
            Process[] myProcesses = Process.GetProcesses();
            foreach (Process myProcess in myProcesses)
            {
                if (myProcess.ProcessName.Contains("HDCraneCIMS.IPC.Desk"))
                {
                    return;
                }
            }

            try
            {
                /*
                System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
                myProcess.StartInfo.FileName = "cmd.exe";//启动cmd命令
                myProcess.StartInfo.UseShellExecute = false;//是否使用系统外壳程序启动进程
                myProcess.StartInfo.RedirectStandardInput = true;//是否从流中读取
                myProcess.StartInfo.RedirectStandardOutput = true;//是否写入流
                myProcess.StartInfo.RedirectStandardError = true;//是否将错误信息写入流
                myProcess.StartInfo.CreateNoWindow = true;//是否在新窗口中启动进程
                myProcess.Start();//启动进程
                myProcess.StandardInput.WriteLine("shutdown -r -t 60");//执行重启计算机命令
                */


                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)System.Runtime.InteropServices.Marshal.SizeOf(startInfo);

                ApiDefinitions.CreateProcessAsUser(
                    userTokenHandle,
                    exePath,
                    "",
                    IntPtr.Zero,
                    IntPtr.Zero,
                    false,
                    0,
                    IntPtr.Zero,
                    workPath,
                    ref startInfo,
                    out procInfo);

                if (userTokenHandle != IntPtr.Zero)
                    ApiDefinitions.CloseHandle(userTokenHandle);

                int _currentAquariusProcessId = (int)procInfo.dwProcessId;
            }
            catch (Exception ex)
            {

            }
        }

        protected override void OnStop()
        {
        }
    }
}
