Click here to Skip to main content
13,088,101 members (75,753 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


21 bookmarked
Posted 17 Oct 2013

C# Detect if Debugger is Attached

, 19 Oct 2013
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);


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.


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


About the Author

Romania Romania
Student @ ACS (UPB), developer wannabe, mad C# fan and webmaster of

You may also be interested in...

Comments and Discussions

QuestionUseless Pin
Member 84748665-Jun-17 0:27
memberMember 84748665-Jun-17 0:27 
GeneralMy vote of 3 Pin
Florian Rosmann17-Oct-13 22:27
memberFlorian Rosmann17-Oct-13 22:27 
GeneralRe: My vote of 3 Pin
Apex9518-Oct-13 1:24
memberApex9518-Oct-13 1:24 
GeneralRe: My vote of 3 Pin
Dan Colasanti18-Oct-13 3:01
memberDan Colasanti18-Oct-13 3:01 
You should update your article with this information because it's not obvious and most devs won't read the comment responses to find it here.

I also suggest removing the line "It's a neat way to add a little bit of protection to your program, but don't expect too much since .NET is far from being safe", because there's nothing inherently unsafe about .NET - in fact, the opposite could easily be argued - that it's safer than native code because pointers to dynamically allocated objects are managed by the framework. In any case, I'm not sure how safety relates to checking whether a debugger is present/attached - if you are trying to make that point, it's not clear, so you may want to be more clear there.

You may also want to address the issue of the need for checking if the debugger is present in the first place (for example, many devs will argue that just calling System.Diagnostics.Debug.WriteLine does not need to have a IsDebuggerAttached conditional around it) and whether native debuggers like OllyDbg can have their calls stripped from a Release build like the VS debugger does (useful for minimizing Release build footprint & improving performance). I think that would add value to your article, since most .NET developers wouldn't know that about OllyDbg.

Good luck!
GeneralRe: My vote of 3 Pin
Giuseppe Tollini19-Oct-13 1:12
memberGiuseppe Tollini19-Oct-13 1:12 
QuestionFor current process IsDebuggerPresent is enough Pin
AnandChavali17-Oct-13 18:53
memberAnandChavali17-Oct-13 18:53 
GeneralThoughts Pin
PIEBALDconsult17-Oct-13 18:28
professionalPIEBALDconsult17-Oct-13 18:28 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 19 Oct 2013
Article Copyright 2013 by Apex95
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid