using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ProcessTest
{
    class ProcessController
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern IntPtr OpenProcess(ProcessAccessFlags processAccess, bool bInheritHandle, int processId);

        public enum ProcessAccessFlags : uint
        {
            All = 0x001F0FFF,
            Terminate = 0x00000001,
            CreateThread = 0x00000002,
            VirtualMemoryOperation = 0x00000008,
            VirtualMemoryRead = 0x00000010,
            VirtualMemoryWrite = 0x00000020,
            DuplicateHandle = 0x00000040,
            CreateProcess = 0x000000080,
            SetQuota = 0x00000100,
            SetInformation = 0x00000200,
            QueryInformation = 0x00000400,
            QueryLimitedInformation = 0x00001000,
            Synchronize = 0x00100000
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool GetExitCodeProcess(IntPtr hProcess, out uint ExitCode);

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool CloseHandle(IntPtr hObject);

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);

        [DllImport("kernel32.dll")]
        static extern uint FormatMessage(uint dwFlags, IntPtr lpSource, uint dwMessageId, uint dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr Arguments);

        private const uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
        IntPtr _hProcess = IntPtr.Zero;

        /// 
        /// プロセスの開始
        /// 
        /// 
        public void Start(string name)
        {
            if (_hProcess == IntPtr.Zero)
            {
                using (Process pro = Process.Start(name))
                {
                    ProcessAccessFlags flg = ProcessAccessFlags.All /*.QueryInformation*/;
                    _hProcess = OpenProcess(flg, true, pro.Id);
                }
            }
        }

        /// 
        /// 終了コード取得
        /// 
        /// 
        public uint GetExitCode()
        {
            uint code = 0;
            if (GetExitCodeProcess(_hProcess, out code) == false)
            {
                string message = GetErrorMessage();
                throw new Exception(message);
            }
            return code;
        }

        /// 
        /// ハンドルを閉じる
        /// 
        public void Close()
        {
            if (CloseHandle(_hProcess) == false)
            {
                string message = GetErrorMessage();
                throw new Exception(message);
            }
            _hProcess = IntPtr.Zero;
        }

        /// 
        /// プロセス終了
        /// 
        /// 
        public uint Terminate()
        {
            uint code = 0;
            if(TerminateProcess(_hProcess, code) == false)
            {
                string message = GetErrorMessage();
                throw new Exception(message);
            }
            return code;
        }

        private string GetErrorMessage()
        {
            int errCode = Marshal.GetLastWin32Error();
            StringBuilder message = new StringBuilder(255);
            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, (uint)errCode, 0, message, message.Capacity, IntPtr.Zero);

            return message.ToString();
        }
    }
}

コメント

人気の投稿