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

问题描述 投票:0回答:1

我正在使用一个定时器,它不断检查调试器是否附加如下:

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

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

c# debugging
1个回答
2
投票

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

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

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

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

qazxsw poi =适用于任何正在运行的进程,并检测本机调试器到qazxsw poi =仅适用于当前进程并仅检测托管调试器。

© www.soinside.com 2019 - 2024. All rights reserved.