isdebuggerpresent (kernel32)
Last changed: -186.136.223.176

.
Summary

VB.Net Signature:

Private Declare Function IsDebuggerPresent Lib "kernel32" () As Integer

C# Signature:

[DllImport("kernel32.dll")]
static extern bool IsDebuggerPresent();

Boo Signature:

[DllImport("kernel32.dll", SetLastError : true)]
def IsDebuggerPresent() as bool:
     pass

User-Defined Types:

None.

VB Usage:

Private Declare Function IsDebuggerPresent Lib "kernel32" () As Integer

  Try

        If IsDebuggerPresent = 1 Then

        MsgBox("Debugger is present!") 'Debugger was detected

        Else

        MsgBox("Debugger isn't present!") Debugger was not detected

        End If

    Catch ex As Exception

        MsgBox(ex.Message)

    End Try

Notes:

This is a quick way to help protect your applications from being reverse engineered at runtime. Call this when your application starts and exit if it returns true.

Tips & Tricks:

Call this before your application starts to protect it from runtime analysis. This is a weak form of protection, and is trivially bypassed. For a more robust mechanism, use a .NET obfuscator tool.

Sample Code:

// debug checking in debug builds would make it impossible to debug in Visual Studio
#if !DEBUG
   if (IsDebuggerPresent() == true) {
      Application.Exit();
   }
#endif

Alternative Managed API:

Do you know one? Please contribute it!

Documentation