Search code examples
c#.netvisual-studioremote-debugging

Is there a way to detect whether the attached debugger is remote-debugger?


Using System.Diagnostics.Debugger.Debugger.IsAttached I can tell that a debugger is attached. Is there a way to detect whether the attached debugger is remote-debugger (Visual Studio Remote Debugger Monitor)?


Solution

  • You can use the native CheckRemoteDebuggerPresent from kernel32.dll

    From MSDN:

    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.

    You can use it as follows:

    [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
    static extern bool CheckRemoteDebuggerPresent(IntPtr hProcess, ref bool isDebuggerPresent);
    
    public static void Main()
    {
        bool isDebuggerPresent = false;
        CheckRemoteDebuggerPresent(Process.GetCurrentProcess().Handle, ref isDebuggerPresent);
    
        Console.WriteLine(string.Format("Debugger Attached: {0}", isDebuggerPresent));
        Console.ReadLine();
    }