private void button1_Click(object sender, EventArgs e)
        {
            Process[] p = Process.GetProcessesByName("War3");
            p[0].Kill();
        }其他的进程都可以关闭,魔兽3的进程失败,求大大解释!

解决方案 »

  1.   

    用这个方法关闭,100%成功,你的那个是要进程正常退出,因此失败。        [Flags]
            public enum ProcessAccessFlags : uint
            {
                All = 0x001F0FFF,
                Terminate = 0x00000001,
                CreateThread = 0x00000002,
                VMOperation = 0x00000008,
                VMRead = 0x00000010,
                VMWrite = 0x00000020,
                DupHandle = 0x00000040,
                SetInformation = 0x00000200,
                QueryInformation = 0x00000400,
                Synchronize = 0x00100000
            }        [DllImport("kernel32.dll")]
            static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId);        [DllImport("kernel32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);        [DllImport("kernel32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool GetExitCodeProcess(IntPtr hProcess, out uint lpExitCode);        private void KillProcess(string processId,string processName)
            {
                IntPtr killer;
                try
                {
                    killer = OpenProcess(ProcessAccessFlags.All, false, uint.Parse(processId));
                    MessageBox.Show(killer.ToString());
                    uint exitcode = 0;
                    bool ret = GetExitCodeProcess(killer, out exitcode);
                    ret = TerminateProcess(killer, exitcode);
                    if (ret)
                        MessageBox.Show(string.Format("成功关闭程序{0}。", processName));
                    else
                    {
                        exitcode = 0;
                        MessageBox.Show(string.Format("杀死进程{0}时失败,原因不明。", processName));
                    }
                }
                catch (Exception Exc)
                {
                    MessageBox.Show(string.Format("杀死进程{0}时失败,原因是{1}。", processName, Exc.Message));
                }
            }
      

  2.   


    原来如此,高手多谢,另外想问下楼主方法的原理,看不太明白,只会调用,另外这个processId怎么获取,比如魔兽3 的processId。
      

  3.   

    Process.GetProcessesByName("War3")返回的这个Process对象里,有个ID属性,就是processId了。
      

  4.   


    程序调试好了,魔兽3 的进程关不掉, MessageBox.Show(string.Format("杀死进程{0}时失败,原因不明。", processName));
      

  5.   

    代码如下:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;namespace exe_controller
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                PKill pkill = new PKill();
                Process[] ps = Process.GetProcesses();
                foreach (Process item in ps)
                {
                    if (item.ProcessName == "War3")
                    {
                        int i;
                        i = item.Id;
                        pkill.KillProcess(i.ToString(), "War3");
                    }
                }
            }
        }
    }