Debugger.IsAttached仅在从visual studio启动时才有效

时间:2017-12-16 20:58:26

标签: c# debugging

我正在使用一个定时器,它不断检查是否附加了这样的调试器:

private void DebuggerCheck_Tick(object sender, EventArgs e)
{
    if (Debugger.IsAttached)
    {
        //Take action 
    }
}

这似乎不适用于外部调试器。当我通过Visual Studio启动程序时,它确实返回true,但是当我附加Visaul Studio调试器或任何其他外部调试器(如IDA,x64dbg,CheatEngine ......)时它不会检测到它们。知道为什么不这样做吗?

1 个答案:

答案 0 :(得分:2)

根据C# Detect if Debugger is Attached,属性Debugger.IsAttached将只检测托管调试器(当然应该包括visual studio调试器,感谢@RenéVogt)。您可以使用CheckRemoteDebuggerPresent代替,文档说明:

  

CheckRemoteDebuggerPresent中的“remote”并不意味着调试器必然驻留在另一台计算机上;相反,它表示调试器驻留在一个单独的并行进程中。使用IsDebuggerPresent函数检测调用进程是否在调试器下运行。

[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CheckRemoteDebuggerPresent(IntPtr hProcess, ref bool isDebuggerPresent);
  

为了避免对Debugger.IsAttachedIsDebuggerPresent造成任何混淆 - 抱歉,我在前面没有提到这一点:

     

IsDebuggerPresent =适用于任何正在运行的进程,并检测本机调试程序   Debugger.IsAttached =仅适用于当前进程,仅检测托管调试程序。

相关问题