像任务管理器(Windows 8)一样杀死进程,因为访问被拒绝

时间:2015-05-02 10:42:05

标签: c# process kill taskmanager win32-process

我有以下问题: 我尝试从c#代码中杀死一个在线游戏的过程。我总是得到“拒绝访问”!它有一个Anti Hack Shield,我认为这就是问题......

[编辑]
我将/ T添加到TaskKill命令行,现在获取带有PID“拒绝访问”的错误子进程 我在Process.GetProcessById()和主进程的Threads List中都找不到这个PID ..有人知道如何找到它吗?

但是使用Window任务管理器我可以杀了它! 通过使用P / invoke FindWindow()和GetWindowThreadProcessId()查找Window来获取我的Process 我检查了PID它是一样的! 我使用P / Invoke,因为无论如何我对SetFourgroundWindow()

都需要它

我也发现这在内核模式中是活跃的[1]它是一个在线游戏所以它使用网络适配器,这可能是问题吗?但为什么我可以用任务管理器杀死它? 在这种情况下,我找到了SetKernelObjectSecurity:http://csharptest.net/coding/我不确定我是否做得对,我只是将AceQualifier.AccessDenied更改为AccessAllowed无效。

那么我如何以与任务管理器相同的方式杀死它? 或者我可以调用任务管理器的方法吗? 也许我之前需要改变一些参数/权利?

我搜索了一下,我发现了SeDebugPrivilege的东西:[2]我使用它但仍然相同的“访问被拒绝”我也尝试使用Process.EnterDebugMode();

这是我尝试的方式:我以管理员身份运行Visual Studio

public void KillKal(IntPtr _WindowPointer)
{
    uint ProcessID;

    GetWindowThreadProcessId(_WindowPointer, out ProcessID);
    try
    {
        #region SeDebugPrivilege 
        IntPtr hToken;
        LUID luidSEDebugNameValue;
        TOKEN_PRIVILEGES tkpPrivileges;

        if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out hToken))
        {
            Console.WriteLine("OpenProcessToken() failed, error = {0} . SeDebugPrivilege is not available", Marshal.GetLastWin32Error());
            return;
        }
        else
        {
            Console.WriteLine("OpenProcessToken() successfully");
        }

        if (!LookupPrivilegeValue(null, SE_DEBUG_NAME, out luidSEDebugNameValue))
        {
            Console.WriteLine("LookupPrivilegeValue() failed, error = {0} .SeDebugPrivilege is not available", Marshal.GetLastWin32Error());
            CloseHandle(hToken);
            return;
        }
        else
        {
            Console.WriteLine("LookupPrivilegeValue() successfully");
        }

        tkpPrivileges.PrivilegeCount = 1;
        tkpPrivileges.Luid = luidSEDebugNameValue;
        tkpPrivileges.Attributes = SE_PRIVILEGE_ENABLED;

        if (!AdjustTokenPrivileges(hToken, false, ref tkpPrivileges, 0, IntPtr.Zero, IntPtr.Zero))
        {
            Console.WriteLine("LookupPrivilegeValue() failed, error = {0} .SeDebugPrivilege is not available", Marshal.GetLastWin32Error());
        }
        else
        {
            Console.WriteLine("SeDebugPrivilege is now available");
        }

        Process.EnterDebugMode();
        #endregion SeDebugPrivilege
        Process p = Process.GetProcessById((int)ProcessID);

        //1. try
        Process.Start(new ProcessStartInfo
        {
            FileName = "cmd.exe",
            CreateNoWindow = true,
            UseShellExecute = false,
            Verb = "runas",
            Arguments = string.Format("/c taskkill /pid {0} /f", ProcessID)
        });
        //2.try
        Process.Start(new ProcessStartInfo
        {
            FileName = "net.exe",
            CreateNoWindow = true,
            UseShellExecute = false,
            Verb = "runas",
            Arguments = string.Format("stop {0}", p.ProcessName)
        });
        //3. try
        Process.Start("cmd", string.Format("/c \"taskkill /f /pid {0}\"", ProcessID));

        //4. try get exception: access denided
        p.Kill();

        //5. try get exception:Process must exit before requested information can be determined.
        TerminateProcess(p.Handle, (uint)p.ExitCode);
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

我还尝试过使用PsKill和任务杀死管理cmd! https://postimg.org/image/qp5nkjvtn/

[1] http://stackoverflow.com/questions/14038165/kill-process-windows-8-issues

[2] http://hintdesk.com/c-how-to-enable-sedebugprivilege/

0 个答案:

没有答案