Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

C# Detect if Debugger is Attached

, 19 Oct 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
C# detect if debugger is attached

This method is used to detect if a running process has a debugger attached to it. It involves using CheckRemoteDebuggerPresent, imported from kernel32.dll via PInvoke.

* tested on Visual Studio's Debugger & OllyDbg

How To...

First, include the following two lines in your program (which will import CheckRemoteDebuggerPresent):

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

Now, this method is pretty simple to use since it takes only 2 arguments:

  1. IntPtr hProcess = the target process' handle
  2. ref bool isDebuggerPresent = pointer that indicates the result

This method does all the 'hard work', so no further code is required: 

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class DetectDebugger
{
    [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("Debugger Attached: " + isDebuggerPresent);
        Console.ReadLine();
    }
}

Update

In order to avoid any confusion about Debugger.IsAttached and IsDebuggerPresent - sorry I didn't mention this earlier in the tip:

  • IsDebuggerPresent = works for any running process and detects native debuggers too
  • Debugger.IsAttached = works only for the current process and detects only managed debuggers. OllyDbg won't be detected by this.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Apex95
Student
Romania Romania
Student @ ACS (Politehnica), developer wannabe, mad C# fan and webmaster of coding.vision

Comments and Discussions

 
GeneralMy vote of 3 PinmemberFlorian Rosmann17-Oct-13 23:27 
Why should I use this instead of Debugger.IsAttached(System.Diagnostics)? PS: It is a very short article. You may change it to a tip?
GeneralRe: My vote of 3 PinmemberApex9518-Oct-13 2:24 
GeneralRe: My vote of 3 PinmemberDan Colasanti18-Oct-13 4:01 
GeneralRe: My vote of 3 PinmemberGiuseppe Tollini19-Oct-13 2:12 
QuestionFor current process IsDebuggerPresent is enough PinmemberAnandChavali17-Oct-13 19:53 
GeneralThoughts PinprofessionalPIEBALDconsult17-Oct-13 19:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 19 Oct 2013
Article Copyright 2013 by Apex95
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid