调用window Api 进行对特定窗口的进程ID进行操作

        /// <summary>
        /// 获取窗口句柄
        /// </summary>
        /// <param name="lpClassName"></param>
        /// <param name="lpWindowName">窗口标题</param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="ID">返回的进程ID</param>
        /// <returns></returns>
        [DllImport("User32.dll", ExactSpelling = true)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
            IntPtr hwnd = FindWindow(null, "microsoft outlook");
            int pid = 0;
            int rssid= GetWindowThreadProcessId(hwnd, out pid);
            if (hwnd != IntPtr.Zero)
            {
                System.Diagnostics.Process item = System.Diagnostics.Process.GetProcessById(pid);

                if (item != null)
                {
                    if (!item.Responding)
                    {
                        item.Kill();
                    }
                }
            }

猜你喜欢

转载自www.cnblogs.com/zacklau/p/9687863.html
今日推荐