有没有办法检测调试器是否从 C# 附加到另一个进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2188201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:13:29  来源:igfitidea点击:

Is there a way to detect if a debugger is attached to another process from C#?

c#debugging

提问by Lucas Meijer

I have a program that Process.Start()another program, and it shuts it down after N seconds.

我有一个程序,Process.Start()另一个程序,它在 N 秒后关闭它。

Sometimes I choose to attach a debugger to the started program. In those cases, I don't want the process shut down after N seconds.

有时我选择将调试器附加到启动的程序。在这些情况下,我不希望进程在 N 秒后关闭。

I'd like the host program to detect if a debugger is attached or not, so it can choose to not shut it down.

我希望主机程序检测是否连接了调试器,因此它可以选择不关闭它。

Clarification: I'm not looking to detect if a debugger is attached to myprocess, I'm looking to detect if a debugger is attached to the process I spawned.

说明:我不是要检测调试器是否连接到我的进程,而是要检测调试器是否连接到我生成的进程。

采纳答案by itowlson

You will need to P/Invokedown to CheckRemoteDebuggerPresent. This requires a handle to the target process, which you can get from Process.Handle.

您将需要P/InvokeCheckRemoteDebuggerPresent。这需要目标进程的句柄,您可以从 Process.Handle 中获取该句柄。

回答by Bryan Watts

if(System.Diagnostics.Debugger.IsAttached)
{
    // ...
}

回答by Aeka

I know this is old, but I was having the same issue and realized if you have a pointer to the EnvDTE, you can check if the process is in Dte.Debugger.DebuggedProcesses:

我知道这是旧的,但我遇到了同样的问题,并意识到如果你有一个指向 EnvDTE 的指针,你可以检查进程是否在Dte.Debugger.DebuggedProcesses 中

foreach (EnvDTE.Process p in Dte.Debugger.DebuggedProcesses) {
  if (p.ProcessID == spawnedProcess.Id) {
    // stuff
  }
}

The CheckRemoteDebuggerPresent call only checks if the process is being natively debugged, I believe - it won't work for detecting managed debugging.

CheckRemoteDebuggerPresent 调用仅检查进程是否正在本地调试,我相信 - 它不适用于检测托管调试。

回答by Adi

The solution for me is Debugger.IsAttached as described here: http://www.fmsinc.com/free/NewTips/NET/NETtip32.asp

我的解决方案是 Debugger.IsAttached,如下所述:http: //www.fmsinc.com/free/NewTips/NET/NETtip32.asp

回答by Drew Noakes

Is the currentprocess being debugged?

目前正在调试的进程?

var isDebuggerAttached = System.Diagnostics.Debugger.IsAttached;

Is anotherprocess being debugged?

是否正在调试另一个进程?

Process process = ...;
bool isDebuggerAttached;
if (!CheckRemoteDebuggerPresent(process.Handle, out isDebuggerAttached)
{
    // handle failure (throw / return / ...)
}
else
{
    // use isDebuggerAttached
}


/// <summary>Checks whether a process is being debugged.</summary>
/// <remarks>
/// The "remote" in CheckRemoteDebuggerPresent does not imply that the debugger
/// necessarily resides on a different computer; instead, it indicates that the 
/// debugger resides in a separate and parallel process.
/// <para/>
/// Use the IsDebuggerPresent function to detect whether the calling process 
/// is running under the debugger.
/// </remarks>
[DllImport("Kernel32.dll", SetLastError=true, ExactSpelling=true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CheckRemoteDebuggerPresent(
    SafeHandle hProcess,
    [MarshalAs(UnmanagedType.Bool)] ref bool isDebuggerPresent);

Within a Visual Studio extension

在 Visual Studio 扩展中

Process process = ...;
bool isDebuggerAttached = Dte.Debugger.DebuggedProcesses.Any(
    debuggee => debuggee.ProcessID == process.Id);